@instructure/ui-table 10.19.2-snapshot-2 → 10.19.2-snapshot-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,439 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import { Component } from 'react'
26
- import { render, screen, waitFor } from '@testing-library/react'
27
- import { MockInstance, vi } from 'vitest'
28
- import { userEvent } from '@testing-library/user-event'
29
- import '@testing-library/jest-dom'
30
-
31
- import { Table } from '../index'
32
- import type { TableProps } from '../props'
33
- import type { TableColHeaderProps } from '../ColHeader/props'
34
- import { runAxeCheck } from '@instructure/ui-axe-check'
35
-
36
- describe('<Table />', async () => {
37
- let consoleErrorMock: MockInstance<typeof console.error>
38
-
39
- beforeEach(() => {
40
- // Mocking console to prevent test output pollution
41
- consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(() => {})
42
- })
43
-
44
- afterEach(() => {
45
- consoleErrorMock.mockRestore()
46
- })
47
-
48
- const renderTable = (props?: TableProps) =>
49
- render(
50
- <Table caption="Test table" {...props}>
51
- <Table.Head>
52
- <Table.Row>
53
- <Table.ColHeader id="foo">ColHeader</Table.ColHeader>
54
- <Table.ColHeader id="bar">Bar-header</Table.ColHeader>
55
- </Table.Row>
56
- </Table.Head>
57
- <Table.Body>
58
- <Table.Row>
59
- <Table.RowHeader>RowHeader</Table.RowHeader>
60
- <Table.Cell>Cell</Table.Cell>
61
- </Table.Row>
62
- </Table.Body>
63
- </Table>
64
- )
65
-
66
- it('should render a caption', async () => {
67
- const { container } = renderTable()
68
- const caption = container.querySelector('caption')
69
-
70
- expect(caption).toBeInTheDocument()
71
- expect(caption).toHaveTextContent('Test table')
72
- })
73
-
74
- it('should meet a11y standards', async () => {
75
- const { container } = renderTable()
76
- const axeCheck = await runAxeCheck(container)
77
-
78
- expect(axeCheck).toBe(true)
79
- })
80
-
81
- it('applies a fixed column layout', async () => {
82
- await renderTable({
83
- layout: 'fixed'
84
- })
85
- const table = screen.getByRole('table')
86
-
87
- expect(table).toHaveStyle({ tableLayout: 'fixed' })
88
- })
89
-
90
- it('passes hover to table row', async () => {
91
- renderTable({
92
- hover: true
93
- })
94
- const tableRows = screen.getAllByRole('row')
95
-
96
- tableRows.forEach((tableRow) => {
97
- expect(tableRow).not.toHaveAttribute('border-left', 'none')
98
- expect(tableRow).not.toHaveAttribute('border-right', 'none')
99
- })
100
- })
101
-
102
- it('sets the scope of column header to col', async () => {
103
- await renderTable()
104
- const columnHeaders = screen.getAllByRole('columnheader')
105
-
106
- columnHeaders.forEach((columnHeader) => {
107
- expect(columnHeader).toHaveAttribute('scope', 'col')
108
- })
109
- })
110
-
111
- it('sets the scope of row header to row', async () => {
112
- renderTable()
113
- const rowHeaders = screen.getAllByRole('rowheader')
114
-
115
- rowHeaders.forEach((rowHeader) => {
116
- expect(rowHeader).toHaveAttribute('scope', 'row')
117
- })
118
- })
119
-
120
- it('can render table in stacked layout', async () => {
121
- renderTable({
122
- layout: 'stacked'
123
- })
124
- const stackedTable = screen.getByRole('table')
125
-
126
- expect(stackedTable).toBeInTheDocument()
127
- expect(stackedTable).toHaveTextContent('RowHeader')
128
- expect(stackedTable).toHaveTextContent('Cell')
129
- expect(stackedTable).not.toHaveTextContent('ColHeader')
130
- })
131
-
132
- it('can handle non-existent head in stacked layout', async () => {
133
- render(
134
- <Table caption="Test table" layout="stacked">
135
- <Table.Body></Table.Body>
136
- </Table>
137
- )
138
- const stackedTable = screen.getByRole('table')
139
-
140
- expect(stackedTable).toBeInTheDocument()
141
- })
142
-
143
- it('can handle empty head in stacked layout', async () => {
144
- render(
145
- <Table caption="Test table" layout="stacked">
146
- <Table.Head></Table.Head>
147
- </Table>
148
- )
149
- const stackedTable = screen.getByRole('table')
150
-
151
- expect(stackedTable).toBeInTheDocument()
152
- })
153
-
154
- it('can handle invalid header in stacked layout', async () => {
155
- render(
156
- <Table caption="Test table" layout="stacked">
157
- <Table.Head>
158
- {/* @ts-expect-error error is normal here */}
159
- <Table.Row>
160
- <Table.Cell>Foo</Table.Cell>
161
- {}
162
- {false}
163
- </Table.Row>
164
- </Table.Head>
165
- <Table.Body>
166
- <Table.Row>
167
- <Table.RowHeader>1</Table.RowHeader>
168
- <Table.Cell>The Shawshank Redemption</Table.Cell>
169
- <Table.Cell>1994</Table.Cell>
170
- <Table.Cell>9.3</Table.Cell>
171
- </Table.Row>
172
- <Table.Row>
173
- <Table.RowHeader>2</Table.RowHeader>
174
- <Table.Cell>The Godfather</Table.Cell>
175
- <Table.Cell>1972</Table.Cell>
176
- <Table.Cell>9.2</Table.Cell>
177
- </Table.Row>
178
- </Table.Body>
179
- </Table>
180
- )
181
- const stackedTable = screen.getByRole('table')
182
-
183
- expect(stackedTable).toBeInTheDocument()
184
- expect(stackedTable).not.toHaveTextContent('Foo')
185
- })
186
-
187
- it('does not crash for invalid children in stacked layout', async () => {
188
- render(
189
- <Table caption="Test table" layout="stacked">
190
- test1
191
- <span>test</span>
192
- {/* @ts-ignore error is normal here */}
193
- <Table.Head>
194
- <span>test</span>
195
- test2
196
- {/* @ts-ignore error is normal here */}
197
- <Table.Row>
198
- test3
199
- <span>test</span>
200
- <Table.Cell>Foo</Table.Cell>
201
- </Table.Row>
202
- test4
203
- <span>test</span>
204
- </Table.Head>
205
- test5
206
- <Table.Body>
207
- test
208
- <span>test</span>
209
- {/* @ts-ignore error is normal here */}
210
- <Table.Row>
211
- test
212
- <span>test</span>
213
- <Table.Cell>Foo</Table.Cell>
214
- test
215
- <span>test</span>
216
- </Table.Row>
217
- </Table.Body>
218
- </Table>
219
- )
220
- const table = screen.getByRole('table')
221
-
222
- expect(table).toBeInTheDocument()
223
- expect(table).toHaveTextContent('Foo')
224
- })
225
-
226
- describe('when table is sortable', async () => {
227
- const renderSortableTable = (
228
- props: TableColHeaderProps | null,
229
- handlers = {},
230
- layout: TableProps['layout'] = 'auto'
231
- ) =>
232
- render(
233
- <Table caption="Sortable table" layout={layout}>
234
- <Table.Head>
235
- <Table.Row>
236
- <Table.ColHeader id="foo" {...props} {...handlers}>
237
- Foo
238
- </Table.ColHeader>
239
- <Table.ColHeader id="bar" {...handlers}>
240
- Bar
241
- </Table.ColHeader>
242
- </Table.Row>
243
- </Table.Head>
244
- <Table.Body>
245
- <Table.Row></Table.Row>
246
- <Table.Row></Table.Row>
247
- </Table.Body>
248
- </Table>
249
- )
250
-
251
- it('can render up arrow for ascending order', async () => {
252
- const { container } = renderSortableTable({
253
- id: 'id',
254
- sortDirection: 'ascending'
255
- })
256
- const arrow = container.querySelector('svg')
257
-
258
- expect(arrow).toHaveAttribute('name', 'IconMiniArrowUp')
259
- })
260
-
261
- it('can render down arrow for descending order', async () => {
262
- const { container } = renderSortableTable({
263
- id: 'id',
264
- sortDirection: 'descending'
265
- })
266
- const arrow = container.querySelector('svg')
267
-
268
- expect(arrow).toHaveAttribute('name', 'IconMiniArrowDown')
269
- })
270
-
271
- it('calls onRequestSort when column header is clicked', async () => {
272
- const onRequestSort = vi.fn()
273
- renderSortableTable(
274
- {
275
- id: 'id'
276
- },
277
- {
278
- onRequestSort
279
- }
280
- )
281
- const button = screen.getByRole('button', { name: 'Foo' })
282
-
283
- userEvent.click(button)
284
-
285
- await waitFor(() => {
286
- expect(onRequestSort).toHaveBeenCalledTimes(1)
287
- })
288
- })
289
-
290
- it('can display custom label in the select in stacked layout', async () => {
291
- renderSortableTable(
292
- {
293
- id: 'id',
294
- stackedSortByLabel: 'Custom Text'
295
- },
296
- {
297
- onRequestSort: vi.fn()
298
- },
299
- 'stacked'
300
- )
301
- const input = screen.getByRole('combobox')
302
-
303
- userEvent.click(input)
304
-
305
- await waitFor(async () => {
306
- const options = screen.getAllByRole('option')
307
-
308
- expect(options[0]).toHaveTextContent('Custom Text')
309
- expect(options[1]).toHaveTextContent('bar')
310
- })
311
- })
312
-
313
- it('can render check mark for sorted column in stacked layout', async () => {
314
- const { container } = renderSortableTable(
315
- {
316
- id: 'id',
317
- sortDirection: 'ascending'
318
- },
319
- {
320
- onRequestSort: vi.fn()
321
- },
322
- 'stacked'
323
- )
324
- const icon = container.querySelector('svg')
325
-
326
- expect(icon).toHaveAttribute('name', 'IconCheck')
327
- })
328
-
329
- it('creates proper aria-sort attributes (ascending)', async () => {
330
- renderSortableTable({
331
- id: 'id',
332
- sortDirection: 'ascending'
333
- })
334
- const header = screen.getByRole('columnheader', { name: 'Foo' })
335
-
336
- expect(header).toHaveAttribute('aria-sort', 'ascending')
337
- })
338
-
339
- it('creates proper aria-sort attributes (descending)', async () => {
340
- renderSortableTable({
341
- id: 'id',
342
- sortDirection: 'descending'
343
- })
344
- const header = screen.getByRole('columnheader', { name: 'Foo' })
345
-
346
- expect(header).toHaveAttribute('aria-sort', 'descending')
347
- })
348
- })
349
-
350
- describe('when using custom components', () => {
351
- it('should render wrapper HOCs', () => {
352
- class CustomTableCell extends Component<any> {
353
- render() {
354
- return <Table.Cell {...this.props}>{this.props.children}</Table.Cell>
355
- }
356
- }
357
- class CustomTableRow extends Component {
358
- render() {
359
- return (
360
- <Table.Row {...this.props}>
361
- <Table.RowHeader>1</Table.RowHeader>
362
- <Table.Cell>The Shawshank Redemption</Table.Cell>
363
- <CustomTableCell>9.3</CustomTableCell>
364
- </Table.Row>
365
- )
366
- }
367
- }
368
- const table = render(
369
- <Table caption="Test custom table">
370
- <Table.Head>
371
- <Table.Row>
372
- <Table.ColHeader id="foo">ColHeader</Table.ColHeader>
373
- <Table.ColHeader id="bar">Bar-header</Table.ColHeader>
374
- <Table.ColHeader id="baz">Bar-header</Table.ColHeader>
375
- </Table.Row>
376
- </Table.Head>
377
- <Table.Body>
378
- <CustomTableRow />
379
- <Table.Row>
380
- <Table.RowHeader>RowHeader</Table.RowHeader>
381
- <Table.Cell>Cell</Table.Cell>
382
- <Table.Cell>Cell2</Table.Cell>
383
- </Table.Row>
384
- </Table.Body>
385
- </Table>
386
- )
387
- const stackedTable = screen.getByRole('table')
388
-
389
- expect(stackedTable).toBeInTheDocument()
390
- const { container } = table
391
- expect(container).toBeInTheDocument()
392
- expect(container).toHaveTextContent('The Shawshank Redemption')
393
- expect(container).toHaveTextContent('9.3')
394
- })
395
-
396
- it('should render fully custom components', () => {
397
- class CustomTableCell extends Component<any> {
398
- render() {
399
- return <td>{this.props.children}</td>
400
- }
401
- }
402
-
403
- class CustomTableRow extends Component<any> {
404
- render() {
405
- return <tr>{this.props.children}</tr>
406
- }
407
- }
408
-
409
- const table = render(
410
- <Table caption="Test custom table">
411
- <Table.Head>
412
- <CustomTableRow>
413
- <CustomTableCell id="foo">ColHeader</CustomTableCell>
414
- <CustomTableCell id="bar">Bar-header</CustomTableCell>
415
- <Table.ColHeader id="baz">Bar-header</Table.ColHeader>
416
- </CustomTableRow>
417
- </Table.Head>
418
- <Table.Body>
419
- <CustomTableRow>
420
- <Table.RowHeader>RowHeader2</Table.RowHeader>
421
- <CustomTableCell>Cell</CustomTableCell>
422
- <Table.Cell>Cell2</Table.Cell>
423
- </CustomTableRow>
424
- </Table.Body>
425
- </Table>
426
- )
427
- const stackedTable = screen.getByRole('table')
428
-
429
- expect(stackedTable).toBeInTheDocument()
430
- const { container } = table
431
- expect(container).toBeInTheDocument()
432
- expect(container).toHaveTextContent('ColHeader')
433
- expect(container).toHaveTextContent('Bar-header')
434
- expect(container).toHaveTextContent('RowHeader2')
435
- expect(container).toHaveTextContent('Cell')
436
- expect(container).toHaveTextContent('Cell2')
437
- })
438
- })
439
- })
@@ -1,2 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- //# sourceMappingURL=Table.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Table.test.d.ts","sourceRoot":"","sources":["../../../src/Table/__new-tests__/Table.test.tsx"],"names":[],"mappings":"AA4BA,OAAO,2BAA2B,CAAA"}