|
| 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 | +}); |
0 commit comments