|
| 1 | +import { VIS_TYPE_PIVOT_TABLE, VIS_TYPE_OUTLIER_TABLE } from '@dhis2/analytics' |
| 2 | +import { useConfig } from '@dhis2/app-runtime' |
| 3 | +import { render, fireEvent } from '@testing-library/react' |
| 4 | +import React from 'react' |
| 5 | +import { DOWNLOAD_TYPE_PLAIN, ID_SCHEME_UID } from '../constants.js' |
| 6 | +import { DownloadMenu } from '../DownloadMenu.js' |
| 7 | + |
| 8 | +jest.mock('@dhis2/app-runtime', () => ({ |
| 9 | + useConfig: jest.fn(() => ({ |
| 10 | + serverVersion: { minor: 42 }, |
| 11 | + })), |
| 12 | + useDhis2ConnectionStatus: jest.fn(() => ({ |
| 13 | + isDisconnected: false, |
| 14 | + })), |
| 15 | +})) |
| 16 | + |
| 17 | +describe('DownloadMenu component', () => { |
| 18 | + const downloadDataFn = jest.fn() |
| 19 | + const downloadImageFn = jest.fn() |
| 20 | + |
| 21 | + it('renders the correct menu items for Pivot Table download', () => { |
| 22 | + const { getByText } = render( |
| 23 | + <DownloadMenu |
| 24 | + visType={VIS_TYPE_PIVOT_TABLE} |
| 25 | + onDownloadData={downloadDataFn} |
| 26 | + onDownloadImage={downloadImageFn} |
| 27 | + /> |
| 28 | + ) |
| 29 | + |
| 30 | + Array( |
| 31 | + 'Excel (.xlsx)', |
| 32 | + 'CSV (.csv)', |
| 33 | + 'HTML (.html)', |
| 34 | + 'JSON', |
| 35 | + 'XML', |
| 36 | + 'Microsoft Excel', |
| 37 | + 'CSV', |
| 38 | + 'Advanced' |
| 39 | + ).forEach((label) => expect(getByText(label)).toBeTruthy()) |
| 40 | + }) |
| 41 | + |
| 42 | + it('renders the correct menu items for Outlier Table download', () => { |
| 43 | + const { getByText } = render( |
| 44 | + <DownloadMenu |
| 45 | + visType={VIS_TYPE_OUTLIER_TABLE} |
| 46 | + onDownloadData={downloadDataFn} |
| 47 | + onDownloadImage={downloadImageFn} |
| 48 | + /> |
| 49 | + ) |
| 50 | + |
| 51 | + Array('JSON', 'Microsoft Excel', 'CSV').forEach((label) => |
| 52 | + expect(getByText(label)).toBeTruthy() |
| 53 | + ) |
| 54 | + }) |
| 55 | + |
| 56 | + test.each( |
| 57 | + [VIS_TYPE_PIVOT_TABLE, VIS_TYPE_OUTLIER_TABLE], |
| 58 | + 'uses the correct format for Excel in 42', |
| 59 | + async (visType) => { |
| 60 | + const { getByText, findByText } = render( |
| 61 | + <DownloadMenu |
| 62 | + visType={visType} |
| 63 | + onDownloadData={downloadDataFn} |
| 64 | + onDownloadImage={downloadImageFn} |
| 65 | + /> |
| 66 | + ) |
| 67 | + |
| 68 | + visType === VIS_TYPE_PIVOT_TABLE && |
| 69 | + expect(getByText('Excel (.xlsx)')).toBeTruthy() |
| 70 | + |
| 71 | + fireEvent.click(getByText('Microsoft Excel')) |
| 72 | + |
| 73 | + await findByText('ID') |
| 74 | + |
| 75 | + expect(getByText('ID')).toBeTruthy() |
| 76 | + |
| 77 | + fireEvent.click(getByText('ID')) |
| 78 | + |
| 79 | + expect(downloadDataFn).toHaveBeenCalledWith({ |
| 80 | + type: DOWNLOAD_TYPE_PLAIN, |
| 81 | + format: 'xlsx', |
| 82 | + idScheme: ID_SCHEME_UID, |
| 83 | + }) |
| 84 | + } |
| 85 | + ) |
| 86 | + |
| 87 | + // VERSION-TOGGLE: remove when 42 is the lowest supported version |
| 88 | + test.each( |
| 89 | + [VIS_TYPE_PIVOT_TABLE, VIS_TYPE_OUTLIER_TABLE], |
| 90 | + 'uses the correct format for Excel in 41', |
| 91 | + async (visType) => { |
| 92 | + useConfig.mockReturnValue({ serverVersion: { minor: 41 } }) |
| 93 | + |
| 94 | + const { getByText, findByText } = render( |
| 95 | + <DownloadMenu |
| 96 | + visType={visType} |
| 97 | + onDownloadData={downloadDataFn} |
| 98 | + onDownloadImage={downloadImageFn} |
| 99 | + /> |
| 100 | + ) |
| 101 | + |
| 102 | + visType === VIS_TYPE_PIVOT_TABLE && |
| 103 | + expect(getByText('Excel (.xls)')).toBeTruthy() |
| 104 | + |
| 105 | + fireEvent.click(getByText('Microsoft Excel')) |
| 106 | + |
| 107 | + await findByText('ID') |
| 108 | + |
| 109 | + expect(getByText('ID')).toBeTruthy() |
| 110 | + |
| 111 | + fireEvent.click(getByText('ID')) |
| 112 | + |
| 113 | + expect(downloadDataFn).toHaveBeenCalledWith({ |
| 114 | + type: DOWNLOAD_TYPE_PLAIN, |
| 115 | + format: 'xls', |
| 116 | + idScheme: ID_SCHEME_UID, |
| 117 | + }) |
| 118 | + } |
| 119 | + ) |
| 120 | +}) |
0 commit comments