forked from patternfly/react-component-groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumnManagementModal.test.tsx
More file actions
141 lines (115 loc) · 4.56 KB
/
ColumnManagementModal.test.tsx
File metadata and controls
141 lines (115 loc) · 4.56 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
import '@testing-library/jest-dom'
import { render, screen, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ColumnManagementModal, { ColumnManagementModalColumn } from './ColumnManagementModal';
const DEFAULT_COLUMNS : ColumnManagementModalColumn[] = [
{
title: 'ID',
key: 'id',
isShownByDefault: true,
isShown: true,
isUntoggleable: true
},
{
title: 'Publish date',
key: 'publishDate',
isShownByDefault: true,
isShown: true
},
{
title: 'Impact',
key: 'impact',
isShownByDefault: true,
isShown: true
},
{
title: 'Score',
key: 'score',
isShownByDefault: false,
isShown: false
}
];
const onClose = jest.fn();
const setColumns = jest.fn();
// Simple mock to track when DragDropSort is used
jest.mock('@patternfly/react-drag-drop', () => ({
DragDropSort: ({ children }) => <div data-testid="drag-drop-sort">{children}</div>,
Droppable: ({ wrapper }) => wrapper,
}));
const renderColumnManagementModal = (props = {}) => render(<ColumnManagementModal
appliedColumns={DEFAULT_COLUMNS}
applyColumns={newColumns => setColumns(newColumns)}
isOpen
onClose={onClose}
data-testid="column-mgmt-modal"
{...props}
/>);
beforeEach(() => {
jest.clearAllMocks();
renderColumnManagementModal();
});
const getCheckboxesState = () => {
// Get only the column checkboxes (exclude the BulkSelect checkbox)
const checkboxes = screen.getByTestId('column-mgmt-modal').querySelectorAll('input[type="checkbox"][data-testid^="column-check-"]');
return (Array.from(checkboxes) as HTMLInputElement[]).map(c => c.checked);
}
describe('ColumnManagementModal component', () => {
it('should have disabled and checked checkboxes for columns that should always be shown', () => {
expect(getCheckboxesState()).toEqual(DEFAULT_COLUMNS.map(c => c.isShownByDefault));
});
it('should have checkbox checked if column is shown by default', () => {
const idCheckbox = screen.getByTestId('column-mgmt-modal').querySelector('input[type="checkbox"][data-testid="column-check-id"]');
expect(idCheckbox).toHaveAttribute('disabled');
expect(idCheckbox).toHaveAttribute('checked');
});
it('should set columns to default state upon clicking on "Reset to default"', () => {
// disable Impact column which is enabled by default
fireEvent.click(screen.getByText('Impact'));
// enable Score column which is disabled by default
fireEvent.click(screen.getByText('Score'));
fireEvent.click(screen.getByText('Reset to default'));
expect(getCheckboxesState()).toEqual(DEFAULT_COLUMNS.map(c => c.isShownByDefault));
});
it('should set all columns to show upon clicking on "Select all"', async () => {
// disable Impact column which is enabled by default
fireEvent.click(screen.getByText('Impact'));
// Use the BulkSelect to select all
const menuToggle = screen.getByLabelText('Bulk select toggle');
await userEvent.click(menuToggle);
const selectAllButton = screen.getByText('Select all (4)');
await userEvent.click(selectAllButton);
expect(getCheckboxesState()).toEqual(DEFAULT_COLUMNS.map(_ => true));
});
it('should change columns on save', () => {
fireEvent.click(screen.getByText('Impact'));
fireEvent.click(screen.getByText('Save'));
const expectedColumns = DEFAULT_COLUMNS;
const impactColumn = expectedColumns.find(c => c.title === 'Impact');
if (impactColumn === undefined) {
throw new Error('Expected to find "Impact" column in "DEFAULT_COLUMNS"');
}
impactColumn.isShown = false;
expect(onClose).toHaveBeenCalled();
expect(setColumns).toHaveBeenCalledWith(expectedColumns);
});
it('should retain columns on cancel', () => {
fireEvent.click(screen.getByText('Impact'));
fireEvent.click(screen.getByText('Cancel'));
expect(onClose).toHaveBeenCalled();
// applyColumns should NOT be called on cancel
expect(setColumns).not.toHaveBeenCalled();
});
describe('enableDragDrop prop', () => {
it('should default enableDragDrop to false', () => {
// Default behavior should not enable drag and drop
expect(screen.queryByTestId('drag-drop-sort')).not.toBeInTheDocument();
});
it('should pass enableDragDrop prop to ListManager', () => {
// Test that the prop is properly passed through to ListManager
jest.clearAllMocks();
renderColumnManagementModal({ enableDragDrop: true });
// When enableDragDrop is true, DragDropSort should be rendered
expect(screen.getByTestId('drag-drop-sort')).toBeInTheDocument();
});
});
});