|
| 1 | +import React, { act } from 'react'; |
| 2 | +import { render, waitFor } from '@testing-library/react'; |
| 3 | +import type { MUIDataTableColumn, MUIDataTableOptions, MUIDataTableProps } from 'mui-datatables'; |
| 4 | +import ResponsiveDataTable from '../custom/ResponsiveDataTable'; |
| 5 | + |
| 6 | +const muiDataTableMock = jest.fn(); |
| 7 | + |
| 8 | +jest.mock('react-markdown', () => { |
| 9 | + const React = require('react'); |
| 10 | + return { |
| 11 | + __esModule: true, |
| 12 | + default: (props: unknown) => React.createElement('div', props) |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +jest.mock('rehype-raw', () => () => null); |
| 17 | +jest.mock('remark-gfm', () => () => null); |
| 18 | + |
| 19 | +jest.mock('../custom', () => { |
| 20 | + const React = require('react'); |
| 21 | + return { |
| 22 | + __esModule: true, |
| 23 | + CustomTooltip: ({ children }: { children: React.ReactNode }) => |
| 24 | + React.createElement('div', null, children) |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +jest.mock('mui-datatables', () => { |
| 29 | + const React = require('react'); |
| 30 | + return { |
| 31 | + __esModule: true, |
| 32 | + default: (props: MUIDataTableProps) => { |
| 33 | + muiDataTableMock(props); |
| 34 | + return React.createElement('div', { 'data-testid': 'mui-datatables' }); |
| 35 | + }, |
| 36 | + MUIDataTableColumn: {} |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +describe('ResponsiveDataTable', () => { |
| 41 | + beforeEach(() => { |
| 42 | + muiDataTableMock.mockClear(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('applies default table options', () => { |
| 46 | + const columns: MUIDataTableColumn[] = [{ name: 'id', label: 'ID' }]; |
| 47 | + |
| 48 | + render( |
| 49 | + <ResponsiveDataTable |
| 50 | + columns={columns} |
| 51 | + data={[]} |
| 52 | + tableCols={columns} |
| 53 | + columnVisibility={{}} |
| 54 | + /> |
| 55 | + ); |
| 56 | + |
| 57 | + expect(muiDataTableMock).toHaveBeenCalled(); |
| 58 | + const props = muiDataTableMock.mock.calls[0][0] as MUIDataTableProps; |
| 59 | + const options = props.options as MUIDataTableOptions; |
| 60 | + |
| 61 | + expect(options).toEqual( |
| 62 | + expect.objectContaining({ |
| 63 | + print: false, |
| 64 | + download: false, |
| 65 | + search: false, |
| 66 | + filter: false, |
| 67 | + viewColumns: false, |
| 68 | + rowsPerPageOptions: [10, 25, 50, 100], |
| 69 | + elevation: 0, |
| 70 | + enableNestedDataAccess: '.' |
| 71 | + }) |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('updates column visibility via onViewColumnsChange', async () => { |
| 76 | + const columns: MUIDataTableColumn[] = [{ name: 'name', label: 'Name', options: { display: true } }]; |
| 77 | + const updateCols = jest.fn(); |
| 78 | + |
| 79 | + render( |
| 80 | + <ResponsiveDataTable |
| 81 | + columns={columns} |
| 82 | + data={[]} |
| 83 | + tableCols={columns} |
| 84 | + columnVisibility={{ name: true }} |
| 85 | + updateCols={updateCols} |
| 86 | + /> |
| 87 | + ); |
| 88 | + |
| 89 | + await waitFor(() => expect(updateCols).toHaveBeenCalled()); |
| 90 | + |
| 91 | + const props = muiDataTableMock.mock.calls[0][0] as MUIDataTableProps; |
| 92 | + const options = props.options as MUIDataTableOptions; |
| 93 | + |
| 94 | + act(() => { |
| 95 | + options.onViewColumnsChange?.('name', 'remove'); |
| 96 | + }); |
| 97 | + |
| 98 | + expect(columns[0].options?.display).toBe(false); |
| 99 | + expect(updateCols).toHaveBeenCalledTimes(2); |
| 100 | + }); |
| 101 | + |
| 102 | + it('attaches date renderer for known date columns', async () => { |
| 103 | + const columns: MUIDataTableColumn[] = [{ name: 'updated_at', label: 'Updated At', options: {} }]; |
| 104 | + const updateCols = jest.fn(); |
| 105 | + |
| 106 | + render( |
| 107 | + <ResponsiveDataTable |
| 108 | + columns={columns} |
| 109 | + data={[]} |
| 110 | + tableCols={columns} |
| 111 | + columnVisibility={{ updated_at: true }} |
| 112 | + updateCols={updateCols} |
| 113 | + /> |
| 114 | + ); |
| 115 | + |
| 116 | + await waitFor(() => expect(updateCols).toHaveBeenCalled()); |
| 117 | + |
| 118 | + const renderer = columns[0].options?.customBodyRender; |
| 119 | + expect(typeof renderer).toBe('function'); |
| 120 | + |
| 121 | + const element = renderer && renderer('2024-01-01T00:00:00Z'); |
| 122 | + expect(element).toBeTruthy(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments