mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-22 07:08:10 +00:00
[web] Add tests for js/components/common/FileChooser.jsx & minor fix.
This commit is contained in:
parent
9a7ac14654
commit
782d564e76
38
web/src/js/__tests__/components/common/FileChooserSpec.js
Normal file
38
web/src/js/__tests__/components/common/FileChooserSpec.js
Normal file
@ -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()
|
||||
})
|
||||
})
|
@ -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>
|
||||
`;
|
@ -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>
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user