-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
dateEditor.spec.ts
674 lines (549 loc) · 29.4 KB
/
dateEditor.spec.ts
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
import moment from 'moment-mini';
import { Editors } from '../index';
import { DateEditor } from '../dateEditor';
import { FieldType } from '../../enums/index';
import { Column, ColumnEditor, Editor, EditorArguments, GridOption } from '../../interfaces/index';
import { TranslateServiceStub } from '../../../../../test/translateServiceStub';
import { SlickEvent, type SlickDataView, type SlickGrid } from '../../core/index';
const containerId = 'demo-container';
// define a <div> container to simulate the grid container
const template = `<div id="${containerId}"></div>`;
const dataViewStub = {
refresh: jest.fn(),
} as unknown as SlickDataView;
const gridOptionMock = {
autoCommitEdit: false,
editable: true,
translater: null,
} as unknown as GridOption;
const getEditorLockMock = {
commitCurrentEdit: jest.fn(),
};
const gridStub = {
focus: jest.fn(),
getActiveCell: jest.fn(),
getColumns: jest.fn(),
getEditorLock: () => getEditorLockMock,
getHeaderRowColumn: jest.fn(),
getOptions: () => gridOptionMock,
navigateNext: jest.fn(),
navigatePrev: jest.fn(),
render: jest.fn(),
onBeforeEditCell: new SlickEvent(),
onCompositeEditorChange: new SlickEvent(),
} as unknown as SlickGrid;
describe('DateEditor', () => {
let translateService: TranslateServiceStub;
let divContainer: HTMLDivElement;
let editor: DateEditor;
let editorArguments: EditorArguments;
let mockColumn: Column;
let mockItemData: any;
beforeEach(() => {
translateService = new TranslateServiceStub();
divContainer = document.createElement('div');
divContainer.innerHTML = template;
document.body.appendChild(divContainer);
mockColumn = { id: 'startDate', field: 'startDate', editable: true, editor: { model: Editors.date }, editorClass: {} as Editor } as Column;
editorArguments = {
grid: gridStub,
column: mockColumn,
item: mockItemData,
event: null as any,
cancelChanges: jest.fn(),
commitChanges: jest.fn(),
container: divContainer,
columnMetaData: null,
dataView: dataViewStub,
gridPosition: { top: 0, left: 0, bottom: 10, right: 10, height: 100, width: 100, visible: true },
position: { top: 0, left: 0, bottom: 10, right: 10, height: 100, width: 100, visible: true },
};
});
describe('with invalid Editor instance', () => {
it('should throw an error when trying to call init without any arguments', (done) => {
try {
editor = new DateEditor(null as any);
} catch (e) {
expect(e.toString()).toContain(`[Slickgrid-Universal] Something is wrong with this grid, an Editor must always have valid arguments.`);
done();
}
});
});
describe('with valid Editor instance', () => {
beforeEach(() => {
mockItemData = { id: 1, startDate: '2001-01-02T11:02:02.000Z', isActive: true };
mockColumn = { id: 'startDate', field: 'startDate', editable: true, editor: { model: Editors.date }, editorClass: {} as Editor } as Column;
editorArguments.column = mockColumn;
editorArguments.item = mockItemData;
});
afterEach(() => {
editor.destroy();
});
it('should initialize the editor', () => {
gridOptionMock.translater = translateService;
editor = new DateEditor(editorArguments);
const editorCount = divContainer.querySelectorAll('input.editor-text.editor-startDate').length;
expect(editorCount).toBe(1);
});
it('should initialize the editor and expect to focus on the element after a small delay', (done) => {
const focusSpy = jest.spyOn(editor, 'focus');
const showSpy = jest.spyOn(editor, 'focus');
editor = new DateEditor(editorArguments);
const editorCount = divContainer.querySelectorAll('input.editor-text.editor-startDate').length;
setTimeout(() => {
expect(editorCount).toBe(1);
expect(focusSpy).toHaveBeenCalled();
expect(showSpy).toHaveBeenCalled();
done();
}, 51);
});
it('should have a placeholder when defined in its column definition', () => {
const testValue = 'test placeholder';
mockColumn.editor!.placeholder = testValue;
editor = new DateEditor(editorArguments);
const editorElm = divContainer.querySelector('input.editor-text.editor-startDate') as HTMLTextAreaElement;
expect(editorElm.placeholder).toBe(testValue);
});
it('should have a title (tooltip) when defined in its column definition', () => {
const testValue = 'test title';
mockColumn.editor!.title = testValue;
editor = new DateEditor(editorArguments);
const editorElm = divContainer.querySelector('input.editor-text.editor-startDate') as HTMLTextAreaElement;
expect(editorElm.title).toBe(testValue);
});
it('should call "columnEditor" GETTER and expect to equal the editor settings we provided', () => {
mockColumn.editor = {
placeholder: 'test placeholder',
title: 'test title',
};
editor = new DateEditor(editorArguments);
expect(editor.columnEditor).toEqual(mockColumn.editor);
});
it('should call "setValue" and expect the DOM element value to be the same string when calling "getValue"', () => {
editor = new DateEditor(editorArguments);
editor.setValue('2001-01-02T11:02:02.000Z');
expect(editor.getValue()).toBe('2001-01-02T11:02:02.000Z');
});
it('should call "setValue" with value & apply value flag and expect the DOM element to have same value and also expect the value to be applied to the item object', () => {
mockColumn.type = FieldType.dateIso;
editor = new DateEditor(editorArguments);
editor.setValue('2001-01-02', true);
expect(editor.getValue()).toBe('2001-01-02');
expect(editorArguments.item.startDate).toBe('2001-01-02');
});
it('should define an item datacontext containing a string as cell value and expect this value to be loaded in the editor when calling "loadValue"', () => {
mockItemData = { id: 1, startDate: '2001-01-02T11:02:02.000Z', isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const editorElm = editor.editorDomElement;
expect(editor.getValue()).toBe('2001-01-02T11:02:02.000Z');
expect(editorElm.defaultValue).toBe('2001-01-02T11:02:02.000Z');
});
it('should hide the DOM element when the "hide" method is called', () => {
editor = new DateEditor(editorArguments);
const spy = jest.spyOn(editor.flatInstance, 'close');
const calendarElm = document.body.querySelector<HTMLDivElement>('.flatpickr-calendar');
editor.hide();
expect(calendarElm).toBeTruthy();
expect(spy).toHaveBeenCalled();
});
it('should show the DOM element when the "show" method is called', () => {
editor = new DateEditor(editorArguments);
const spy = jest.spyOn(editor.flatInstance, 'open');
const calendarElm = document.body.querySelector<HTMLDivElement>('.flatpickr-calendar');
editor.show();
editor.focus();
expect(gridStub.focus).toHaveBeenCalled();
expect(calendarElm).toBeTruthy();
expect(spy).toHaveBeenCalled();
});
it('should enable Dark Mode and expect ".slick-dark-mode" CSS class to be found on parent element', () => {
gridOptionMock.darkMode = true;
editor = new DateEditor(editorArguments);
const spy = jest.spyOn(editor.flatInstance, 'open');
const calendarElm = document.body.querySelector<HTMLDivElement>('.flatpickr-calendar');
editor.show();
editor.focus();
expect(gridStub.focus).toHaveBeenCalled();
expect(calendarElm?.classList.contains('slick-dark-mode')).toBeTruthy();
expect(spy).toHaveBeenCalled();
});
it('should call the "changeEditorOption" method and expect new option to be merged with the previous Editor options and also expect to call Flatpickr "set" method', () => {
editor = new DateEditor(editorArguments);
const spy = jest.spyOn(editor.flatInstance, 'set');
const calendarElm = document.body.querySelector<HTMLDivElement>('.flatpickr-calendar');
editor.changeEditorOption('minDate', 'today');
expect(calendarElm).toBeTruthy();
expect(spy).toHaveBeenCalledWith('minDate', 'today');
});
describe('isValueChanged method', () => {
it('should return True when date is changed in the picker', () => {
// change to allow input value only for testing purposes & use the regular flatpickr input to test that one too
mockColumn.editor!.editorOptions = { allowInput: true, altInput: false };
mockItemData = { id: 1, startDate: '2001-01-02T11:02:02.000Z', isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
editor.focus();
const editorInputElm = divContainer.querySelector('.flatpickr input') as HTMLInputElement;
editorInputElm.value = '2024-04-02T16:02:02.239Z';
editorInputElm.dispatchEvent(new (window.window as any).KeyboardEvent('keydown', { keyCode: 13, bubbles: true, cancelable: true }));
expect(editor.isValueChanged()).toBe(true);
expect(editor.isValueTouched()).toBe(true);
});
it('should return True when date is reset by the clear date button', () => {
// change to allow input value only for testing purposes & use the regular flatpickr input to test that one too
mockColumn.editor!.editorOptions = { allowInput: true, altInput: false };
mockItemData = { id: 1, startDate: '2001-01-02T11:02:02.000Z', isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
editor.focus();
const clearBtnElm = divContainer.querySelector('.btn.icon-clear') as HTMLInputElement;
const editorInputElm = divContainer.querySelector('.flatpickr input') as HTMLInputElement;
clearBtnElm.click();
editorInputElm.dispatchEvent(new (window.window as any).KeyboardEvent('keydown', { keyCode: 13, bubbles: true, cancelable: true }));
expect(editorInputElm.value).toBe('');
expect(editor.isValueChanged()).toBe(true);
expect(editor.isValueTouched()).toBe(true);
});
it('should also return True when date is reset by the clear date button even if the previous date was empty', () => {
// change to allow input value only for testing purposes & use the regular flatpickr input to test that one too
mockColumn.editor!.editorOptions = { allowInput: true, altInput: false };
mockItemData = { id: 1, startDate: '', isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
editor.focus();
const clearBtnElm = divContainer.querySelector('.btn.icon-clear') as HTMLInputElement;
const editorInputElm = divContainer.querySelector('.flatpickr input') as HTMLInputElement;
clearBtnElm.click();
expect(editorInputElm.value).toBe('');
expect(editor.isValueChanged()).toBe(true);
expect(editor.isValueTouched()).toBe(true);
});
it('should return False when date in the picker is the same as the current date', () => {
mockItemData = { id: 1, startDate: '2001-01-02T11:02:02.000Z', isActive: true };
mockColumn.editor!.editorOptions = { allowInput: true }; // change to allow input value only for testing purposes
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const editorInputElm = divContainer.querySelector('input.flatpickr-alt-input') as HTMLInputElement;
editorInputElm.value = '2001-01-02T11:02:02.000Z';
editorInputElm.dispatchEvent(new (window.window as any).KeyboardEvent('keydown', { keyCode: 13, bubbles: true, cancelable: true }));
expect(editor.isValueChanged()).toBe(false);
expect(editor.isValueTouched()).toBe(true);
});
it('should return False when input date is invalid', () => {
mockItemData = { id: 1, startDate: '1900-02-32', isActive: true };
mockColumn.type = FieldType.dateUs;
mockColumn.editor!.editorOptions = { allowInput: true }; // change to allow input value only for testing purposes
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const editorInputElm = divContainer.querySelector('input.flatpickr-alt-input') as HTMLInputElement;
editorInputElm.value = '1900-02-32';
editorInputElm.dispatchEvent(new (window.window as any).KeyboardEvent('keydown', { keyCode: 13, bubbles: true, cancelable: true }));
expect(editor.isValueChanged()).toBe(false);
expect(editor.isValueTouched()).toBe(true);
});
});
describe('applyValue method', () => {
it('should apply the value to the startDate property with ISO format when no "outputType" is defined and when it passes validation', () => {
mockColumn.editor!.validator = null as any;
mockColumn.type = FieldType.date;
mockItemData = { id: 1, startDate: '2001-04-05T11:33:42.000Z', isActive: true };
const newDate = new Date(Date.UTC(2001, 0, 2, 16, 2, 2, 0));
editor = new DateEditor(editorArguments);
editor.applyValue(mockItemData, newDate);
// @ts-ignore:2349
expect(mockItemData).toEqual({ id: 1, startDate: moment(newDate).format('YYYY-MM-DD'), isActive: true });
});
it('should apply the value to the startDate property with "outputType" format with a field having dot notation (complex object) that passes validation', () => {
mockColumn.editor!.validator = null as any;
mockColumn.type = FieldType.date;
mockColumn.outputType = FieldType.dateTimeIsoAmPm;
mockColumn.field = 'employee.startDate';
mockItemData = { id: 1, employee: { startDate: '2001-04-05T11:33:42.000Z' }, isActive: true };
const newDate = new Date(Date.UTC(2001, 0, 2, 16, 2, 2, 0));
editor = new DateEditor(editorArguments);
editor.applyValue(mockItemData, newDate);
// @ts-ignore:2349
expect(mockItemData).toEqual({ id: 1, employee: { startDate: moment(newDate).format('YYYY-MM-DD hh:mm:ss a') }, isActive: true });
});
it('should apply the value to the startDate property with output format defined by "saveOutputType" when it passes validation', () => {
mockColumn.editor!.validator = null as any;
mockColumn.type = FieldType.date;
mockColumn.saveOutputType = FieldType.dateTimeIsoAmPm;
mockItemData = { id: 1, startDate: '2001-04-05T11:33:42.000Z', isActive: true };
const newDate = new Date(Date.UTC(2001, 0, 2, 16, 2, 2, 0));
editor = new DateEditor(editorArguments);
editor.applyValue(mockItemData, newDate);
// @ts-ignore:2349
expect(mockItemData).toEqual({ id: 1, startDate: moment(newDate).format('YYYY-MM-DD hh:mm:ss a'), isActive: true });
});
it('should return item data with an empty string in its value when it fails the custom validation', () => {
mockColumn.editor!.validator = (value: any) => {
if (value.length > 10) {
return { valid: false, msg: 'Must be at least 10 chars long.' };
}
return { valid: true, msg: '' };
};
mockItemData = { id: 1, startDate: '2001-04-05T11:33:42.000Z', isActive: true };
editor = new DateEditor(editorArguments);
editor.applyValue(mockItemData, '2001-01-02T16:02:02.000+05:00');
expect(mockItemData).toEqual({ id: 1, startDate: '', isActive: true });
});
});
describe('serializeValue method', () => {
it('should return serialized value as a date string', () => {
mockColumn.type = FieldType.dateIso;
mockItemData = { id: 1, startDate: '2001-01-02T16:02:02.000+05:00', isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const output = editor.serializeValue();
expect(output).toBe('2001-01-02');
});
it('should return serialized value as an empty string when item value is also an empty string', () => {
mockItemData = { id: 1, startDate: '', isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const output = editor.serializeValue();
expect(output).toBe('');
});
it('should return serialized value as an empty string when item value is null', () => {
mockItemData = { id: 1, startDate: null, isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const output = editor.serializeValue();
expect(output).toBe('');
});
it('should return serialized value as a date string when using a dot (.) notation for complex object', () => {
mockColumn.type = FieldType.dateIso;
mockColumn.field = 'employee.startDate';
mockItemData = { id: 1, employee: { startDate: '2001-01-02T16:02:02.000+05:00' }, isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const output = editor.serializeValue();
expect(output).toBe('2001-01-02');
});
});
describe('save method', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should call "getEditorLock" method when "hasAutoCommitEdit" is enabled', () => {
mockItemData = { id: 1, startDate: '2001-01-02T16:02:02.000+05:00', isActive: true };
gridOptionMock.autoCommitEdit = true;
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue('2022-03-02T16:02:02.000+05:00');
editor.save();
expect(spy).toHaveBeenCalled();
});
it('should call "commitChanges" method when "hasAutoCommitEdit" is disabled', () => {
mockItemData = { id: 1, startDate: '2001-01-02T16:02:02.000+05:00', isActive: true };
gridOptionMock.autoCommitEdit = false;
const spy = jest.spyOn(editorArguments, 'commitChanges');
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue('2022-03-02T16:02:02.000+05:00');
editor.save();
expect(spy).toHaveBeenCalled();
});
it('should not call anything when the input value is empty but is required', () => {
mockItemData = { id: 1, startDate: '', isActive: true };
mockColumn.editor!.required = true;
gridOptionMock.autoCommitEdit = true;
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
editor.save();
expect(spy).not.toHaveBeenCalled();
});
it('should not throw any error when date is invalid when lower than required "minDate" defined in the "editorOptions" and "autoCommitEdit" is enabled', () => {
// change to allow input value only for testing purposes & use the regular flatpickr input to test that one too
mockColumn.editor!.editorOptions = { minDate: 'today', altInput: true };
mockItemData = { id: 1, startDate: '500-01-02T11:02:02.000Z', isActive: true };
gridOptionMock.autoCommitEdit = true;
gridOptionMock.autoEdit = true;
gridOptionMock.editable = true;
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
editor.flatInstance.toggle();
const editorInputElm = divContainer.querySelector('.flatpickr input') as HTMLInputElement;
expect(editor.pickerOptions).toBeTruthy();
expect(editorInputElm.value).toBe('');
expect(editor.serializeValue()).toBe('');
});
});
describe('validate method', () => {
it('should return False when field is required and field is empty', () => {
mockColumn.editor!.required = true;
editor = new DateEditor(editorArguments);
const validation = editor.validate(null, '');
expect(validation).toEqual({ valid: false, msg: 'Field is required' });
});
it('should return True when field is required and input is a valid input value', () => {
mockColumn.editor!.required = true;
editor = new DateEditor(editorArguments);
const validation = editor.validate(null, 'text');
expect(validation).toEqual({ valid: true, msg: null });
});
});
describe('with different locale', () => {
it('should display a console warning when locale is not previously imported', (done) => {
const consoleSpy = jest.spyOn(global.console, 'warn').mockReturnValue();
gridOptionMock.translater = translateService;
translateService.use('zz-yy'); // will be trimmed to 2 chars "zz"
editor = new DateEditor(editorArguments);
setTimeout(() => {
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining(`[Slickgrid-Universal] Flatpickr missing locale imports (zz), will revert to English as the default locale.`));
done();
});
});
it('should display text in new locale', async () => {
await (await import('flatpickr/dist/l10n/fr')).French;
gridOptionMock.translater = translateService;
translateService.use('fr');
editor = new DateEditor(editorArguments);
const spy = jest.spyOn(editor.flatInstance, 'open');
const calendarElm = document.body.querySelector('.flatpickr-calendar') as HTMLDivElement;
const selectonOptionElms = calendarElm.querySelectorAll<HTMLSelectElement>(' .flatpickr-monthDropdown-months option');
editor.show();
expect(calendarElm).toBeTruthy();
expect(selectonOptionElms.length).toBe(12);
expect(selectonOptionElms[0].textContent).toBe('janvier');
expect(spy).toHaveBeenCalled();
});
});
});
describe('with Composite Editor', () => {
beforeEach(() => {
editorArguments = {
...editorArguments,
compositeEditorOptions: { headerTitle: 'Test', modalType: 'edit', formValues: {}, editors: {} },
} as EditorArguments;
});
afterEach(() => {
jest.clearAllMocks();
});
it('should call "setValue" with value & apply value flag and expect the DOM element to have same value and also expect the value to be applied to the item object', () => {
const activeCellMock = { row: 0, cell: 0 };
jest.spyOn(gridStub, 'getActiveCell').mockReturnValue(activeCellMock);
const onCompositeEditorSpy = jest.spyOn(gridStub.onCompositeEditorChange, 'notify').mockReturnValue({
getReturnValue: () => false
} as any);
mockColumn.type = FieldType.dateIso;
editor = new DateEditor(editorArguments);
editor.setValue('2001-01-02', true);
expect(editor.getValue()).toContain('2001-01-02');
expect(onCompositeEditorSpy).toHaveBeenCalledWith({
...activeCellMock, column: mockColumn, item: mockItemData, grid: gridStub,
formValues: { startDate: '2001-01-02' }, editors: {}, triggeredBy: 'system',
}, expect.anything());
});
it('should call "show" and expect the DOM element to not be disabled when "onBeforeEditCell" is NOT returning false', () => {
const activeCellMock = { row: 0, cell: 0 };
const getCellSpy = jest.spyOn(gridStub, 'getActiveCell').mockReturnValue(activeCellMock);
const onBeforeEditSpy = jest.spyOn(gridStub.onBeforeEditCell, 'notify').mockReturnValue({
getReturnValue: () => undefined
} as any);
editor = new DateEditor(editorArguments);
const disableSpy = jest.spyOn(editor, 'disable');
editor.show();
expect(getCellSpy).toHaveBeenCalled();
expect(onBeforeEditSpy).toHaveBeenCalledWith({ ...activeCellMock, column: mockColumn, item: mockItemData, grid: gridStub, target: 'composite', compositeEditorOptions: editorArguments.compositeEditorOptions });
expect(disableSpy).toHaveBeenCalledWith(false);
});
it('should call "show" and expect the DOM element to become disabled with empty value set in the form values when "onBeforeEditCell" returns false', () => {
const activeCellMock = { row: 0, cell: 0 };
const getCellSpy = jest.spyOn(gridStub, 'getActiveCell').mockReturnValue(activeCellMock);
const onBeforeEditSpy = jest.spyOn(gridStub.onBeforeEditCell, 'notify').mockReturnValue({
getReturnValue: () => false
} as any);
const onCompositeEditorSpy = jest.spyOn(gridStub.onCompositeEditorChange, 'notify').mockReturnValue({
getReturnValue: () => false
} as any);
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const disableSpy = jest.spyOn(editor, 'disable');
editor.show();
expect(getCellSpy).toHaveBeenCalled();
expect(onBeforeEditSpy).toHaveBeenCalledWith({ ...activeCellMock, column: mockColumn, item: mockItemData, grid: gridStub, target: 'composite', compositeEditorOptions: editorArguments.compositeEditorOptions });
expect(onCompositeEditorSpy).toHaveBeenCalledWith({
...activeCellMock, column: mockColumn, item: mockItemData, grid: gridStub,
formValues: { startDate: '' }, editors: {}, triggeredBy: 'user',
}, expect.anything());
expect(disableSpy).toHaveBeenCalledWith(true);
expect(editor.flatInstance._input.disabled).toEqual(true);
expect(editor.flatInstance._input.value).toEqual('');
});
it('should call "show" and expect the DOM element to become disabled and empty when "onBeforeEditCell" returns false and also expect "onBeforeComposite" to not be called because the value is blank', () => {
const activeCellMock = { row: 0, cell: 0 };
const getCellSpy = jest.spyOn(gridStub, 'getActiveCell').mockReturnValue(activeCellMock);
const onBeforeEditSpy = jest.spyOn(gridStub.onBeforeEditCell, 'notify').mockReturnValue({
getReturnValue: () => false
} as any);
const onCompositeEditorSpy = jest.spyOn(gridStub.onCompositeEditorChange, 'notify').mockReturnValue({
getReturnValue: () => false
} as any);
gridOptionMock.compositeEditorOptions = {
excludeDisabledFieldFormValues: true
};
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
const disableSpy = jest.spyOn(editor, 'disable');
editor.show();
expect(getCellSpy).toHaveBeenCalled();
expect(onBeforeEditSpy).toHaveBeenCalledWith({ ...activeCellMock, column: mockColumn, item: mockItemData, grid: gridStub, target: 'composite', compositeEditorOptions: editorArguments.compositeEditorOptions });
expect(onCompositeEditorSpy).not.toHaveBeenCalled();
expect(disableSpy).toHaveBeenCalledWith(true);
expect(editor.flatInstance._input.disabled).toEqual(true);
expect(editor.flatInstance._input.value).toEqual('');
});
it('should call "disable" method and expect the DOM element to become disabled and have an empty formValues be passed in the onCompositeEditorChange event', () => {
const activeCellMock = { row: 0, cell: 0 };
const getCellSpy = jest.spyOn(gridStub, 'getActiveCell').mockReturnValue(activeCellMock);
const onCompositeEditorSpy = jest.spyOn(gridStub.onCompositeEditorChange, 'notify').mockReturnValue({
getReturnValue: () => false
} as any);
gridOptionMock.compositeEditorOptions = {
excludeDisabledFieldFormValues: true
};
editor = new DateEditor(editorArguments);
editor.loadValue({ ...mockItemData, startDate: '2020-01-01' });
editor.show();
editor.disable();
expect(getCellSpy).toHaveBeenCalled();
expect(onCompositeEditorSpy).toHaveBeenCalledWith({
...activeCellMock, column: mockColumn, item: mockItemData, grid: gridStub,
formValues: {}, editors: {}, triggeredBy: 'user',
}, expect.anything());
expect(editor.flatInstance._input.disabled).toEqual(true);
expect(editor.flatInstance._input.value).toEqual('');
});
it('should expect "onCompositeEditorChange" to have been triggered with the new value showing up in its "formValues" object', () => {
const activeCellMock = { row: 0, cell: 0 };
mockColumn.editor!.editorOptions = { allowInput: true, altInput: false };
mockColumn.type = FieldType.dateIso;
const getCellSpy = jest.spyOn(gridStub, 'getActiveCell').mockReturnValue(activeCellMock);
const onBeforeEditSpy = jest.spyOn(gridStub.onBeforeEditCell, 'notify').mockReturnValue({
getReturnValue: () => undefined
} as any);
const onCompositeEditorSpy = jest.spyOn(gridStub.onCompositeEditorChange, 'notify').mockReturnValue({
getReturnValue: () => false
} as any);
gridOptionMock.autoCommitEdit = true;
mockItemData = { id: 1, startDate: '2001-01-02', isActive: true };
editor = new DateEditor(editorArguments);
editor.loadValue(mockItemData);
editor.focus();
const editorInputElm = divContainer.querySelector('.flatpickr input') as HTMLInputElement;
editorInputElm.value = '2001-01-02';
editorInputElm.dispatchEvent(new (window.window as any).KeyboardEvent('keydown', { keyCode: 13, bubbles: true, cancelable: true }));
expect(getCellSpy).toHaveBeenCalled();
expect(onBeforeEditSpy).toHaveBeenCalledWith({ ...activeCellMock, column: mockColumn, item: mockItemData, grid: gridStub, target: 'composite', compositeEditorOptions: editorArguments.compositeEditorOptions });
expect(onCompositeEditorSpy).toHaveBeenCalledWith({
...activeCellMock, column: mockColumn, item: mockItemData, grid: gridStub,
formValues: { startDate: '2001-01-02' }, editors: {}, triggeredBy: 'user',
}, expect.anything());
});
});
});