Skip to content

Commit

Permalink
feat(tests): add missing unit tests for all Editors
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Oct 1, 2019
1 parent 9dc1abe commit c275b87
Show file tree
Hide file tree
Showing 19 changed files with 914 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,17 @@ describe('AutoCompleteEditor', () => {
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"', () => {
editor = new AutoCompleteEditor(editorArguments);
editor.loadValue(mockItemData);
const editorElm = editor.editorDomElement;

expect(editor.getValue()).toBe('male');
expect(editorElm[0].defaultValue).toBe('male');
});

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"', () => {
mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true };
mockColumn.field = 'gender.value';
editor = new AutoCompleteEditor(editorArguments);
editor.loadValue(mockItemData);
const editorElm = editor.editorDomElement;

expect(editor.getValue()).toBe('male');
expect(editorElm[0].defaultValue).toBe('male');
});

it('should dispatch a keyboard event and expect "stopImmediatePropagation()" to have been called when using Left Arrow key', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ describe('CheckboxEditor', () => {
mockColumn.internalColumnEditor.title = testValue;

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

expect(editorElm.title).toBe(testValue);
expect(editorElmJquery[0].title).toBe(testValue);
});

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

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

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

expect(editor.isValueChanged()).toBe(true);
expect(spy).toHaveBeenCalled();
});

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

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

editor = new CheckboxEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(true);
editor.save();

expect(spy).toHaveBeenCalled();
});

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

editor = new CheckboxEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(true);
editor.save();

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

editor = new CheckboxEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(false);
editor.save();

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

editor = new CheckboxEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(false);
editor.save();

expect(spy).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ describe('FloatEditor', () => {
expect(editorCount).toBe(1);
});

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

expect(editorCount).toBe(1);
setTimeout(() => {
expect(editorCount).toBe(1);
expect(spy).toHaveBeenCalled();
done();
}, 51);
});

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

expect(editor.getValue()).toBe('213');
expect(editorElm[0].defaultValue).toBe('213');
});

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

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

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

editor = new FloatEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(35);
editor.save();

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

editor = new FloatEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(35);
editor.save();

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

editor = new FloatEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue('.');
editor.save();

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

editor = new FloatEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(35);
const spySave = jest.spyOn(editor, 'save');
const editorElm = editor.editorDomElement;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ describe('IntegerEditor', () => {
expect(editorCount).toBe(1);
});

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

expect(editorCount).toBe(1);
setTimeout(() => {
expect(editorCount).toBe(1);
expect(spy).toHaveBeenCalled();
done();
}, 51);
});

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

expect(editor.getValue()).toBe('213');
expect(editorElm[0].defaultValue).toBe('213');
});

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

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

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

editor = new IntegerEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(35);
editor.save();

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

editor = new IntegerEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(35);
editor.save();

expect(spy).toHaveBeenCalled();
});

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

editor = new IntegerEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue('.2');
editor.save();

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

editor = new IntegerEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue(35);
const spySave = jest.spyOn(editor, 'save');
const editorElm = editor.editorDomElement;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ describe('LongTextEditor', () => {
});

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

editor = new LongTextEditor(editorArguments);
Expand Down Expand Up @@ -338,6 +338,7 @@ describe('LongTextEditor', () => {

editor = new LongTextEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue('task 1');
editor.save();

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

editor = new LongTextEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue('task 1');
editor.save();

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

editor = new LongTextEditor(editorArguments);
editor.loadValue(mockItemData);
editor.setValue('');
editor.save();

expect(spy).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { TestBed } from '@angular/core/testing';
import { TranslateService, TranslateModule } from '@ngx-translate/core';
import { Editors } from '../index';
import { MultipleSelectEditor } from '../multipleSelectEditor';
import { CollectionService } from '../../services/collection.service';
import { AutocompleteOption, Column, EditorArgs, EditorArguments, GridOption, KeyCode } from '../../models';
import { Column, EditorArguments, GridOption } from '../../models';

const KEY_CHAR_A = 97;
const containerId = 'demo-container';

// define a <div> container to simulate the grid container
Expand Down

0 comments on commit c275b87

Please sign in to comment.