I have the following function in a React component:
JavaScript
x
5
1
onUploadStart(file, xhr, formData) {
2
formData.append('filename', file.name);
3
formData.append('mimeType', file.type);
4
}
5
This is my test that at least gets the spy to be called:
JavaScript
1
9
1
const formData = { append: jest.fn() };
2
const file = { name: 'someFileName', type: 'someMimeType' };
3
eventHandlers.onUploadStart(file, null, formData);
4
5
expect(formData.append).toHaveBeenCalledWith(
6
['mimeType', 'someMimeType'],
7
['fileName', 'someFileName']
8
);
9
However, the assertion is not working:
JavaScript
1
5
1
Expected mock function to have been called with:
2
[["mimeType", "someMimeType"], ["fileName", "someFileName"]]
3
But it was called with:
4
["mimeType", "someMimeType"], ["filename", "someFileName"]
5
What is the right way to use toHaveBeenCalledWith
?
Advertisement
Answer
I was able mock multiple calls and check the arguments this way:
JavaScript
1
5
1
expect(mockFn.mock.calls).toEqual([
2
[arg1, arg2, ], // First call
3
[arg1, arg2, ] // Second call
4
]);
5
where mockFn
is your mocked function name.