fireEvent.change does not trigger onChange listener
The problem is that spyOn
is called after the event is fired.
Change the test code to:
test('when user changes the file input, uploadImage function is called once', () => {
render(<NavBar />);
const mock = jest.spyOn(uploadImage, 'uploadImage');
const fileInput = screen.getByLabelText(/Upload/i);
fireEvent.change(fileInput, {
target: {
files: [new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' })],
},
});
expect(mock).toBeCalled();
});
so that the mock function is created and ready before it gets called.