Skip to content

Commit 50f8e87

Browse files
committed
add tests
1 parent 5feea95 commit 50f8e87

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as React from 'react';
2+
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
3+
import { Basic, FewColumns, LabelTypes } from './ColumnsButton.stories';
4+
5+
describe('ColumnsButton', () => {
6+
it('should render one row per column unless they are hidden', async () => {
7+
render(<Basic />);
8+
fireEvent.click(await screen.findByText('ra.action.select_columns'));
9+
await screen.findByLabelText('c_0');
10+
await screen.findByLabelText('c_1');
11+
await screen.findByLabelText('c_2');
12+
await screen.findByLabelText('c_3');
13+
await screen.findByLabelText('c_4');
14+
await screen.findByLabelText('c_5');
15+
// await screen.findByLabelText('c_6'); // hidden
16+
await screen.findByLabelText('c_7');
17+
});
18+
it('should not render the filter input when there are too few columns', async () => {
19+
render(<FewColumns />);
20+
fireEvent.click(await screen.findByText('ra.action.select_columns'));
21+
await screen.findByLabelText('c_0');
22+
expect(screen.queryByText('ra.action.search_columns')).toBeNull();
23+
});
24+
it('should render a filter input when there are many columns', async () => {
25+
render(<LabelTypes />);
26+
fireEvent.click(await screen.findByText('ra.action.select_columns'));
27+
await screen.findByLabelText('resources.test.fields.col0');
28+
expect(
29+
screen
30+
.getByRole('menu')
31+
.querySelectorAll('li:not(.columns-selector-actions)')
32+
).toHaveLength(7);
33+
// Typing a filter
34+
fireEvent.change(
35+
screen.getByPlaceholderText('ra.action.search_columns'),
36+
{
37+
// filter should be case and diacritics insensitive
38+
target: { value: 'DiA' },
39+
}
40+
);
41+
await waitFor(() => {
42+
expect(
43+
screen
44+
.getByRole('menu')
45+
.querySelectorAll('li:not(.columns-selector-actions)')
46+
).toHaveLength(1);
47+
});
48+
screen.getByLabelText('Téstïng diàcritics');
49+
// Clear the filter
50+
fireEvent.click(screen.getByLabelText('ra.action.clear_input_value'));
51+
await waitFor(() => {
52+
expect(
53+
screen
54+
.getByRole('menu')
55+
.querySelectorAll('li:not(.columns-selector-actions)')
56+
).toHaveLength(7);
57+
});
58+
});
59+
});

packages/ra-ui-materialui/src/list/datatable/ColumnsSelector.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ export const ColumnsSelector = ({ children }: ColumnsSelectorProps) => {
5858
};
5959
}, [elementId, container]);
6060

61-
const [columnFilter, setColumnFilter] = React.useState<
62-
string | undefined
63-
>();
61+
const [columnFilter, setColumnFilter] = React.useState<string>('');
6462

6563
if (!container) return null;
6664

0 commit comments

Comments
 (0)