-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
useSelecteViewColumns.test.tsx
59 lines (45 loc) 路 1.87 KB
/
useSelecteViewColumns.test.tsx
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
import { act, renderHook } from '@testing-library/react';
import { describe, expect, test } from 'vitest';
import useSelectViewColumns from 'hooks/useSelectViewColumns';
const mockStructure = [
{ title: 'Method', key: 'method' },
{ title: 'Name', key: 'name' },
{ title: '# Requests', key: 'numRequests' },
];
describe('useSelectViewColumns hook', () => {
test('should initialize with default columns', () => {
const { result } = renderHook(() => useSelectViewColumns(mockStructure));
expect(result.current.selectedColumns).toEqual(mockStructure.map(s => s.key));
});
test('should add a new column', () => {
const { result } = renderHook(() => useSelectViewColumns(mockStructure));
act(() => {
result.current.addColumn('column3');
});
expect(result.current.selectedColumns).toEqual([...mockStructure.map(s => s.key), 'column3']);
});
test('should remove an existing column', () => {
const { result } = renderHook(() => useSelectViewColumns(mockStructure));
act(() => {
result.current.removeColumn('method');
});
expect(result.current.selectedColumns).toEqual(['name', 'numRequests']);
});
test('filterStructure should filter out unselected columns', () => {
const { result } = renderHook(() => useSelectViewColumns(mockStructure));
act(() => {
// remove column with key 'method'
result.current.removeColumn('method');
});
const filteredStructure = result.current.filteredStructure;
// expect only columns with keys 'name' and 'numRequests' to be returned
expect(filteredStructure.length).toBe(2);
expect(filteredStructure).toEqual(
expect.arrayContaining([
expect.objectContaining({ key: 'name' }),
expect.objectContaining({ key: 'numRequests' }),
]),
);
expect(filteredStructure).not.toContainEqual(expect.objectContaining({ key: 'method' }));
});
});