1
votes

I am trying to write a unit test case for the following function:

export function exportTableToCSV(data, columns, filename) {
    const csv = encode(data, columns);
    downloadCsv(csv, filename);
}

And here the test case:

exportTableToCSV = jest.fn();
it('testing exportTableToCSV function', () => {
    const data = {
        "Linkname": "ATL-HOU",
        "Anode": "ATL:pos0/1",
        "Aloc": "ATL"
    }
    const columns = Object.keys(data);
    const filename = "data.csv";
    exportTableToCSV(data,columns,filename);
    expect(exportTableToCSV).toHaveBeenCalledWith(data,columns,filename);
}

this works but when try to add:

expect(encode).toHaveBeenCalledWith(data,columns);

it gives me an error saying it was not called. can someone please guide me what is the correct way to write testcase for above function

1
Please share the complete code file where exportTableToCSV is defined, thanks. (Or at least include the function definitions for encode and downloadCSV, how those functions are defined determines how you mock them)Brian Adams

1 Answers

0
votes

I believe exportTableToCSV which is called is not the real exportTableToCSV, it is a jest.fn(). The real exportTableToCSV will invoked encode(), but you aren't using the real one here, so encode() isn't invoked.

One solution I would try would be to import the real exportTableToCSV() and have encode() as a jest.fn(). Then, when you call exportTableToCSV(), expect encode() to have been called and you will prove that both were invoked.

Here's one way to do that:

import { exportTableToCSV } from './foo'

it('testing exportTableToCSV function', () => {
  const mockEncodeResult = 'i am a mock';
  const encode = jest.fn((data, columns) => mockEncodeResult);
  const downloadCsv = jest.fn((csv, filename) => {});

  const testData = {
    Linkname: 'ATL-HOU',
    Anode: 'ATL:pos0/1',
    Aloc: 'ATL',
  };

  const testColumns = Object.keys(testData);
  const testFilename = 'data.csv';

  exportTableToCSV(testData, testColumns, testFilename);

  expect(encode).toHaveBeenCalledWith(testData, testColumns);
  expect(downloadCsv).toHaveBeenCalledWith(mockEncodeResult, testFilename);
});

Please note: In the above example I do not mock exportTableToCSV. jest.fn() is a mock. This unit test imports and tests the real exportTableToCSV function, but it mocks encode() and downloadCsv(). Cheers!

UPDATE ---

Per your comment you state you are seeing an error about createObjectURL, but this is not in the sample code provided. Clearly, then, there is some problematic code in the module you are importing along with exportTableToCSV. The proper way to fix this is to mock and clean that problematic code, but because I want to provide a self-contained solution here, a second-best approach is to mock exportTableToCSV itself. In this case you will not test the original function, but only the mock. Remove the import statement and add the following inside the scope of the test case, after the definition of downloadCsv:

const exportTableToCSV = jest.fn((data, columns, filename) => {
  const csv = encode(data, columns);
  downloadCsv(csv, filename);
});