generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathheader-cell.test.tsx
More file actions
140 lines (124 loc) · 4.68 KB
/
header-cell.test.tsx
File metadata and controls
140 lines (124 loc) · 4.68 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import * as React from 'react';
import { fireEvent, render } from '@testing-library/react';
import { ContainerQueryEntry } from '@cloudscape-design/component-toolkit/internal/container-queries/interfaces.js';
import TestI18nProvider from '../../../lib/components/i18n/testing';
import { TableHeaderCell, TableHeaderCellProps } from '../../../lib/components/table/header-cell';
import { useStickyColumns } from '../../../lib/components/table/sticky-columns';
import { renderHook } from '../../__tests__/render-hook';
import { renderWithSingleTabStopNavigation } from '../../internal/context/__tests__/utils';
import { TableProps } from '../interfaces';
import styles from '../../../lib/components/table/header-cell/styles.css.js';
import resizerStyles from '../../../lib/components/table/resizer/styles.css.js';
jest.mock('@cloudscape-design/component-toolkit/internal', () => ({
...jest.requireActual('@cloudscape-design/component-toolkit/internal'),
useResizeObserver: (_getElement: () => null | HTMLElement, onObserve: (entry: ContainerQueryEntry) => void) => {
React.useLayoutEffect(() => {
onObserve({ borderBoxWidth: 234 } as ContainerQueryEntry);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
},
}));
const tableRole = 'table';
const testItem = {
test: 'test',
};
const column: TableProps.ColumnDefinition<typeof testItem> = {
id: 'test',
header: 'Test',
sortingField: 'test',
editConfig: {
ariaLabel: 'test input',
editIconAriaLabel: 'error',
editingCell: (item, { setValue, currentValue }: TableProps.CellContext<any>) => {
return <input type="text" value={currentValue ?? item.test} onChange={e => setValue(e.target.value)} />;
},
},
cell: item => item.test,
};
const { result } = renderHook(() =>
useStickyColumns({ visibleColumns: ['id'], stickyColumnsFirst: 0, stickyColumnsLast: 0 })
);
function TableWrapper({ children }: { children: React.ReactNode }) {
return (
<table>
<thead>
<tr>{children}</tr>
</thead>
</table>
);
}
function TestComponent(props: Partial<TableHeaderCellProps<typeof testItem>>) {
return (
<TableHeaderCell<typeof testItem>
column={column}
colIndex={0}
tabIndex={0}
updateColumn={() => {}}
onClick={() => {}}
onResizeFinish={() => {}}
stickyState={result.current}
columnId="id"
cellRef={() => {}}
tableRole={tableRole}
variant="container"
{...props}
/>
);
}
it('renders a fake focus outline on the sort control', () => {
const { container } = render(
<TableWrapper>
<TestComponent focusedComponent="sorting-control-id" />
</TableWrapper>
);
// Activate focus-visible
fireEvent.keyDown(document.body, { key: 'Tab', keyCode: 65 });
expect(container.querySelector(`.${styles['header-cell-fake-focus']}`)).toBeInTheDocument();
});
it('renders a fake focus outline on the resize control', () => {
const { container } = render(
<TableWrapper>
<TestComponent focusedComponent="resize-control-id" resizableColumns={true} />
</TableWrapper>
);
// Activate focus-visible
fireEvent.keyDown(document.body, { key: 'Tab', keyCode: 65 });
expect(container.querySelector(`.${resizerStyles['has-focus']}`)).toBeInTheDocument();
});
it('updates based on resize observer if dynamic flag is set', () => {
const updateColumn = jest.fn();
render(
<TableWrapper>
<TestComponent hasDynamicContent={true} hidden={true} updateColumn={updateColumn} />
</TableWrapper>
);
expect(updateColumn).toHaveBeenCalledWith('id', 234);
});
describe('i18n', () => {
it('supports using editIconAriaLabel from i18n provider', () => {
const { container } = render(
<TestI18nProvider messages={{ table: { 'columnDefinitions.editConfig.editIconAriaLabel': 'Custom editable' } }}>
<TableWrapper>
<TestComponent column={{ ...column, editConfig: undefined }} resizableColumns={true} isEditable={true} />
</TableWrapper>
</TestI18nProvider>
);
expect(container.querySelector(`.${styles['edit-icon']} [role=img]`)).toHaveAttribute(
'aria-label',
'Custom editable'
);
});
test('does not set tab index when negative', () => {
const { setCurrentTarget } = renderWithSingleTabStopNavigation(<TestComponent />, {
navigationActive: true,
});
const headerCell = document.querySelector('th')!;
expect(headerCell).not.toHaveAttribute('tabIndex');
setCurrentTarget(headerCell);
expect(headerCell).toHaveAttribute('tabIndex', '0');
setCurrentTarget(null);
expect(headerCell).not.toHaveAttribute('tabIndex');
});
});