-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathTable.test.tsx
More file actions
224 lines (172 loc) · 8.06 KB
/
Table.test.tsx
File metadata and controls
224 lines (172 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { render, screen } from '@testing-library/react';
import { Table } from '../Table';
import { Td } from '../Td';
import { Th } from '../Th';
import { AnimationsProvider, useAnimationsConfig, useHasAnimations } from '@patternfly/react-core/dist/esm/helpers';
import styles from '@patternfly/react-styles/css/components/Table/table';
test('Renders without children', () => {
render(<Table aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).toBeInTheDocument();
});
test('Renders with children', () => {
render(
<Table aria-label="Test table">
<caption>Table caption</caption>
</Table>
);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveTextContent('Table caption');
});
test('Renders with role="treegrid" when isTreeTable is true', () => {
render(<Table isTreeTable aria-label="Test table" />);
expect(screen.getByRole('treegrid', { name: 'Test table' })).toBeInTheDocument();
});
test(`Renders with class ${styles.table} by default`, () => {
render(<Table aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.table);
});
test(`Renders with a pf-m-grid class by default`, () => {
render(<Table aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(/^pf-m-grid/);
});
test(`Renders with a pf-m-tree-view-grid class when isTreeTable is true`, () => {
render(<Table isTreeTable aria-label="Test table" />);
expect(screen.getByRole('treegrid', { name: 'Test table' })).not.toHaveClass(/^pf-m-grid/);
expect(screen.getByRole('treegrid', { name: 'Test table' })).toHaveClass(/^pf-m-tree-view-grid/);
});
test(`Does not render with class ${styles.modifiers.animateExpand} by default`, () => {
render(<Table aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand);
});
test(`Renders with class ${styles.modifiers.animateExpand} hasAnimations is true`, () => {
render(<Table hasAnimations aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.animateExpand);
});
// Animation context tests for expandable tables
test('respects AnimationsProvider context when no local hasAnimations prop for expandable table', () => {
render(
<AnimationsProvider config={{ hasAnimations: true }}>
<Table isExpandable aria-label="Test table" />
</AnimationsProvider>
);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.animateExpand);
});
test('local hasAnimations prop takes precedence over context for expandable table', () => {
render(
<AnimationsProvider config={{ hasAnimations: true }}>
<Table isExpandable hasAnimations={false} aria-label="Test table" />
</AnimationsProvider>
);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand);
});
test('works without AnimationsProvider (backward compatibility) for expandable table', () => {
render(<Table isExpandable aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand);
});
test('Matches snapshot without children', () => {
const { asFragment } = render(<Table />);
expect(asFragment()).toMatchSnapshot();
});
test('Renders expandable toggle button with pf-m-small class when variant is compact', () => {
render(
<Table variant="compact" isExpandable aria-label="Test table">
<tbody>
<tr>
<Td
expand={{
rowIndex: 0,
isExpanded: false,
onToggle: () => {}
}}
/>
<Td>Test content</Td>
</tr>
</tbody>
</Table>
);
const toggleButton = screen.getByRole('button', { name: 'Details' });
expect(toggleButton).toHaveClass('pf-m-small');
});
test('Renders expandable toggle button in Th with pf-m-small class when variant is compact', () => {
render(
<Table variant="compact" isExpandable aria-label="Test table">
<thead>
<tr>
<Th
expand={{
areAllExpanded: false,
collapseAllAriaLabel: 'Expand all',
onToggle: () => {}
}}
aria-label="Row expansion"
/>
<Th>Name</Th>
</tr>
</thead>
<tbody>
<tr>
<Td
expand={{
rowIndex: 0,
isExpanded: false,
onToggle: () => {}
}}
/>
<td>Test content</td>
</tr>
</tbody>
</Table>
);
const toggleButtons = screen.getAllByRole('button');
expect(toggleButtons).toHaveLength(2); // One in Th, one in Td
toggleButtons.forEach((button) => {
expect(button).toHaveClass('pf-m-small');
});
});
test(`Renders with class ${styles.modifiers.plain} when isPlain is true`, () => {
render(<Table isPlain aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.plain);
});
test(`Does not render with class ${styles.modifiers.plain} when isPlain is not defined`, () => {
render(<Table aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.plain);
});
test(`Does not render with class ${styles.modifiers.plain} when isPlain is false`, () => {
render(<Table isPlain={false} aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.plain);
});
test(`Renders with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is true`, () => {
render(<Table isNoPlainOnGlass aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.noPlainOnGlass);
});
test(`Does not render with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is undefined`, () => {
render(<Table aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.noPlainOnGlass);
});
test(`Does not render with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is false`, () => {
render(<Table isNoPlainOnGlass={false} aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.noPlainOnGlass);
});
test(`Renders with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is true`, () => {
render(<Table isStickyHeaderBase aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.stickyHeaderBase);
});
test(`Does not render with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is false`, () => {
render(<Table isStickyHeaderBase={false} aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderBase);
});
test(`Does not render with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is undefined`, () => {
render(<Table aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderBase);
});
test(`Renders with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is true`, () => {
render(<Table isStickyHeaderStuck aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.stickyHeaderStuck);
});
test(`Does not render with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is false`, () => {
render(<Table isStickyHeaderStuck={false} aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderStuck);
});
test(`Does not render with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is undefined`, () => {
render(<Table aria-label="Test table" />);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderStuck);
});