Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add support for input file on fireEvent.update #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
afontcu merged 9 commits into testing-library:master from JeromeDeLeon:input-file-test-fail
Nov 24, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/__tests__/fire-event.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ const eventTypes = [
elementType: 'div',
},
]

const mockFile = ({
name,
size = 0,
type = 'text/plain',
lastModified = new Date()
}) => {
const blob = new Blob(['0'.repeat(size)], { type });
blob.lastModifiedDate = lastModified;
return new File([blob], name);
};

beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {})
})
Expand Down Expand Up @@ -215,6 +227,31 @@ test('fireEvent.update does not trigger warning messages', async () => {
expect(console.warn).not.toHaveBeenCalled()
})

test('fireEvent.update should not crash with input file', async () => {
Copy link
Contributor Author

@JeromeDeLeon JeromeDeLeon Nov 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test example not enough? @afontcu

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is! But as mentioned, I wouldn't make fireEvent.update() smarter when it comes to type="file" inputs. I would just trigger the proper event, and then add a test showcasing how to handle files as in React Testing Library (and probably also a comment suggesting userEvent to do so:

test('fireEvent.update should not crash with input file', async () => {
 const {getByTestId} = render({
 template: `<input type="file" data-testid="test-update" />`
 })
 const file = new File(['(⌐しろいしかく_しろいしかく)'], 'chucknorris.png', { type: 'image/png' })
 const inputEl = getByTestId('test-update')
 Object.defineProperty(inputEl, 'files', {
 value: [file],
 })
 await fireEvent.update(inputEl)
 expect(console.warn).not.toHaveBeenCalled()
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a suggestion with the change I mention above 👍

const inputSpy = jest.fn();
const changeSpy = jest.fn();

const {getByTestId} = render({
render(h) {
return h('input', {
on: {
input: inputSpy,
change: changeSpy
},
attrs: {
type: 'file',
'data-testid': 'test-update',
},
})
}
})

// should expect a array of list since it's a fileList
await fireEvent.update(getByTestId('test-update'), [mockFile({ name: 'random.txt', size: 524288 })])

expect(console.warn).not.toHaveBeenCalled()
})

test('fireEvent.update does not crash if non-input element is passed in', async () => {
const {getByText} = render({
template: `<div>Hi</div>`,
Expand Down

AltStyle によって変換されたページ (->オリジナル) /