|
| 1 | +// import 3rd party lib multiple-select for the tests |
| 2 | +import '../../../../../assets/lib/multiple-select/multiple-select'; |
| 3 | + |
| 4 | +import { TestBed } from '@angular/core/testing'; |
| 5 | +import { TranslateService, TranslateModule } from '@ngx-translate/core'; |
| 6 | +import { Editors } from '../index'; |
| 7 | +import { MultipleSelectEditor } from '../multipleSelectEditor'; |
| 8 | +import { CollectionService } from '../../services/collection.service'; |
| 9 | +import { AutocompleteOption, Column, EditorArgs, EditorArguments, GridOption, KeyCode } from '../../models'; |
| 10 | + |
| 11 | +const KEY_CHAR_A = 97; |
| 12 | +const containerId = 'demo-container'; |
| 13 | + |
| 14 | +// define a <div> container to simulate the grid container |
| 15 | +const template = `<div id="${containerId}"></div>`; |
| 16 | + |
| 17 | +const dataViewStub = { |
| 18 | + refresh: jest.fn(), |
| 19 | +}; |
| 20 | + |
| 21 | +const gridOptionMock = { |
| 22 | + autoCommitEdit: false, |
| 23 | + editable: true, |
| 24 | + i18n: null, |
| 25 | +} as GridOption; |
| 26 | + |
| 27 | +const getEditorLockMock = { |
| 28 | + commitCurrentEdit: jest.fn(), |
| 29 | +}; |
| 30 | + |
| 31 | +const gridStub = { |
| 32 | + getOptions: () => gridOptionMock, |
| 33 | + getColumns: jest.fn(), |
| 34 | + getEditorLock: () => getEditorLockMock, |
| 35 | + getHeaderRowColumn: jest.fn(), |
| 36 | + navigateNext: jest.fn(), |
| 37 | + navigatePrev: jest.fn(), |
| 38 | + render: jest.fn(), |
| 39 | +}; |
| 40 | + |
| 41 | +describe('SelectEditor', () => { |
| 42 | + let divContainer: HTMLDivElement; |
| 43 | + let editor: MultipleSelectEditor; |
| 44 | + let editorArguments: EditorArguments; |
| 45 | + let mockColumn: Column; |
| 46 | + let mockItemData: any; |
| 47 | + let translate: TranslateService; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + divContainer = document.createElement('div'); |
| 51 | + divContainer.innerHTML = template; |
| 52 | + document.body.appendChild(divContainer); |
| 53 | + |
| 54 | + TestBed.configureTestingModule({ |
| 55 | + providers: [], |
| 56 | + imports: [TranslateModule.forRoot()] |
| 57 | + }); |
| 58 | + translate = TestBed.get(TranslateService); |
| 59 | + |
| 60 | + translate.setTranslation('en', { |
| 61 | + CANCEL: 'Cancel', |
| 62 | + SAVE: 'Save', |
| 63 | + }); |
| 64 | + translate.setTranslation('fr', { |
| 65 | + CANCEL: 'Annuler', |
| 66 | + SAVE: 'Sauvegarder', |
| 67 | + }); |
| 68 | + translate.setDefaultLang('fr'); |
| 69 | + |
| 70 | + mockColumn = { id: 'gender', field: 'gender', editable: true, editor: { model: Editors.multipleSelect }, internalColumnEditor: {} } as Column; |
| 71 | + |
| 72 | + editorArguments = { |
| 73 | + grid: gridStub, |
| 74 | + column: mockColumn, |
| 75 | + item: mockItemData, |
| 76 | + event: null, |
| 77 | + cancelChanges: jest.fn(), |
| 78 | + commitChanges: jest.fn(), |
| 79 | + container: divContainer, |
| 80 | + columnMetaData: null, |
| 81 | + dataView: dataViewStub, |
| 82 | + gridPosition: { top: 0, left: 0, bottom: 10, right: 10, height: 100, width: 100, visible: true }, |
| 83 | + position: { top: 0, left: 0, bottom: 10, right: 10, height: 100, width: 100, visible: true }, |
| 84 | + }; |
| 85 | + }); |
| 86 | + |
| 87 | + describe('with valid Editor instance', () => { |
| 88 | + beforeEach(() => { |
| 89 | + mockItemData = { id: 1, gender: 'male', isActive: true }; |
| 90 | + mockColumn = { id: 'gender', field: 'gender', editable: true, editor: { model: Editors.multipleSelect }, internalColumnEditor: {} } as Column; |
| 91 | + mockColumn.internalColumnEditor.collection = [{ value: 'male', label: 'male' }, { value: 'female', label: 'female' }]; |
| 92 | + |
| 93 | + editorArguments.column = mockColumn; |
| 94 | + editorArguments.item = mockItemData; |
| 95 | + }); |
| 96 | + |
| 97 | + afterEach(() => { |
| 98 | + editor.destroy(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should initialize the editor', (done) => { |
| 102 | + mockColumn.internalColumnEditor.collection = [{ value: 'male', label: 'male' }, { value: 'female', label: 'female' }]; |
| 103 | + gridOptionMock.i18n = translate; |
| 104 | + editor = new MultipleSelectEditor(editorArguments); |
| 105 | + const editorCount = document.body.querySelectorAll('select.ms-filter.editor-gender').length; |
| 106 | + const spy = jest.spyOn(editor, 'show'); |
| 107 | + |
| 108 | + setTimeout(() => { |
| 109 | + expect(spy).toHaveBeenCalled(); |
| 110 | + expect(editorCount).toBe(1); |
| 111 | + done(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should call "setValue" with a single string and expect the string to be returned as an array when calling "getValue"', () => { |
| 116 | + editor = new MultipleSelectEditor(editorArguments); |
| 117 | + editor.setValue(['male']); |
| 118 | + |
| 119 | + expect(editor.getValue()).toEqual(['male']); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should hide the DOM element div wrapper when the "hide" method is called', () => { |
| 123 | + editor = new MultipleSelectEditor(editorArguments); |
| 124 | + const editorElm = document.body.querySelector<HTMLDivElement>('[name=editor-gender].ms-drop'); |
| 125 | + |
| 126 | + editor.show(); |
| 127 | + expect(editorElm.style.display).toBe(''); |
| 128 | + |
| 129 | + editor.hide(); |
| 130 | + expect(editorElm.style.display).toBe('none'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should show the DOM element div wrapper when the "show" method is called', () => { |
| 134 | + editor = new MultipleSelectEditor(editorArguments); |
| 135 | + const editorElm = document.body.querySelector<HTMLDivElement>('[name=editor-gender].ms-drop'); |
| 136 | + |
| 137 | + editor.hide(); |
| 138 | + expect(editorElm.style.display).toBe('none'); |
| 139 | + |
| 140 | + editor.show(); |
| 141 | + expect(editorElm.style.display).toBe(''); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments