|
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'); |
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { SistentThemeProvider } from '../theme'; |
| 4 | + |
| 5 | +jest.mock('react-markdown', () => ({ |
| 6 | + __esModule: true, |
| 7 | + default: ({ children }: { children: React.ReactNode }) => <div>{children}</div> |
| 8 | +})); |
| 9 | + |
| 10 | +jest.mock('remark-gfm', () => ({ |
| 11 | + __esModule: true, |
| 12 | + default: () => {} |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('rehype-raw', () => ({ |
| 16 | + __esModule: true, |
| 17 | + default: () => {} |
| 18 | +})); |
| 19 | + |
| 20 | +jest.mock('@sistent/mui-datatables', () => { |
| 21 | + const MockMUIDataTable = ({ |
| 22 | + data, |
| 23 | + columns |
| 24 | + }: { |
| 25 | + data: string[][]; |
| 26 | + columns: { name: string; label: string }[]; |
| 27 | + }) => ( |
| 28 | + <table data-testid="mui-datatable"> |
| 29 | + <thead> |
| 30 | + <tr> |
| 31 | + {columns.map((col) => ( |
| 32 | + <th key={col.name}>{col.label}</th> |
| 33 | + ))} |
| 34 | + </tr> |
| 35 | + </thead> |
| 36 | + <tbody> |
| 37 | + {data.map((row, rowIndex) => ( |
| 38 | + <tr key={rowIndex}> |
| 39 | + {row.map((cell, cellIndex) => ( |
| 40 | + <td key={cellIndex}>{cell}</td> |
| 41 | + ))} |
| 42 | + </tr> |
| 43 | + ))} |
| 44 | + </tbody> |
| 45 | + </table> |
| 46 | + ); |
10 | 47 | return { |
11 | 48 | __esModule: true, |
12 | | - default: (props: unknown) => React.createElement('div', props) |
| 49 | + default: MockMUIDataTable |
13 | 50 | }; |
14 | 51 | }); |
15 | 52 |
|
16 | | -jest.mock('rehype-raw', () => () => null); |
17 | | -jest.mock('remark-gfm', () => () => null); |
| 53 | +// eslint-disable-next-line import/first |
| 54 | +import ResponsiveDataTable from '../custom/ResponsiveDataTable'; |
18 | 55 |
|
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 | | -}); |
| 56 | +const mockColumns = [ |
| 57 | + { name: 'id', label: 'ID', options: { display: true } }, |
| 58 | + { name: 'name', label: 'Name', options: { display: true } } |
| 59 | +]; |
27 | 60 |
|
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 | | -}); |
| 61 | +const mockData = [ |
| 62 | + ['1', 'Test Item 1'], |
| 63 | + ['2', 'Test Item 2'] |
| 64 | +]; |
39 | 65 |
|
40 | | -describe('ResponsiveDataTable', () => { |
41 | | - beforeEach(() => { |
42 | | - muiDataTableMock.mockClear(); |
43 | | - }); |
| 66 | +const mockColumnVisibility = { |
| 67 | + id: true, |
| 68 | + name: true |
| 69 | +}; |
44 | 70 |
|
45 | | - it('applies default table options', () => { |
46 | | - const columns: MUIDataTableColumn[] = [{ name: 'id', label: 'ID' }]; |
| 71 | +const renderWithTheme = (ui: React.ReactElement) => { |
| 72 | + return render(<SistentThemeProvider>{ui}</SistentThemeProvider>); |
| 73 | +}; |
47 | 74 |
|
48 | | - render( |
| 75 | +describe('ResponsiveDataTable', () => { |
| 76 | + it('renders without errors', () => { |
| 77 | + renderWithTheme( |
49 | 78 | <ResponsiveDataTable |
50 | | - columns={columns} |
51 | | - data={[]} |
52 | | - tableCols={columns} |
53 | | - columnVisibility={{}} |
| 79 | + data={mockData} |
| 80 | + columns={mockColumns} |
| 81 | + tableCols={mockColumns} |
| 82 | + columnVisibility={mockColumnVisibility} |
54 | 83 | /> |
55 | 84 | ); |
| 85 | + }); |
56 | 86 |
|
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 | | - }) |
| 87 | + it('renders table with data', () => { |
| 88 | + renderWithTheme( |
| 89 | + <ResponsiveDataTable |
| 90 | + data={mockData} |
| 91 | + columns={mockColumns} |
| 92 | + tableCols={mockColumns} |
| 93 | + columnVisibility={mockColumnVisibility} |
| 94 | + /> |
72 | 95 | ); |
| 96 | + expect(screen.getByText('Test Item 1')).toBeTruthy(); |
| 97 | + expect(screen.getByText('Test Item 2')).toBeTruthy(); |
73 | 98 | }); |
74 | 99 |
|
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( |
| 100 | + it('renders column headers', () => { |
| 101 | + renderWithTheme( |
80 | 102 | <ResponsiveDataTable |
81 | | - columns={columns} |
82 | | - data={[]} |
83 | | - tableCols={columns} |
84 | | - columnVisibility={{ name: true }} |
85 | | - updateCols={updateCols} |
| 103 | + data={mockData} |
| 104 | + columns={mockColumns} |
| 105 | + tableCols={mockColumns} |
| 106 | + columnVisibility={mockColumnVisibility} |
86 | 107 | /> |
87 | 108 | ); |
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); |
| 109 | + expect(screen.getByText('ID')).toBeTruthy(); |
| 110 | + expect(screen.getByText('Name')).toBeTruthy(); |
100 | 111 | }); |
101 | 112 |
|
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( |
| 113 | + it('accepts custom rowsPerPageOptions', () => { |
| 114 | + renderWithTheme( |
107 | 115 | <ResponsiveDataTable |
108 | | - columns={columns} |
109 | | - data={[]} |
110 | | - tableCols={columns} |
111 | | - columnVisibility={{ updated_at: true }} |
112 | | - updateCols={updateCols} |
| 116 | + data={mockData} |
| 117 | + columns={mockColumns} |
| 118 | + tableCols={mockColumns} |
| 119 | + columnVisibility={mockColumnVisibility} |
| 120 | + rowsPerPageOptions={[5, 10, 20]} |
113 | 121 | /> |
114 | 122 | ); |
| 123 | + }); |
115 | 124 |
|
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(); |
| 125 | + it('calls updateCols when provided', () => { |
| 126 | + const mockUpdateCols = jest.fn(); |
| 127 | + renderWithTheme( |
| 128 | + <ResponsiveDataTable |
| 129 | + data={mockData} |
| 130 | + columns={mockColumns} |
| 131 | + tableCols={mockColumns} |
| 132 | + columnVisibility={mockColumnVisibility} |
| 133 | + updateCols={mockUpdateCols} |
| 134 | + /> |
| 135 | + ); |
| 136 | + expect(mockUpdateCols).toHaveBeenCalled(); |
123 | 137 | }); |
124 | 138 | }); |
0 commit comments