|
| 1 | +import { Editors } from '../index'; |
| 2 | +import { AutoCompleteEditor } from '../autoCompleteEditor'; |
| 3 | +import { AutocompleteOption, Column, FieldType, EditorArguments, GridOption, OperatorType, KeyCode } from '../../models'; |
| 4 | + |
| 5 | +const containerId = 'demo-container'; |
| 6 | + |
| 7 | +// define a <div> container to simulate the grid container |
| 8 | +const template = `<div id="${containerId}"></div>`; |
| 9 | + |
| 10 | +const dataViewStub = { |
| 11 | + refresh: jest.fn(), |
| 12 | +}; |
| 13 | + |
| 14 | +const gridOptionMock = { |
| 15 | + enableeditoring: true, |
| 16 | + enableeditorTrimWhiteSpace: true, |
| 17 | +} as GridOption; |
| 18 | + |
| 19 | +const gridStub = { |
| 20 | + getOptions: () => gridOptionMock, |
| 21 | + getColumns: jest.fn(), |
| 22 | + getHeaderRowColumn: jest.fn(), |
| 23 | + render: jest.fn(), |
| 24 | +}; |
| 25 | + |
| 26 | +describe('AutoCompleteEditor', () => { |
| 27 | + let divContainer: HTMLDivElement; |
| 28 | + let editor: AutoCompleteEditor; |
| 29 | + let editorArguments: EditorArguments; |
| 30 | + let mockColumn: Column; |
| 31 | + let mockItemData: any; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + divContainer = document.createElement('div'); |
| 35 | + divContainer.innerHTML = template; |
| 36 | + document.body.appendChild(divContainer); |
| 37 | + |
| 38 | + mockColumn = { id: 'gender', field: 'gender', editable: true, editor: { model: Editors.autoComplete }, internalColumnEditor: {} } as Column; |
| 39 | + |
| 40 | + editorArguments = { |
| 41 | + grid: gridStub, |
| 42 | + column: mockColumn, |
| 43 | + item: mockItemData, |
| 44 | + event: null, |
| 45 | + cancelChanges: jest.fn(), |
| 46 | + commitChanges: jest.fn(), |
| 47 | + container: divContainer, |
| 48 | + columnMetaData: null, |
| 49 | + dataView: dataViewStub, |
| 50 | + gridPosition: { top: 0, left: 0, bottom: 10, right: 10, height: 100, width: 100, visible: true }, |
| 51 | + position: { top: 0, left: 0, bottom: 10, right: 10, height: 100, width: 100, visible: true }, |
| 52 | + }; |
| 53 | + }); |
| 54 | + |
| 55 | + describe('with invalid Editor instance', () => { |
| 56 | + it('should throw an error when trying to call init without any arguments', (done) => { |
| 57 | + try { |
| 58 | + editor = new AutoCompleteEditor(null); |
| 59 | + } catch (e) { |
| 60 | + expect(e.toString()).toContain(`[Angular-SlickGrid] Something is wrong with this grid, an Editor must always have valid arguments.`); |
| 61 | + done(); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('should throw an error when collection is not a valid array', (done) => { |
| 66 | + try { |
| 67 | + // @ts-ignore |
| 68 | + mockColumn.internalColumnEditor.collection = { hello: 'world' }; |
| 69 | + editor = new AutoCompleteEditor(editorArguments); |
| 70 | + } catch (e) { |
| 71 | + expect(e.toString()).toContain(`The "collection" passed to the Autocomplete Editor is not a valid array.`); |
| 72 | + done(); |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('with valid Editor instance', () => { |
| 78 | + beforeEach(() => { |
| 79 | + mockItemData = { id: 123, gender: 'male', isActive: true }; |
| 80 | + mockColumn = { id: 'gender', field: 'gender', editable: true, editor: { model: Editors.autoComplete }, internalColumnEditor: {} } as Column; |
| 81 | + mockColumn.internalColumnEditor.collection = [{ value: 'male', label: 'male' }, { value: 'female', label: 'female' }]; |
| 82 | + |
| 83 | + editorArguments.column = mockColumn; |
| 84 | + editorArguments.item = mockItemData; |
| 85 | + }); |
| 86 | + |
| 87 | + afterEach(() => { |
| 88 | + editor.destroy(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should initialize the editor', () => { |
| 92 | + editor = new AutoCompleteEditor(editorArguments); |
| 93 | + const editorCount = divContainer.querySelectorAll('input.editor-text.editor-gender').length; |
| 94 | + const autocompleteUlElms = document.body.querySelectorAll<HTMLUListElement>('ul.ui-autocomplete'); |
| 95 | + |
| 96 | + expect(autocompleteUlElms.length).toBe(1); |
| 97 | + expect(editorCount).toBe(1); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should initialize the editor even when user define his own editor options', () => { |
| 101 | + mockColumn.internalColumnEditor.editorOptions = { minLength: 3 } as AutocompleteOption; |
| 102 | + editor = new AutoCompleteEditor(editorArguments); |
| 103 | + const editorCount = divContainer.querySelectorAll('input.editor-text.editor-gender').length; |
| 104 | + const autocompleteUlElms = document.body.querySelectorAll<HTMLUListElement>('ul.ui-autocomplete'); |
| 105 | + |
| 106 | + expect(autocompleteUlElms.length).toBe(1); |
| 107 | + expect(editorCount).toBe(1); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should have a placeholder when defined in its column definition', () => { |
| 111 | + const testValue = 'test placeholder'; |
| 112 | + mockColumn.internalColumnEditor.placeholder = testValue; |
| 113 | + |
| 114 | + editor = new AutoCompleteEditor(editorArguments); |
| 115 | + const editorElm = divContainer.querySelector<HTMLInputElement>('input.editor-text.editor-gender'); |
| 116 | + |
| 117 | + expect(editorElm.placeholder).toBe(testValue); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should have a title (tooltip) when defined in its column definition', () => { |
| 121 | + const testValue = 'test title'; |
| 122 | + mockColumn.internalColumnEditor.title = testValue; |
| 123 | + |
| 124 | + editor = new AutoCompleteEditor(editorArguments); |
| 125 | + const editorElm = divContainer.querySelector<HTMLInputElement>('input.editor-text.editor-gender'); |
| 126 | + |
| 127 | + expect(editorElm.title).toBe(testValue); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should call "setValue" and expect the DOM element to have the same value when calling "getValue"', () => { |
| 131 | + editor = new AutoCompleteEditor(editorArguments); |
| 132 | + editor.setValue('male'); |
| 133 | + |
| 134 | + expect(editor.getValue()).toBe('male'); |
| 135 | + }); |
| 136 | + |
| 137 | + 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"', () => { |
| 138 | + editor = new AutoCompleteEditor(editorArguments); |
| 139 | + editor.loadValue(mockItemData); |
| 140 | + const editorElm = editor.editorDomElement; |
| 141 | + |
| 142 | + expect(editor.getValue()).toBe('male'); |
| 143 | + expect(editorElm[0].defaultValue).toBe('male'); |
| 144 | + }); |
| 145 | + |
| 146 | + 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"', () => { |
| 147 | + mockItemData = { id: 123, gender: { value: 'male', label: 'Male' }, isActive: true }; |
| 148 | + mockColumn.field = 'gender.value'; |
| 149 | + editor = new AutoCompleteEditor(editorArguments); |
| 150 | + editor.loadValue(mockItemData); |
| 151 | + const editorElm = editor.editorDomElement; |
| 152 | + |
| 153 | + expect(editor.getValue()).toBe('Male'); |
| 154 | + expect(editorElm[0].defaultValue).toBe('Male'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should dispatch a keyboard event and expect "stopImmediatePropagation()" to have been called when using Left Arrow key', () => { |
| 158 | + const event = new (window.window as any).KeyboardEvent('keydown', { keyCode: KeyCode.LEFT, bubbles: true, cancelable: true }); |
| 159 | + const spyEvent = jest.spyOn(event, 'stopImmediatePropagation'); |
| 160 | + |
| 161 | + editor = new AutoCompleteEditor(editorArguments); |
| 162 | + const editorElm = divContainer.querySelector<HTMLInputElement>('input.editor-gender'); |
| 163 | + |
| 164 | + editorElm.focus(); |
| 165 | + editorElm.dispatchEvent(event); |
| 166 | + |
| 167 | + expect(spyEvent).toHaveBeenCalled(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should dispatch a keyboard event and expect "stopImmediatePropagation()" to have been called when using Right Arrow key', () => { |
| 171 | + const event = new (window.window as any).KeyboardEvent('keydown', { keyCode: KeyCode.RIGHT, bubbles: true, cancelable: true }); |
| 172 | + const spyEvent = jest.spyOn(event, 'stopImmediatePropagation'); |
| 173 | + |
| 174 | + editor = new AutoCompleteEditor(editorArguments); |
| 175 | + const editorElm = divContainer.querySelector<HTMLInputElement>('input.editor-gender'); |
| 176 | + |
| 177 | + editorElm.focus(); |
| 178 | + editorElm.dispatchEvent(event); |
| 179 | + |
| 180 | + expect(spyEvent).toHaveBeenCalled(); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should render the DOM element with different key/value pair when user provide its own customStructure', () => { |
| 184 | + mockColumn.internalColumnEditor.collection = [{ option: 'male', text: 'Male' }, { option: 'female', text: 'Female' }]; |
| 185 | + mockColumn.internalColumnEditor.customStructure = { value: 'option', label: 'text' }; |
| 186 | + const event = new (window.window as any).KeyboardEvent('keydown', { keyCode: 109, bubbles: true, cancelable: true }); |
| 187 | + |
| 188 | + editor = new AutoCompleteEditor(editorArguments); |
| 189 | + const editorElm = divContainer.querySelector<HTMLInputElement>('input.editor-gender'); |
| 190 | + |
| 191 | + editorElm.focus(); |
| 192 | + editorElm.dispatchEvent(event); |
| 193 | + |
| 194 | + expect(editor.elementCollection).toEqual([{ value: 'male', label: 'Male' }, { value: 'female', label: 'Female' }]); |
| 195 | + }); |
| 196 | + }); |
| 197 | +}); |
0 commit comments