Skip to content

Commit c275b87

Browse files
committed
feat(tests): add missing unit tests for all Editors
1 parent 9dc1abe commit c275b87

19 files changed

+914
-136
lines changed

src/app/modules/angular-slickgrid/editors/__tests__/autoCompleteEditor.spec.ts

-4
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,17 @@ describe('AutoCompleteEditor', () => {
143143
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"', () => {
144144
editor = new AutoCompleteEditor(editorArguments);
145145
editor.loadValue(mockItemData);
146-
const editorElm = editor.editorDomElement;
147146

148147
expect(editor.getValue()).toBe('male');
149-
expect(editorElm[0].defaultValue).toBe('male');
150148
});
151149

152150
it('should define an item datacontext containing a complex object as cell value and expect this value to be loaded in the editor when calling "loadValue"', () => {
153151
mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true };
154152
mockColumn.field = 'gender.value';
155153
editor = new AutoCompleteEditor(editorArguments);
156154
editor.loadValue(mockItemData);
157-
const editorElm = editor.editorDomElement;
158155

159156
expect(editor.getValue()).toBe('male');
160-
expect(editorElm[0].defaultValue).toBe('male');
161157
});
162158

163159
it('should dispatch a keyboard event and expect "stopImmediatePropagation()" to have been called when using Left Arrow key', () => {

src/app/modules/angular-slickgrid/editors/__tests__/checkboxEditor.spec.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ describe('CheckboxEditor', () => {
101101
mockColumn.internalColumnEditor.title = testValue;
102102

103103
editor = new CheckboxEditor(editorArguments);
104+
const editorElmJquery = editor.editorDomElement;
104105
const editorElm = divContainer.querySelector<HTMLInputElement>('input.editor-checkbox.editor-isActive');
105106

106107
expect(editorElm.title).toBe(testValue);
108+
expect(editorElmJquery[0].title).toBe(testValue);
107109
});
108110

109111
it('should call "columnEditor" GETTER and expect to equal the editor settings we provided', () => {
@@ -146,13 +148,16 @@ describe('CheckboxEditor', () => {
146148

147149
describe('isValueChanged method', () => {
148150
it('should return True when previous event is a click event', () => {
151+
gridOptionMock.autoCommitEdit = true;
149152
editor = new CheckboxEditor(editorArguments);
153+
const spy = jest.spyOn(editor, 'save');
150154
editor.loadValue({ id: 2, title: 'task 1', isActive: true });
151155

152-
const editorElm = editor.editorDomElement;
153-
editorElm.click();
156+
const editorElm = divContainer.querySelector<HTMLInputElement>('input.editor-isActive');
157+
editorElm.dispatchEvent(new (window.window as any).CustomEvent('click'));
154158

155159
expect(editor.isValueChanged()).toBe(true);
160+
expect(spy).toHaveBeenCalled();
156161
});
157162

158163
it('should return False when previous event is not a click event', () => {
@@ -261,24 +266,26 @@ describe('CheckboxEditor', () => {
261266
});
262267

263268
it('should call "getEditorLock" method when "hasAutoCommitEdit" is enabled', () => {
264-
mockItemData = { id: 1, title: 'task', isActive: true };
269+
mockItemData = { id: 1, title: 'task', isActive: false };
265270
gridOptionMock.autoCommitEdit = true;
266271
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
267272

268273
editor = new CheckboxEditor(editorArguments);
269274
editor.loadValue(mockItemData);
275+
editor.setValue(true);
270276
editor.save();
271277

272278
expect(spy).toHaveBeenCalled();
273279
});
274280

275281
it('should call "commitChanges" method when "hasAutoCommitEdit" is disabled', () => {
276-
mockItemData = { id: 1, title: 'task', isActive: true };
282+
mockItemData = { id: 1, title: 'task', isActive: false };
277283
gridOptionMock.autoCommitEdit = false;
278284
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
279285

280286
editor = new CheckboxEditor(editorArguments);
281287
editor.loadValue(mockItemData);
288+
editor.setValue(true);
282289
editor.save();
283290

284291
expect(spy).not.toHaveBeenCalled();
@@ -292,6 +299,7 @@ describe('CheckboxEditor', () => {
292299

293300
editor = new CheckboxEditor(editorArguments);
294301
editor.loadValue(mockItemData);
302+
editor.setValue(false);
295303
editor.save();
296304

297305
expect(spy).not.toHaveBeenCalled();
@@ -305,6 +313,7 @@ describe('CheckboxEditor', () => {
305313

306314
editor = new CheckboxEditor(editorArguments);
307315
editor.loadValue(mockItemData);
316+
editor.setValue(false);
308317
editor.save();
309318

310319
expect(spy).not.toHaveBeenCalled();

src/app/modules/angular-slickgrid/editors/__tests__/floatEditor.spec.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,16 @@ describe('FloatEditor', () => {
8888
expect(editorCount).toBe(1);
8989
});
9090

91-
it('should initialize the editor even when user define his own editor options', () => {
92-
mockColumn.internalColumnEditor.editorOptions = { minLength: 3 } as AutocompleteOption;
91+
it('should initialize the editor and focus on the element after a small delay', (done) => {
92+
const spy = jest.spyOn(editor, 'focus');
9393
editor = new FloatEditor(editorArguments);
9494
const editorCount = divContainer.querySelectorAll('input.editor-text.editor-price').length;
9595

96-
expect(editorCount).toBe(1);
96+
setTimeout(() => {
97+
expect(editorCount).toBe(1);
98+
expect(spy).toHaveBeenCalled();
99+
done();
100+
}, 51);
97101
});
98102

99103
it('should have a placeholder when defined in its column definition', () => {
@@ -141,7 +145,6 @@ describe('FloatEditor', () => {
141145
const editorElm = editor.editorDomElement;
142146

143147
expect(editor.getValue()).toBe('213');
144-
expect(editorElm[0].defaultValue).toBe('213');
145148
});
146149

147150
it('should dispatch a keyboard event and expect "stopImmediatePropagation()" to have been called when using Left Arrow key', () => {
@@ -171,7 +174,7 @@ describe('FloatEditor', () => {
171174
});
172175

173176
describe('isValueChanged method', () => {
174-
it('should return True when previously dispatched keyboard event being char 0', () => {
177+
it('should return True when previously dispatched keyboard event is a new char 0', () => {
175178
const event = new (window.window as any).KeyboardEvent('keydown', { keyCode: KEY_CHAR_0, bubbles: true, cancelable: true });
176179

177180
editor = new FloatEditor(editorArguments);
@@ -382,6 +385,7 @@ describe('FloatEditor', () => {
382385

383386
editor = new FloatEditor(editorArguments);
384387
editor.loadValue(mockItemData);
388+
editor.setValue(35);
385389
editor.save();
386390

387391
expect(spy).toHaveBeenCalled();
@@ -394,6 +398,7 @@ describe('FloatEditor', () => {
394398

395399
editor = new FloatEditor(editorArguments);
396400
editor.loadValue(mockItemData);
401+
editor.setValue(35);
397402
editor.save();
398403

399404
expect(spy).toHaveBeenCalled();
@@ -406,6 +411,7 @@ describe('FloatEditor', () => {
406411

407412
editor = new FloatEditor(editorArguments);
408413
editor.loadValue(mockItemData);
414+
editor.setValue('.');
409415
editor.save();
410416

411417
expect(spy).not.toHaveBeenCalled();
@@ -418,6 +424,7 @@ describe('FloatEditor', () => {
418424

419425
editor = new FloatEditor(editorArguments);
420426
editor.loadValue(mockItemData);
427+
editor.setValue(35);
421428
const spySave = jest.spyOn(editor, 'save');
422429
const editorElm = editor.editorDomElement;
423430

src/app/modules/angular-slickgrid/editors/__tests__/integerEditor.spec.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,16 @@ describe('IntegerEditor', () => {
8888
expect(editorCount).toBe(1);
8989
});
9090

91-
it('should initialize the editor even when user define his own editor options', () => {
92-
mockColumn.internalColumnEditor.editorOptions = { minLength: 3 } as AutocompleteOption;
91+
it('should initialize the editor and focus on the element after a small delay', (done) => {
92+
const spy = jest.spyOn(editor, 'focus');
9393
editor = new IntegerEditor(editorArguments);
9494
const editorCount = divContainer.querySelectorAll('input.editor-text.editor-price').length;
9595

96-
expect(editorCount).toBe(1);
96+
setTimeout(() => {
97+
expect(editorCount).toBe(1);
98+
expect(spy).toHaveBeenCalled();
99+
done();
100+
}, 51);
97101
});
98102

99103
it('should have a placeholder when defined in its column definition', () => {
@@ -141,7 +145,6 @@ describe('IntegerEditor', () => {
141145
const editorElm = editor.editorDomElement;
142146

143147
expect(editor.getValue()).toBe('213');
144-
expect(editorElm[0].defaultValue).toBe('213');
145148
});
146149

147150
it('should dispatch a keyboard event and expect "stopImmediatePropagation()" to have been called when using Left Arrow key', () => {
@@ -171,7 +174,7 @@ describe('IntegerEditor', () => {
171174
});
172175

173176
describe('isValueChanged method', () => {
174-
it('should return True when previously dispatched keyboard event being char 0', () => {
177+
it('should return True when previously dispatched keyboard event is a new char 0', () => {
175178
const event = new (window.window as any).KeyboardEvent('keydown', { keyCode: KEY_CHAR_0, bubbles: true, cancelable: true });
176179

177180
editor = new IntegerEditor(editorArguments);
@@ -346,6 +349,7 @@ describe('IntegerEditor', () => {
346349

347350
editor = new IntegerEditor(editorArguments);
348351
editor.loadValue(mockItemData);
352+
editor.setValue(35);
349353
editor.save();
350354

351355
expect(spy).toHaveBeenCalled();
@@ -358,18 +362,20 @@ describe('IntegerEditor', () => {
358362

359363
editor = new IntegerEditor(editorArguments);
360364
editor.loadValue(mockItemData);
365+
editor.setValue(35);
361366
editor.save();
362367

363368
expect(spy).toHaveBeenCalled();
364369
});
365370

366371
it('should not call anything when the input value is not a valid integer', () => {
367-
mockItemData = { id: 1, price: '.2', isActive: true };
372+
mockItemData = { id: 1, price: '.1', isActive: true };
368373
gridOptionMock.autoCommitEdit = true;
369374
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
370375

371376
editor = new IntegerEditor(editorArguments);
372377
editor.loadValue(mockItemData);
378+
editor.setValue('.2');
373379
editor.save();
374380

375381
expect(spy).not.toHaveBeenCalled();
@@ -382,6 +388,7 @@ describe('IntegerEditor', () => {
382388

383389
editor = new IntegerEditor(editorArguments);
384390
editor.loadValue(mockItemData);
391+
editor.setValue(35);
385392
const spySave = jest.spyOn(editor, 'save');
386393
const editorElm = editor.editorDomElement;
387394

src/app/modules/angular-slickgrid/editors/__tests__/longTextEditor.spec.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ describe('LongTextEditor', () => {
207207
});
208208

209209
describe('isValueChanged method', () => {
210-
it('should return True when previously dispatched keyboard event being char "a"', () => {
210+
it('should return True when previously dispatched keyboard event is a new char "a"', () => {
211211
const event = new (window.window as any).KeyboardEvent('keydown', { keyCode: KEY_CHAR_A, bubbles: true, cancelable: true });
212212

213213
editor = new LongTextEditor(editorArguments);
@@ -338,6 +338,7 @@ describe('LongTextEditor', () => {
338338

339339
editor = new LongTextEditor(editorArguments);
340340
editor.loadValue(mockItemData);
341+
editor.setValue('task 1');
341342
editor.save();
342343

343344
expect(spy).toHaveBeenCalled();
@@ -350,6 +351,7 @@ describe('LongTextEditor', () => {
350351

351352
editor = new LongTextEditor(editorArguments);
352353
editor.loadValue(mockItemData);
354+
editor.setValue('task 1');
353355
editor.save();
354356

355357
expect(spy).toHaveBeenCalled();
@@ -363,6 +365,7 @@ describe('LongTextEditor', () => {
363365

364366
editor = new LongTextEditor(editorArguments);
365367
editor.loadValue(mockItemData);
368+
editor.setValue('');
366369
editor.save();
367370

368371
expect(spy).not.toHaveBeenCalled();

src/app/modules/angular-slickgrid/editors/__tests__/multipleSelectEditor.spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import { TestBed } from '@angular/core/testing';
55
import { TranslateService, TranslateModule } from '@ngx-translate/core';
66
import { Editors } from '../index';
77
import { MultipleSelectEditor } from '../multipleSelectEditor';
8-
import { CollectionService } from '../../services/collection.service';
9-
import { AutocompleteOption, Column, EditorArgs, EditorArguments, GridOption, KeyCode } from '../../models';
8+
import { Column, EditorArguments, GridOption } from '../../models';
109

11-
const KEY_CHAR_A = 97;
1210
const containerId = 'demo-container';
1311

1412
// define a <div> container to simulate the grid container

0 commit comments

Comments
 (0)