Skip to content

Commit 41c6a96

Browse files
committed
test: add unit tests for CSV export
1 parent 0ce1b45 commit 41c6a96

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

app/src/__mocks__/file-saver.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const saveAs = jest.fn();
2+
3+
export default { saveAs };

app/src/__tests__/components/history/HistoryPage.spec.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { fireEvent } from '@testing-library/react';
3+
import { saveAs } from 'file-saver';
34
import { renderWithProviders } from 'util/tests';
45
import { createStore, Store } from 'store';
56
import HistoryPage from 'components/history/HistoryPage';
@@ -50,4 +51,10 @@ describe('HistoryPage', () => {
5051
fireEvent.click(getByText('arrow-left.svg'));
5152
expect(store.uiStore.page).toEqual('loop');
5253
});
54+
55+
it('should export channels', () => {
56+
const { getByText } = render();
57+
fireEvent.click(getByText('download.svg'));
58+
expect(saveAs).toBeCalledWith(expect.any(Blob), 'swaps.csv');
59+
});
5360
});

app/src/__tests__/components/loop/LoopPage.spec.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { SwapStatus } from 'types/generated/loop_pb';
33
import { grpc } from '@improbable-eng/grpc-web';
44
import { fireEvent, waitFor } from '@testing-library/react';
5+
import { saveAs } from 'file-saver';
56
import { formatSats } from 'util/formatters';
67
import { renderWithProviders } from 'util/tests';
78
import { loopListSwaps } from 'util/tests/sampleData';
@@ -57,6 +58,17 @@ describe('LoopPage component', () => {
5758
expect(await findByText('525,000 sats')).toBeInTheDocument();
5859
});
5960

61+
it('should display the export icon', () => {
62+
const { getByText } = render();
63+
expect(getByText('download.svg')).toBeInTheDocument();
64+
});
65+
66+
it('should export channels', () => {
67+
const { getByText } = render();
68+
fireEvent.click(getByText('download.svg'));
69+
expect(saveAs).toBeCalledWith(expect.any(Blob), 'channels.csv');
70+
});
71+
6072
describe('Swap Process', () => {
6173
it('should display actions bar when Loop button is clicked', () => {
6274
const { getByText } = render();

app/src/__tests__/util/csv.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { saveAs } from 'file-saver';
2+
import CsvExporter from 'util/csv';
3+
import { loopListSwaps } from 'util/tests/sampleData';
4+
import { Swap } from 'store/models';
5+
6+
describe('csv Util', () => {
7+
const csv = new CsvExporter();
8+
const swaps = [new Swap(loopListSwaps.swapsList[0])];
9+
10+
it('should export using the .csv extension', () => {
11+
csv.export('swaps', Swap.csvColumns, swaps);
12+
expect(saveAs).toBeCalledWith(expect.any(Blob), 'swaps.csv');
13+
});
14+
15+
it('should convert swap data to the correct string', () => {
16+
const actual = csv.convert(Swap.csvColumns, swaps);
17+
const expected = [
18+
'"Swap ID","Type","Amount","Status","Created On","Updated On"',
19+
'"f4eb118383c2b09d8c7289ce21c25900cfb4545d46c47ed23a31ad2aa57ce830","Loop Out","500000","Failed","4/8/2020 7:59:13 PM","4/8/2020 10:12:49 PM"',
20+
].join('\n');
21+
expect(actual).toEqual(expected);
22+
});
23+
});

0 commit comments

Comments
 (0)