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);
});
exportTableToCSV
is defined, thanks. (Or at least include the function definitions forencode
anddownloadCSV
, how those functions are defined determines how you mock them) – Brian Adams