Skip to content

Commit 53d4739

Browse files
committed
EES-5733 export menu tests
1 parent ac68552 commit 53d4739

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import ChartExportMenu from '@common/modules/charts/components/ChartExportMenu';
2+
import React, { createRef } from 'react';
3+
import { screen } from '@testing-library/react';
4+
import render from '@common-test/render';
5+
6+
describe('ChartExportMenu', () => {
7+
test('renders', () => {
8+
const ref = createRef<HTMLTableElement>();
9+
10+
render(<ChartExportMenu chartRef={ref} chartTitle="Test title" />);
11+
12+
expect(
13+
screen.getByRole('button', { name: 'Export options for Test title' }),
14+
).toBeInTheDocument();
15+
16+
expect(
17+
screen.queryByRole('button', { name: 'Download chart as PNG' }),
18+
).not.toBeInTheDocument();
19+
20+
expect(
21+
screen.queryByRole('button', { name: 'Copy chart to clipboard' }),
22+
).not.toBeInTheDocument();
23+
});
24+
25+
test('renders the options when expanded', async () => {
26+
const ref = createRef<HTMLTableElement>();
27+
28+
const { user } = render(
29+
<ChartExportMenu chartRef={ref} chartTitle="Test title" />,
30+
);
31+
32+
await user.click(
33+
screen.getByRole('button', { name: 'Export options for Test title' }),
34+
);
35+
36+
expect(
37+
screen.getByRole('button', { name: 'Download chart as PNG' }),
38+
).toBeInTheDocument();
39+
40+
expect(
41+
screen.getByRole('button', { name: 'Copy chart to clipboard' }),
42+
).toBeInTheDocument();
43+
});
44+
});

src/explore-education-statistics-common/src/modules/find-statistics/components/__tests__/DataBlockTabs.test.tsx

+42
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,48 @@ describe('DataBlockTabs', () => {
381381
});
382382
});
383383

384+
test('renders chart and table export menus', async () => {
385+
tableBuilderService.getDataBlockTableData.mockResolvedValue(
386+
testChartTableData,
387+
);
388+
389+
const { user } = render(
390+
<DataBlockTabs
391+
releaseVersionId="release-1"
392+
id="test-datablock"
393+
dataBlock={{
394+
...testDataBlock,
395+
charts: [testChartConfiguration],
396+
}}
397+
/>,
398+
);
399+
400+
forceVisible();
401+
402+
await waitFor(() => {
403+
expect(tableBuilderService.getDataBlockTableData).toBeCalledWith(
404+
'release-1',
405+
'block-1-parent',
406+
);
407+
408+
expect(screen.getAllByRole('tab')).toHaveLength(2);
409+
});
410+
411+
expect(
412+
screen.getByRole('button', {
413+
name: 'Export options for Aggregated results chart',
414+
}),
415+
).toBeInTheDocument();
416+
417+
await user.click(screen.getByRole('tab', { name: /Table/ }));
418+
419+
expect(
420+
screen.getByRole('button', {
421+
name: 'Export options for table',
422+
}),
423+
).toBeInTheDocument();
424+
});
425+
384426
test('selecting data set with boundaryLevel retrieves and renders new map polygons', async () => {
385427
tableBuilderService.getDataBlockTableData.mockResolvedValue(
386428
testMapTableData,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import TableExportMenu from '@common/modules/find-statistics/components/TableExportMenu';
2+
import React, { createRef } from 'react';
3+
import { screen, waitFor } from '@testing-library/react';
4+
import render from '@common-test/render';
5+
import { WorkBook, writeFile } from 'xlsx';
6+
7+
describe('TableExportMenu', () => {
8+
test('renders', () => {
9+
const ref = createRef<HTMLTableElement>();
10+
11+
render(
12+
<TableExportMenu
13+
tableRef={ref}
14+
title="Test title"
15+
onCsvDownload={jest.fn()}
16+
/>,
17+
);
18+
19+
expect(
20+
screen.getByRole('button', { name: 'Export options for Test title' }),
21+
).toBeInTheDocument();
22+
23+
expect(
24+
screen.queryByRole('button', { name: 'Download table as ODS' }),
25+
).not.toBeInTheDocument();
26+
27+
expect(
28+
screen.queryByRole('button', { name: 'Download table as CSV' }),
29+
).not.toBeInTheDocument();
30+
31+
expect(
32+
screen.queryByRole('button', { name: 'Copy table to clipboard' }),
33+
).not.toBeInTheDocument();
34+
});
35+
36+
test('renders the options when expanded', async () => {
37+
const ref = createRef<HTMLTableElement>();
38+
39+
const { user } = render(
40+
<TableExportMenu
41+
tableRef={ref}
42+
title="Test title"
43+
onCsvDownload={jest.fn()}
44+
/>,
45+
);
46+
47+
await user.click(
48+
screen.getByRole('button', { name: 'Export options for Test title' }),
49+
);
50+
51+
expect(
52+
screen.getByRole('button', { name: 'Download table as ODS' }),
53+
).toBeInTheDocument();
54+
55+
expect(
56+
screen.getByRole('button', { name: 'Download table as CSV' }),
57+
).toBeInTheDocument();
58+
59+
expect(
60+
screen.getByRole('button', { name: 'Copy table to clipboard' }),
61+
).toBeInTheDocument();
62+
});
63+
64+
test('downloads the table as an ODS file', async () => {
65+
const ref = createRef<HTMLTableElement>();
66+
67+
const { user } = render(
68+
<>
69+
<table ref={ref}>
70+
<tbody>
71+
<tr>
72+
<th />
73+
<th>Date 1</th>
74+
<th>Date 2</th>
75+
</tr>
76+
<tr>
77+
<th>Indicator</th>
78+
<td>101</td>
79+
<td>102</td>
80+
</tr>
81+
</tbody>
82+
</table>
83+
<TableExportMenu
84+
tableRef={ref}
85+
title="Test title"
86+
onCsvDownload={jest.fn()}
87+
/>
88+
</>,
89+
);
90+
91+
await user.click(
92+
screen.getByRole('button', { name: 'Export options for Test title' }),
93+
);
94+
95+
await user.click(
96+
screen.getByRole('button', { name: 'Download table as ODS' }),
97+
);
98+
99+
const mockedWriteFile = writeFile as jest.Mock;
100+
101+
await waitFor(() => {
102+
expect(mockedWriteFile).toHaveBeenCalledTimes(1);
103+
104+
const workbook = mockedWriteFile.mock.calls[0][0] as WorkBook;
105+
106+
expect(workbook.Sheets.Sheet1.A1.v).toBe('Test title');
107+
expect(workbook.Sheets.Sheet1.B3.v).toBe('Date 1');
108+
expect(workbook.Sheets.Sheet1.C3.v).toBe('Date 2');
109+
expect(workbook.Sheets.Sheet1.A4.v).toBe('Indicator');
110+
expect(workbook.Sheets.Sheet1.B4.v).toBe('101');
111+
expect(workbook.Sheets.Sheet1.C4.v).toBe('102');
112+
113+
expect(mockedWriteFile.mock.calls[0][1]).toBe('table.ods');
114+
});
115+
});
116+
117+
test('calls the CSV download handler', async () => {
118+
const ref = createRef<HTMLTableElement>();
119+
const handleCsvDownload = jest.fn();
120+
121+
const { user } = render(
122+
<TableExportMenu
123+
tableRef={ref}
124+
title="Test title"
125+
onCsvDownload={handleCsvDownload}
126+
/>,
127+
);
128+
129+
await user.click(
130+
screen.getByRole('button', { name: 'Export options for Test title' }),
131+
);
132+
133+
expect(handleCsvDownload).not.toHaveBeenCalled();
134+
135+
await user.click(
136+
screen.getByRole('button', { name: 'Download table as CSV' }),
137+
);
138+
139+
expect(handleCsvDownload).toHaveBeenCalledTimes(1);
140+
});
141+
});

0 commit comments

Comments
 (0)