forked from pgadmin-org/pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchemaDialogView.spec.js
More file actions
300 lines (266 loc) · 11.2 KB
/
SchemaDialogView.spec.js
File metadata and controls
300 lines (266 loc) · 11.2 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2026, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import { act, render } from '@testing-library/react';
import {TestSchema} from './TestSchema.ui';
import SchemaView from '../../../pgadmin/static/js/SchemaView';
import pgAdmin from '../fake_pgadmin';
import { withBrowser } from '../genericFunctions';
import userEvent from '@testing-library/user-event';
function getSchema() {
return new TestSchema();
}
describe('SchemaView', ()=>{
const SchemaViewWithBrowser = withBrowser(SchemaView);
const user = userEvent.setup();
let confirmSpy;
beforeAll(()=>{
jest.spyOn(pgAdmin.Browser.notifier, 'alert').mockImplementation(() => {});
confirmSpy = jest.spyOn(pgAdmin.Browser.notifier, 'confirm');
});
describe('SchemaDialogView', ()=>{
let ctrl,
onSave=jest.fn(() => Promise.resolve()),
onClose=jest.fn(),
onHelp=jest.fn(),
onEdit=jest.fn(),
onDataChange=jest.fn(),
getSQLValue=jest.fn(() => {
return Promise.resolve('select 1;');
}),
ctrlMount = async (props)=>{
await act(async ()=>{
ctrl = await render(
<SchemaViewWithBrowser
formType='dialog'
schema={getSchema()}
viewHelperProps={{
mode: 'create',
}}
onSave={onSave}
onClose={onClose}
onHelp={onHelp}
onEdit={onEdit}
onDataChange={onDataChange}
confirmOnCloseReset={true}
hasSQL={true}
getSQLValue={getSQLValue}
disableSqlHelp={false}
{...props}
/>
);
});
},
simulateValidData = async ()=>{
// Wait for focus
await act(async ()=>{
await new Promise(resolve => setTimeout(resolve, 500));
});
await user.type(ctrl.container.querySelector('[name="field1"]'), 'val1');
await user.type(ctrl.container.querySelector('[name="field2"]'), '2');
await user.type(ctrl.container.querySelector('[name="field5"]'), 'val5');
/* Add a row */
await user.click(ctrl.container.querySelector('button[data-test="add-row"]'));
await user.click(ctrl.container.querySelector('button[data-test="add-row"]'));
// Wait for focus
await act(async ()=>{
await new Promise(resolve => setTimeout(resolve, 500));
});
await user.type(ctrl.container.querySelectorAll('[name="field5"]')[0], 'rval51');
await user.type(ctrl.container.querySelectorAll('[name="field5"]')[1], 'rval52');
// Wait for validations to run
await act(async ()=>{
await new Promise(resolve => setTimeout(resolve, 500));
});
};
describe('form fields', ()=>{
beforeEach(async ()=>{
await ctrlMount({
getInitData: ()=>Promise.resolve({}),
});
});
it('init', ()=>{
expect(ctrl.container.querySelectorAll('[data-testid="form-input"]').length).toBe(5);
expect(ctrl.container.querySelector('[data-test="Reset"]').hasAttribute('disabled')).toBe(true);
expect(ctrl.container.querySelector('[data-test="Save"]').hasAttribute('disabled')).toBe(true);
});
it('change text', async ()=>{
// Wait for autofocus timer (200ms in FormView) to complete
await act(async ()=>{
await new Promise(resolve => setTimeout(resolve, 500));
});
await user.type(ctrl.container.querySelector('[name="field2"]'), '2');
/* Error should come for field1 as it is empty and noEmpty true */
expect(ctrl.container.querySelector('[data-test="notifier-message"]')).toHaveTextContent('\'Field1\' cannot be empty.');
expect(ctrl.container.querySelector('[data-test="Save"]').hasAttribute('disabled')).toBe(true);
});
it('close error on click', async ()=>{
await user.click(ctrl.container.querySelector('[data-test="notifier-message"] button'));
expect(ctrl.container.querySelector('[data-test="notifier-message"]')).toBe(null);
});
it('valid form data', async ()=>{
await simulateValidData();
expect(ctrl.container.querySelector('[data-test="notifier-message"]')).toBe(null);
});
});
describe('DataGridView', ()=>{
beforeEach(async ()=>{
await ctrlMount({
getInitData: ()=>Promise.resolve({}),
});
});
it('add row', async ()=>{
await simulateValidData();
await user.click(ctrl.container.querySelector('[data-test="add-row"]'));
expect(ctrl.container.querySelectorAll('[data-test="data-table-row"]').length).toBe(3);
});
it('remove row OK', async ()=>{
await simulateValidData();
/* Press OK */
confirmSpy.mockClear();
await user.click(ctrl.container.querySelector('[data-test="delete-row"]'));
await act(async ()=>{
await new Promise(resolve => setTimeout(resolve));
confirmSpy.mock.calls[0][2]();
});
expect(confirmSpy.mock.calls[0][0]).toBe('Custom delete title');
expect(confirmSpy.mock.calls[0][1]).toBe('Custom delete message');
});
it('remove row Cancel', async ()=>{
await simulateValidData();
/* Press Cancel */
confirmSpy.mockClear();
await user.click(ctrl.container.querySelector('[data-test="delete-row"]'));
await act(async ()=>{
await new Promise(resolve => setTimeout(resolve));
confirmSpy.mock.calls[0][3]();
});
expect(ctrl.container.querySelector('[data-test="notifier-message"]')).toBe(null);
});
it('expand row', async ()=>{
await simulateValidData();
await user.click(ctrl.container.querySelector('[data-test="expand-row"]'));
expect(ctrl.container.querySelectorAll('[data-test="data-grid-view"] [data-test="form-view"]').length).toBe(1);
});
it('unique col test', async ()=>{
await simulateValidData();
await user.clear(ctrl.container.querySelectorAll('[name="field5"]')[1]);
await user.type(ctrl.container.querySelectorAll('[name="field5"]')[1], 'rval51');
expect(ctrl.container.querySelector('[data-test="notifier-message"]')).toHaveTextContent('Field5 in FieldColl must be unique.');
});
});
describe('SQL tab', ()=>{
beforeEach(async ()=>{
await ctrlMount({
getInitData: ()=>Promise.resolve({}),
});
});
it('no changes', async ()=>{
await user.click(ctrl.container.querySelector('button[data-test="SQL"]'));
expect(ctrl.container.querySelector('[data-testid="SQL"] .cm-content')).toHaveTextContent('-- No updates.');
});
it('data invalid', async ()=>{
await user.clear(ctrl.container.querySelector('[name="field2"]'));
await user.type(ctrl.container.querySelector('[name="field2"]'), '2');
await user.click(ctrl.container.querySelector('button[data-test="SQL"]'));
expect(ctrl.container.querySelector('[data-testid="SQL"] .cm-content')).toHaveTextContent('-- Definition incomplete.');
});
it('valid data', async ()=>{
await simulateValidData();
await user.click(ctrl.container.querySelector('button[data-test="SQL"]'));
expect(ctrl.container.querySelector('[data-testid="SQL"] .cm-content')).toHaveTextContent('select 1;');
});
});
describe('others', ()=>{
beforeEach(async ()=>{
await ctrlMount({
getInitData: ()=>Promise.resolve({}),
});
});
it('onSave click', async ()=>{
await simulateValidData();
onSave.mockClear();
await user.click(ctrl.container.querySelector('button[data-test="Save"]'));
expect(onSave.mock.calls[0][0]).toBe(true);
expect(onSave.mock.calls[0][1]).toEqual({
id: undefined,
field1: 'val1',
field2: '2',
field5: 'val5',
fieldcoll: [
{field3: null, field4: null, field5: 'rval51'},
{field3: null, field4: null, field5: 'rval52'},
]
});
expect(pgAdmin.Browser.notifier.alert).toHaveBeenCalledWith('Warning', 'some inform text');
});
it('onHelp SQL', async ()=>{
await user.click(ctrl.container.querySelector('[data-test="sql-help"]'));
expect(onHelp).toHaveBeenCalledWith(true, true);
});
it('onHelp Dialog', async ()=>{
await user.click(ctrl.container.querySelector('[data-test="dialog-help"]'));
expect(onHelp).toHaveBeenCalledWith(false, true);
});
});
describe('onReset', ()=>{
let onResetAction = (data)=> {
expect(ctrl.container.querySelector('[data-test="Reset"]').hasAttribute('disabled')).toBe(true);
expect(ctrl.container.querySelector('[data-test="Save"]').hasAttribute('disabled')).toBe(true);
const callArgs = onDataChange.mock.calls[onDataChange.mock.calls.length - 1];
expect(callArgs[0]).toEqual(false);
expect(callArgs[1]).toEqual(data);
};
beforeEach(async ()=>{
await ctrlMount({
getInitData: ()=>Promise.resolve({}),
});
});
it('with confirm check and yes click', async ()=>{
await simulateValidData();
onDataChange.mockClear();
confirmSpy.mockClear();
await user.click(ctrl.container.querySelector('button[data-test="Reset"]'));
/* Press OK */
await act(async ()=>{
await new Promise(resolve => setTimeout(resolve));
confirmSpy.mock.calls[0][2]();
});
onResetAction({ id: undefined, field1: null, field2: null, fieldcoll: null });
});
it('with confirm check and cancel click', async ()=>{
await simulateValidData();
confirmSpy.mockClear();
await user.click(ctrl.container.querySelector('button[data-test="Reset"]'));
/* Press cancel */
await act(async ()=>{
await new Promise(resolve => setTimeout(resolve));
confirmSpy.mock.calls[0][3]();
});
expect(ctrl.container.querySelector('[data-test="Reset"]').hasAttribute('disabled')).toBe(false);
expect(ctrl.container.querySelector('[data-test="Save"]').hasAttribute('disabled')).toBe(false);
});
it('no confirm check', async ()=>{
await ctrlMount({
confirmOnCloseReset: false,
});
await simulateValidData();
onDataChange.mockClear();
confirmSpy.mockClear();
await user.click(ctrl.container.querySelector('button[data-test="Reset"]'));
expect(confirmSpy).not.toHaveBeenCalled();
expect(ctrl.container.querySelector('[data-test="Reset"]').hasAttribute('disabled')).toBe(true);
expect(ctrl.container.querySelector('[data-test="Save"]').hasAttribute('disabled')).toBe(true);
// on reset, orig data will be considered
const callArgs = onDataChange.mock.calls[onDataChange.mock.calls.length - 1];
expect(callArgs[0]).toEqual(false);
expect(callArgs[1]).toEqual({ id: undefined, field1: null, field2: null, fieldcoll: null });
});
});
});
});