How to check multiple arguments for multiple spyware calls?

I have the following function in the React component:

onUploadStart(file, xhr, formData) { formData.append('filename', file.name); formData.append('mimeType', file.type); } 

This is my test, which at least causes a spy:

 const formData = { append: jest.fn() }; const file = { name: 'someFileName', type: 'someMimeType' }; eventHandlers.onUploadStart(file, null, formData); expect(formData.append).toHaveBeenCalledWith( ['mimeType', 'someMimeType'], ['fileName', 'someFileName'] ); 

However, the statement does not work:

 Expected mock function to have been called with: [["mimeType", "someMimeType"], ["fileName", "someFileName"]] But it was called with: ["mimeType", "someMimeType"], ["filename", "someFileName"] 

How to use toHaveBeenCalledWith ?

+16
source share
6 answers

Signature .toHaveBeenCalledWith(arg1, arg2, ...) , where arg1, arg2, ... means one call ( see ).

If you want to test multiple calls, just expect this several times.

Unfortunately, I have not yet found a way to check the order of several calls.

+5
source

I was able to make several calls and check the arguments as follows:

 expect(mockFn.mock.calls).toEqual([ [arg1, arg2, ...], // First call [arg1, arg2, ...] // Second call ]); 

where mockFn is your product function name.

+47
source

Starting with .toHaveBeenNthCalledWith(nthCall, arg1, arg2,....) 23.0, there is .toHaveBeenNthCalledWith(nthCall, arg1, arg2,....) https://facebook.imtqy.com/jest/docs/en/expect .html # tohavebeennthcalledwithnthcall-arg1-arg2-

+5
source

You can also check toHaveBeenCalledWith and test several times for each expected combination of parameters.

The example below checks that GA has been called three times, including three required plugins.

  describe("requireDefaultGoogleAnalyticsPlugins", () => { it("requires defualt plugins", () => { requireDefaultGoogleAnalyticsPlugins(TRACKER); expect(GoogleAnalytics.analytics).toHaveBeenCalledTimes(3); expect(GoogleAnalytics.analytics).toHaveBeenCalledWith( '${TRACKER}.require', "linkid", "linkid.js" ); expect(GoogleAnalytics.analytics).toHaveBeenCalledWith( '${TRACKER}.require', "displayfeatures" ); expect(GoogleAnalytics.analytics).toHaveBeenCalledWith( '${TRACKER}.require', "ec" ); }); }); 

In the case of OP, you can check this with

 expect(formData.append).toHaveBeenCalledWith('mimeType', 'someMimeType'); expect(formData.append).toHaveBeenCalledWith('fileName', 'someFileName'); 
+1
source

This worked for me ... when the page is loaded the default search is performed ... interaction with the user, and another search is performed by clicking on the search ... is required to verify that the search process correctly increased the search values ​​...

 let model = { addressLine1: null, addressLine2: null, city: null, country: "US"}; let caModel = { ...model, country: "CA" }; const searchSpy = props.patientActions.searchPatient; expect(searchSpy.mock.calls).toEqual([[{ ...model }], [{ ...caModel }]]); 
0
source

Another solution based on Andy. Select the call you want and check the value of the arguments. In this example, the first call is selected:

 expect(mockFn.mock.calls[0][0]).toEqual('first argument'); expect(mockFn.mock.calls[0][1]).toEqual('second argument'); 
0
source

Source: https://habr.com/ru/post/1258132/


All Articles