forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomColumnBuilder.test.tsx
More file actions
215 lines (181 loc) · 7.24 KB
/
CustomColumnBuilder.test.tsx
File metadata and controls
215 lines (181 loc) · 7.24 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
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { EventShimCustomEvent } from '@deephaven/utils';
import CustomColumnBuilder, {
CustomColumnBuilderProps,
} from './CustomColumnBuilder';
import IrisGridTestUtils from '../IrisGridTestUtils';
import IrisGridModel from '../IrisGridModel';
function Builder({
model = IrisGridTestUtils.makeModel(),
customColumns = [],
onSave = jest.fn(),
onCancel = jest.fn(),
}: Partial<CustomColumnBuilderProps>) {
return (
<CustomColumnBuilder
model={model}
customColumns={customColumns}
onSave={onSave}
onCancel={onCancel}
/>
);
}
test('Renders the default state', async () => {
render(<Builder />);
expect(screen.getByPlaceholderText('Column Name')).toBeInTheDocument();
expect(screen.getByText('Column Formula')).toBeInTheDocument();
});
test('Calls on save', async () => {
const user = userEvent.setup();
const customColumns = ['abc=def', 'foo=bar'];
const mockSave = jest.fn();
render(<Builder onSave={mockSave} customColumns={customColumns} />);
await user.type(screen.getByDisplayValue('abc'), 'cba');
await user.click(screen.getByText(/Save/));
expect(mockSave).toHaveBeenLastCalledWith(['abccba=def', 'foo=bar']);
});
test('Switches to loader button while saving', async () => {
jest.useFakeTimers();
const user = userEvent.setup({ delay: null });
const model = IrisGridTestUtils.makeModel();
const mockSave = jest.fn(() =>
setTimeout(() => {
model.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.COLUMNS_CHANGED)
);
}, 50)
);
render(
<Builder model={model} onSave={mockSave} customColumns={['foo=bar']} />
);
await user.click(screen.getByText(/Save/));
expect(screen.getByText('Applying')).toBeInTheDocument();
jest.advanceTimersByTime(50);
expect(screen.getByText('Success')).toBeInTheDocument();
jest.advanceTimersByTime(CustomColumnBuilder.SUCCESS_SHOW_DURATION);
expect(screen.getByText(/Save/)).toBeInTheDocument();
// Component should ignore this event and not change the save button
model.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.COLUMNS_CHANGED)
);
expect(screen.getByText(/Save/)).toBeInTheDocument();
jest.useRealTimers();
});
test('Adds a column', async () => {
const user = userEvent.setup();
render(<Builder />);
await user.click(screen.getByText('Add Another Column'));
expect(screen.getAllByPlaceholderText('Column Name').length).toBe(2);
expect(screen.getAllByText('Column Formula').length).toBe(2);
});
test('Ignores empty names or formulas on save', async () => {
const user = userEvent.setup();
const customColumns = ['foo=bar'];
const mockSave = jest.fn();
render(<Builder customColumns={customColumns} onSave={mockSave} />);
await user.click(screen.getByText('Add Another Column'));
await user.type(screen.getAllByPlaceholderText('Column Name')[1], 'test');
await user.click(screen.getByText(/Save/));
expect(mockSave).toBeCalledWith(customColumns);
});
test('Ignores deleted formulas on save', async () => {
// There is an issue with populating the custom columns and then editing the existing column
// RTL/monaco aren't playing nicely and it won't edit the existing text
// This test instead creates the new text, saves, then removes it to test the same behavior
jest.useFakeTimers();
const user = userEvent.setup({ delay: null });
const model = IrisGridTestUtils.makeModel();
const mockSave = jest.fn(() =>
setTimeout(() => {
model.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.COLUMNS_CHANGED)
);
}, 50)
);
const { container } = render(<Builder model={model} onSave={mockSave} />);
await user.type(screen.getByPlaceholderText('Column Name'), 'foo');
await user.click(container.querySelectorAll('.input-editor-wrapper')[0]);
await user.keyboard('bar');
await user.click(screen.getByText(/Save/));
jest.advanceTimersByTime(50); // Applying duration
jest.advanceTimersByTime(CustomColumnBuilder.SUCCESS_SHOW_DURATION);
expect(mockSave).toBeCalledWith(['foo=bar']);
mockSave.mockClear();
await user.click(container.querySelectorAll('.input-editor-wrapper')[0]);
await user.keyboard('{Control>}a{/Control}{Backspace}');
await user.click(screen.getByText(/Save/));
expect(mockSave).toBeCalledWith([]);
jest.useRealTimers();
});
test('Deletes columns', async () => {
const user = userEvent.setup();
const customColumns = ['abc=def', 'foo=bar'];
render(<Builder customColumns={customColumns} />);
await user.click(screen.getAllByLabelText(/Delete/)[0]);
expect(screen.queryByDisplayValue('abc')).toBeNull();
expect(screen.queryByDisplayValue('def')).toBeNull();
expect(screen.getByDisplayValue('foo')).toBeInTheDocument();
expect(screen.getByDisplayValue('bar')).toBeInTheDocument();
await user.click(screen.getByLabelText(/Delete/));
expect(screen.queryByDisplayValue('foo')).toBeNull();
expect(screen.queryByDisplayValue('bar')).toBeNull();
expect(screen.getByPlaceholderText('Column Name')).toBeInTheDocument();
expect(screen.getByText('Column Formula')).toBeInTheDocument();
});
test('Displays request failure message', async () => {
const user = userEvent.setup();
const model = IrisGridTestUtils.makeModel();
render(<Builder model={model} customColumns={['foo=bar']} />);
// Should ignore this since not in saving state
model.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.REQUEST_FAILED, {
detail: { errorMessage: 'Error message' },
})
);
expect(screen.queryByText(/Error message/)).toBeNull();
await user.click(screen.getByText(/Save/));
model.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.REQUEST_FAILED, {
detail: { errorMessage: 'Error message' },
})
);
expect(screen.getByText(/Error message/)).toBeInTheDocument();
const input = screen.getByDisplayValue('foo');
await user.click(input);
expect(input).not.toHaveClass('is-invalid');
});
test('Handles focus changes via keyboard', async () => {
const user = userEvent.setup();
const { container } = render(
<Builder customColumns={['abc=bar', 'foo=bar']} />
);
const nameInputs = screen.getAllByPlaceholderText('Column Name');
const formulaInputs = container.querySelectorAll(
'.input-editor-wrapper textarea'
);
const deleteButtons = screen.getAllByLabelText(/Delete/);
const dragHandles = screen.getAllByLabelText(/Drag/);
await user.click(nameInputs[0]);
await user.keyboard('{Tab}');
expect(deleteButtons[0]).toHaveFocus();
await user.keyboard('{Tab}');
expect(dragHandles[0]).toHaveFocus();
await user.keyboard('{Tab}');
expect(formulaInputs[0]).toHaveFocus();
await user.keyboard('{Tab}');
expect(nameInputs[1]).toHaveFocus();
await user.keyboard('{Tab}');
expect(deleteButtons[1]).toHaveFocus();
await user.keyboard('{Tab}');
expect(dragHandles[1]).toHaveFocus();
await user.keyboard('{Tab}');
expect(formulaInputs[1]).toHaveFocus();
await user.keyboard('{Tab}');
expect(screen.getByText('Add Another Column')).toHaveFocus();
await user.keyboard('{Shift>}{Tab}{/Shift}');
expect(formulaInputs[1]).toHaveFocus();
await user.keyboard('{Shift>}{Tab}{/Shift}');
expect(dragHandles[1]).toHaveFocus();
});