|
| 1 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react' |
| 2 | +import { CSSTable } from '../CSSTable' |
| 3 | + |
| 4 | +jest.mock('@patternfly/react-tokens/dist/esm/componentIndex', () => ({ |
| 5 | + test: { |
| 6 | + '.test-selector': { |
| 7 | + '--test-property': { |
| 8 | + name: 'test-property', |
| 9 | + value: 'test-value', |
| 10 | + }, |
| 11 | + '--another-property': { |
| 12 | + name: 'another-property', |
| 13 | + value: '#123456', |
| 14 | + }, |
| 15 | + }, |
| 16 | + '.another-selector': { |
| 17 | + '--list-property': { |
| 18 | + name: 'list-property', |
| 19 | + value: 'list-item-1', |
| 20 | + values: ['list-item-1', 'list-item-2'], |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + 'pf-v6-empty': {}, |
| 25 | +})) |
| 26 | + |
| 27 | +describe('<CSSTable />', () => { |
| 28 | + it('renders without crashing', () => { |
| 29 | + render(<CSSTable cssPrefix="pf-v6-test" />) |
| 30 | + expect( |
| 31 | + screen.getByRole('grid', { |
| 32 | + name: /CSS Variables prefixed with pf-v6-test/i, |
| 33 | + }), |
| 34 | + ).toBeInTheDocument() |
| 35 | + }) |
| 36 | + |
| 37 | + it('renders the correct table headers when hideSelectorColumn is false (default)', () => { |
| 38 | + render(<CSSTable cssPrefix="pf-v6-test" />) |
| 39 | + expect(screen.getByText('Selector')).toBeInTheDocument() |
| 40 | + expect(screen.getByText('Variable')).toBeInTheDocument() |
| 41 | + expect(screen.getByText('Value')).toBeInTheDocument() |
| 42 | + }) |
| 43 | + |
| 44 | + it('renders the correct table headers when hideSelectorColumn is true', () => { |
| 45 | + render(<CSSTable cssPrefix="pf-v6-test" hideSelectorColumn />) |
| 46 | + expect(screen.queryByText('Selector')).not.toBeInTheDocument() |
| 47 | + expect(screen.getByText('Variable')).toBeInTheDocument() |
| 48 | + expect(screen.getByText('Value')).toBeInTheDocument() |
| 49 | + }) |
| 50 | + |
| 51 | + it('renders the correct data rows', () => { |
| 52 | + render(<CSSTable cssPrefix="pf-v6-test" />) |
| 53 | + expect( |
| 54 | + screen.getByRole('row', { |
| 55 | + name: '.test-selector test-property test-value', |
| 56 | + }), |
| 57 | + ).toBeInTheDocument() |
| 58 | + expect( |
| 59 | + screen.getByRole('row', { |
| 60 | + name: '.test-selector another-property (In light theme) #123456', |
| 61 | + }), |
| 62 | + ).toBeInTheDocument() |
| 63 | + expect( |
| 64 | + screen.getByRole('row', { |
| 65 | + name: 'Details .another-selector list-property list-item-1', |
| 66 | + }), |
| 67 | + ).toBeInTheDocument() |
| 68 | + }) |
| 69 | + |
| 70 | + it('renders the "Prefixed with" header when autoLinkHeader is true', () => { |
| 71 | + render(<CSSTable cssPrefix="pf-v6-test" autoLinkHeader />) |
| 72 | + expect( |
| 73 | + screen.getByRole('heading', { |
| 74 | + name: /Prefixed with 'pf-v6-test'/i, |
| 75 | + level: 3, |
| 76 | + }), |
| 77 | + ).toBeInTheDocument() |
| 78 | + }) |
| 79 | + |
| 80 | + it('does not render the "Prefixed with" header when autoLinkHeader is false (default)', () => { |
| 81 | + render(<CSSTable cssPrefix="pf-v6-test" />) |
| 82 | + expect( |
| 83 | + screen.queryByRole('heading', { |
| 84 | + name: /Prefixed with 'pf-v6-test'/i, |
| 85 | + level: 3, |
| 86 | + }), |
| 87 | + ).not.toBeInTheDocument() |
| 88 | + }) |
| 89 | + |
| 90 | + it('filters rows based on search input', async () => { |
| 91 | + render(<CSSTable cssPrefix="pf-v6-test" debounceLength={0} />) |
| 92 | + const searchInput = screen.getByPlaceholderText('Filter CSS Variables') |
| 93 | + |
| 94 | + expect( |
| 95 | + screen.getByRole('row', { |
| 96 | + name: '.test-selector test-property test-value', |
| 97 | + }), |
| 98 | + ).toBeInTheDocument() |
| 99 | + expect( |
| 100 | + screen.getByRole('row', { |
| 101 | + name: 'Details .another-selector list-property list-item-1', |
| 102 | + }), |
| 103 | + ).toBeInTheDocument() |
| 104 | + fireEvent.change(searchInput, { target: { value: 'test' } }) |
| 105 | + await waitFor(() => { |
| 106 | + // row doesn't seem to work here for whatever reason |
| 107 | + expect(screen.getAllByText('.test-selector')).toHaveLength(2) |
| 108 | + expect(screen.getByText('test-property')).toBeInTheDocument() |
| 109 | + expect(screen.queryByText('.another-selector')).not.toBeInTheDocument() |
| 110 | + expect(screen.queryByText('list-property')).not.toBeInTheDocument() |
| 111 | + }) |
| 112 | + |
| 113 | + fireEvent.change(searchInput, { target: { value: 'list' } }) |
| 114 | + await waitFor(() => { |
| 115 | + expect(screen.queryByText('.test-selector')).not.toBeInTheDocument() |
| 116 | + expect(screen.queryByText('test-property')).not.toBeInTheDocument() |
| 117 | + expect(screen.getByText('.another-selector')).toBeInTheDocument() |
| 118 | + expect(screen.getAllByText('list-property')).toHaveLength(2) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + it('handles the case where the cssPrefix does not exist in tokensModule', () => { |
| 123 | + render(<CSSTable cssPrefix="pf-v6-nonexistent" />) |
| 124 | + expect(screen.queryByRole('grid')).toBeInTheDocument() // Table should still render, but be empty |
| 125 | + expect(screen.queryByText('.test-selector')).not.toBeInTheDocument() |
| 126 | + }) |
| 127 | + |
| 128 | + it('renders correctly when hideSelectorColumn is true and there are rows', () => { |
| 129 | + render(<CSSTable cssPrefix="pf-v6-test" hideSelectorColumn />) |
| 130 | + expect(screen.queryByText('.test-selector')).not.toBeInTheDocument() |
| 131 | + expect( |
| 132 | + screen.getByRole('row', { |
| 133 | + name: 'test-property', |
| 134 | + }), |
| 135 | + ).toBeInTheDocument() |
| 136 | + }) |
| 137 | +}) |
0 commit comments