Skip to content

Commit

Permalink
[web] Add tests for js/components/common/FileChooser.jsx & minor fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewShao committed May 12, 2017
1 parent 9a7ac14 commit 782d564
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
38 changes: 38 additions & 0 deletions web/src/js/__tests__/components/common/FileChooserSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react'
import renderer from 'react-test-renderer'
import FileChooser from '../../../components/common/FileChooser'

describe('FileChooser Component', () => {
let openFileFunc = jest.fn(),
createNodeMock = () => { return { click: jest.fn() } },
fileChooser = renderer.create(
<FileChooser className="foo" title="bar" onOpenFile={ openFileFunc }/>
, { createNodeMock })
//[test refs with react-test-renderer](https://github.com/facebook/react/issues/7371)

it('should render correctly', () => {
let tree = fileChooser.toJSON()
expect(tree).toMatchSnapshot()
})

it('should handle click action', () => {
let tree = fileChooser.toJSON(),
mockEvent = {
preventDefault: jest.fn(),
target: {
files: [ "foo", "bar" ]
}
}
tree.children[1].props.onChange(mockEvent)
expect(openFileFunc).toBeCalledWith("foo")
tree.props.onClick()
// without files
mockEvent = {
...mockEvent,
target: { files: [ ]}
}
openFileFunc.mockClear()
tree.children[1].props.onChange(mockEvent)
expect(openFileFunc).not.toBeCalled()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`FileChooser Component should render correctly 1`] = `
<a
className="foo"
href="#"
onClick={[Function]}
title="bar"
>
<i
className="fa fa-fw undefined"
/>
<input
className="hidden"
onChange={[Function]}
type="file"
/>
</a>
`;
2 changes: 1 addition & 1 deletion web/src/js/components/common/FileChooser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function FileChooser({ icon, text, className, title, onOpenFile }
ref={ref => fileInput = ref}
className="hidden"
type="file"
onChange={e => { e.preventDefault(); if(e.target.files.length > 0) onOpenFile(e.target.files[0]); fileInput = "";}}
onChange={e => { e.preventDefault(); if(e.target.files.length > 0) onOpenFile(e.target.files[0])}}
/>
</a>
)
Expand Down

0 comments on commit 782d564

Please sign in to comment.