Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 01f53ed

Browse files
committed
fix(editors): complex objects should work with all editors
- add Jest unit tests for all Editors - increase code coverage for some Filters unit tests
1 parent d9dab94 commit 01f53ed

26 files changed

+3907
-528
lines changed

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

Lines changed: 117 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ describe('AutoCompleteEditor', () => {
156156
editor.loadValue(mockItemData);
157157
const editorElm = editor.editorDomElement;
158158

159-
expect(editor.getValue()).toBe('Male');
160-
expect(editorElm[0].defaultValue).toBe('Male');
159+
expect(editor.getValue()).toBe('male');
160+
expect(editorElm[0].defaultValue).toBe('male');
161161
});
162162

163163
it('should dispatch a keyboard event and expect "stopImmediatePropagation()" to have been called when using Left Arrow key', () => {
@@ -238,7 +238,7 @@ describe('AutoCompleteEditor', () => {
238238
expect(editor.isValueChanged()).toBe(true);
239239
});
240240

241-
it('should call "focus()" method and expect the DOM element to be focused and selected', async () => {
241+
it('should call "focus()" method and expect the DOM element to be focused and selected', () => {
242242
editor = new AutoCompleteEditor(editorArguments);
243243
const editorElm = editor.editorDomElement;
244244
const spy = jest.spyOn(editorElm, 'focus');
@@ -247,136 +247,157 @@ describe('AutoCompleteEditor', () => {
247247
expect(spy).toHaveBeenCalled();
248248
});
249249

250-
it('should return override the item data as an object found from the collection when calling "applyValue" that passes validation', () => {
251-
mockColumn.internalColumnEditor.validator = null;
252-
mockItemData = { id: 123, gender: 'female', isActive: true };
250+
describe('applyValue method', () => {
251+
it('should apply the value to the gender property when it passes validation', () => {
252+
mockColumn.internalColumnEditor.validator = null;
253+
mockItemData = { id: 123, gender: 'female', isActive: true };
253254

254-
editor = new AutoCompleteEditor(editorArguments);
255-
editor.applyValue(mockItemData, { value: 'female', label: 'female' });
255+
editor = new AutoCompleteEditor(editorArguments);
256+
editor.applyValue(mockItemData, { value: 'female', label: 'female' });
256257

257-
expect(mockItemData).toEqual({ id: 123, gender: { value: 'female', label: 'female' }, isActive: true });
258-
});
258+
expect(mockItemData).toEqual({ id: 123, gender: { value: 'female', label: 'female' }, isActive: true });
259+
});
259260

260-
it('should return override the item data as a string found from the collection when calling "applyValue" that passes validation', () => {
261-
mockColumn.internalColumnEditor.validator = null;
262-
mockColumn.internalColumnEditor.collection = ['male', 'female'];
263-
mockItemData = { id: 123, gender: 'female', isActive: true };
261+
it('should apply the value to the gender property with a field having dot notation (complex object) that passes validation', () => {
262+
mockColumn.internalColumnEditor.validator = null;
263+
mockColumn.field = 'user.gender';
264+
mockItemData = { id: 1, user: { gender: 'female' }, isActive: true };
264265

265-
editor = new AutoCompleteEditor(editorArguments);
266-
editor.applyValue(mockItemData, 'female');
266+
editor = new AutoCompleteEditor(editorArguments);
267+
editor.applyValue(mockItemData, { value: 'female', label: 'female' });
267268

268-
expect(mockItemData).toEqual({ id: 123, gender: 'female', isActive: true });
269-
});
269+
expect(mockItemData).toEqual({ id: 1, user: { gender: { value: 'female', label: 'female' } }, isActive: true });
270+
});
270271

271-
it('should return item data with an empty string in its value when calling "applyValue" which fails the custom validation', () => {
272-
mockColumn.internalColumnEditor.validator = (value: any, args: EditorArgs) => {
273-
if (value.label.length < 10) {
274-
return { valid: false, msg: 'Must be at least 10 chars long.' };
275-
}
276-
return { valid: true, msg: '' };
277-
};
278-
mockItemData = { id: 123, gender: 'female', isActive: true };
272+
it('should return override the item data as a string found from the collection that passes validation', () => {
273+
mockColumn.internalColumnEditor.validator = null;
274+
mockColumn.internalColumnEditor.collection = ['male', 'female'];
275+
mockItemData = { id: 123, gender: 'female', isActive: true };
279276

280-
editor = new AutoCompleteEditor(editorArguments);
281-
editor.applyValue(mockItemData, 'female');
277+
editor = new AutoCompleteEditor(editorArguments);
278+
editor.applyValue(mockItemData, 'female');
282279

283-
expect(mockItemData).toEqual({ id: 123, gender: '', isActive: true });
284-
});
280+
expect(mockItemData).toEqual({ id: 123, gender: 'female', isActive: true });
281+
});
285282

286-
it('should return DOM element value when "forceUserInput" is enabled and loaded value length is greater then minLength defined when calling "serializeValue"', () => {
287-
mockColumn.internalColumnEditor.editorOptions = { forceUserInput: true, };
288-
mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true };
283+
it('should return item data with an empty string in its value when calling "applyValue" which fails the custom validation', () => {
284+
mockColumn.internalColumnEditor.validator = (value: any, args: EditorArgs) => {
285+
if (value.label.length < 10) {
286+
return { valid: false, msg: 'Must be at least 10 chars long.' };
287+
}
288+
return { valid: true, msg: '' };
289+
};
290+
mockItemData = { id: 123, gender: 'female', isActive: true };
289291

290-
editor = new AutoCompleteEditor(editorArguments);
291-
editor.loadValue(mockItemData);
292-
editor.setValue('Female');
293-
const output = editor.serializeValue();
292+
editor = new AutoCompleteEditor(editorArguments);
293+
editor.applyValue(mockItemData, 'female');
294294

295-
expect(output).toBe('Female');
295+
expect(mockItemData).toEqual({ id: 123, gender: '', isActive: true });
296+
});
296297
});
297298

298-
it('should return DOM element value when "forceUserInput" is enabled and loaded value length is greater then custom minLength defined when calling "serializeValue"', () => {
299-
mockColumn.internalColumnEditor.editorOptions = { forceUserInput: true, minLength: 2 } as AutocompleteOption;
300-
mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true };
299+
describe('forceUserInput flag', () => {
300+
it('should return DOM element value when "forceUserInput" is enabled and loaded value length is greater then minLength defined when calling "serializeValue"', () => {
301+
mockColumn.internalColumnEditor.editorOptions = { forceUserInput: true, };
302+
mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true };
301303

302-
editor = new AutoCompleteEditor(editorArguments);
303-
editor.loadValue(mockItemData);
304-
editor.setValue('Female');
305-
const output = editor.serializeValue();
304+
editor = new AutoCompleteEditor(editorArguments);
305+
editor.loadValue(mockItemData);
306+
editor.setValue('Female');
307+
const output = editor.serializeValue();
306308

307-
expect(output).toBe('Female');
308-
});
309+
expect(output).toBe('Female');
310+
});
309311

310-
it('should return loaded value when "forceUserInput" is enabled and loaded value length is lower than minLength defined when calling "serializeValue"', () => {
311-
mockColumn.internalColumnEditor.editorOptions = { forceUserInput: true, };
312-
mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true };
312+
it('should return DOM element value when "forceUserInput" is enabled and loaded value length is greater then custom minLength defined when calling "serializeValue"', () => {
313+
mockColumn.internalColumnEditor.editorOptions = { forceUserInput: true, minLength: 2 } as AutocompleteOption;
314+
mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true };
313315

314-
editor = new AutoCompleteEditor(editorArguments);
315-
editor.loadValue(mockItemData);
316-
editor.setValue('F');
317-
const output = editor.serializeValue();
316+
editor = new AutoCompleteEditor(editorArguments);
317+
editor.loadValue(mockItemData);
318+
editor.setValue('Female');
319+
const output = editor.serializeValue();
318320

319-
expect(output).toBe('Male');
320-
});
321+
expect(output).toBe('Female');
322+
});
321323

322-
it('should return correct object value even when defining a "customStructure" when calling "serializeValue"', () => {
323-
mockColumn.internalColumnEditor.collection = [{ option: 'male', text: 'Male' }, { option: 'female', text: 'Female' }];
324-
mockColumn.internalColumnEditor.customStructure = { value: 'option', label: 'text' };
325-
mockItemData = { id: 123, gender: { option: 'female', text: 'Female' }, isActive: true };
324+
it('should return loaded value when "forceUserInput" is enabled and loaded value length is lower than minLength defined when calling "serializeValue"', () => {
325+
mockColumn.internalColumnEditor.editorOptions = { forceUserInput: true, };
326+
mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true };
326327

327-
editor = new AutoCompleteEditor(editorArguments);
328-
editor.loadValue(mockItemData);
329-
const output = editor.serializeValue();
328+
editor = new AutoCompleteEditor(editorArguments);
329+
editor.loadValue(mockItemData);
330+
editor.setValue('F');
331+
const output = editor.serializeValue();
330332

331-
expect(output).toBe('Female');
333+
expect(output).toBe('Male');
334+
});
332335
});
333336

334-
it('should return an object output when calling "serializeValue" with its column definition set to "FieldType.object"', () => {
335-
mockColumn.type = FieldType.object;
336-
mockColumn.internalColumnEditor.collection = [{ value: 'm', label: 'Male' }, { value: 'f', label: 'Female' }];
337-
mockItemData = { id: 123, gender: { value: 'f', label: 'Female' }, isActive: true };
337+
describe('serializeValue method', () => {
338+
it('should return correct object value even when defining a "customStructure" when calling "serializeValue"', () => {
339+
mockColumn.internalColumnEditor.collection = [{ option: 'male', text: 'Male' }, { option: 'female', text: 'Female' }];
340+
mockColumn.internalColumnEditor.customStructure = { value: 'option', label: 'text' };
341+
mockItemData = { id: 123, gender: { option: 'female', text: 'Female' }, isActive: true };
338342

339-
editor = new AutoCompleteEditor(editorArguments);
340-
editor.loadValue(mockItemData);
341-
const output = editor.serializeValue();
343+
editor = new AutoCompleteEditor(editorArguments);
344+
editor.loadValue(mockItemData);
345+
const output = editor.serializeValue();
342346

343-
expect(output).toEqual({ value: 'f', label: 'Female' });
344-
});
347+
expect(output).toBe('Female');
348+
});
345349

346-
it('should call "getEditorLock" when "hasAutoCommitEdit" is enabled after calling "save()" method', async () => {
347-
gridOptionMock.autoCommitEdit = true;
348-
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
350+
it('should return an object output when calling "serializeValue" with its column definition set to "FieldType.object"', () => {
351+
mockColumn.type = FieldType.object;
352+
mockColumn.internalColumnEditor.collection = [{ value: 'm', label: 'Male' }, { value: 'f', label: 'Female' }];
353+
mockItemData = { id: 123, gender: { value: 'f', label: 'Female' }, isActive: true };
349354

350-
editor = new AutoCompleteEditor(editorArguments);
351-
editor.save();
355+
editor = new AutoCompleteEditor(editorArguments);
356+
editor.loadValue(mockItemData);
357+
const output = editor.serializeValue();
352358

353-
expect(spy).toHaveBeenCalled();
359+
expect(output).toEqual({ value: 'f', label: 'Female' });
360+
});
354361
});
355362

356-
it('should call "commitChanges" when "hasAutoCommitEdit" is disabled after calling "save()" method', async () => {
357-
gridOptionMock.autoCommitEdit = false;
358-
const spy = jest.spyOn(editorArguments, 'commitChanges');
363+
describe('save method', () => {
364+
it('should call "getEditorLock" when "hasAutoCommitEdit" is enabled after calling "save()" method', () => {
365+
gridOptionMock.autoCommitEdit = true;
366+
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
359367

360-
editor = new AutoCompleteEditor(editorArguments);
361-
editor.save();
368+
editor = new AutoCompleteEditor(editorArguments);
369+
editor.save();
362370

363-
expect(spy).toHaveBeenCalled();
364-
});
371+
expect(spy).toHaveBeenCalled();
372+
});
365373

366-
it('should validate and return False when field is required and field is an empty string', () => {
367-
mockColumn.internalColumnEditor.required = true;
368-
editor = new AutoCompleteEditor(editorArguments);
369-
const validation = editor.validate('');
374+
it('should call "commitChanges" when "hasAutoCommitEdit" is disabled after calling "save()" method', () => {
375+
gridOptionMock.autoCommitEdit = false;
376+
const spy = jest.spyOn(editorArguments, 'commitChanges');
377+
378+
editor = new AutoCompleteEditor(editorArguments);
379+
editor.save();
370380

371-
expect(validation).toEqual({ valid: false, msg: 'Field is required' });
381+
expect(spy).toHaveBeenCalled();
382+
});
372383
});
373384

374-
it('should validate and return True when field is required and field a valid object', () => {
375-
mockColumn.internalColumnEditor.required = true;
376-
editor = new AutoCompleteEditor(editorArguments);
377-
const validation = editor.validate(mockItemData);
385+
describe('validate method', () => {
386+
it('should validate and return False when field is required and field is an empty string', () => {
387+
mockColumn.internalColumnEditor.required = true;
388+
editor = new AutoCompleteEditor(editorArguments);
389+
const validation = editor.validate('');
390+
391+
expect(validation).toEqual({ valid: false, msg: 'Field is required' });
392+
});
393+
394+
it('should validate and return True when field is required and field is a valid input value', () => {
395+
mockColumn.internalColumnEditor.required = true;
396+
editor = new AutoCompleteEditor(editorArguments);
397+
const validation = editor.validate('gender');
378398

379-
expect(validation).toEqual({ valid: true, msg: null });
399+
expect(validation).toEqual({ valid: true, msg: null });
400+
});
380401
});
381402

382403
describe('onSelect method', () => {

0 commit comments

Comments
 (0)