|
| 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