forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinput-suggestions.component.spec.ts
More file actions
351 lines (299 loc) · 11.8 KB
/
Copy pathinput-suggestions.component.spec.ts
File metadata and controls
351 lines (299 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import {
ChangeDetectionStrategy,
DebugElement,
NO_ERRORS_SCHEMA,
} from '@angular/core';
import {
ComponentFixture,
TestBed,
waitForAsync,
} from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { InputSuggestionsComponent } from './input-suggestions.component';
describe('InputSuggestionsComponent', () => {
let comp: InputSuggestionsComponent;
let fixture: ComponentFixture<InputSuggestionsComponent>;
let de: DebugElement;
let el: HTMLElement;
let suggestions;
beforeEach(waitForAsync(() => {
suggestions = [{ displayValue: 'suggestion uno', value: 'suggestion uno' }, {
displayValue: 'suggestion dos',
value: 'suggestion dos',
}, { displayValue: 'suggestion tres', value: 'suggestion tres' }];
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NoopAnimationsModule, FormsModule, InputSuggestionsComponent],
providers: [],
schemas: [NO_ERRORS_SCHEMA],
}).overrideComponent(InputSuggestionsComponent, {
set: { changeDetection: ChangeDetectionStrategy.Default },
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(InputSuggestionsComponent);
comp = fixture.componentInstance; // LoadingComponent test instance
comp.suggestions = suggestions;
// query for the message <label> by CSS element selector
de = fixture.debugElement;
el = de.nativeElement;
comp.show.next(true);
fixture.detectChanges();
});
it('should create', () => {
expect(comp).toBeTruthy();
});
describe('the accessible name of the suggestion input', () => {
beforeEach(() => {
comp.name = 'author';
comp.placeholder = 'Search for an author';
fixture.detectChanges();
});
it('should label the input with a label that resolves to it', () => {
const input: HTMLInputElement = el.querySelector('input.suggestion_input');
const label: HTMLLabelElement = el.querySelector('label');
expect(input.id).toBeTruthy();
expect(label).toBeTruthy();
// a placeholder is not a label: the association has to be real
expect(label.htmlFor).toEqual(input.id);
expect(label.textContent.trim()).toEqual('Search for an author');
// and the label must not be rendered visibly (Bootstrap 5 dropped the old screen-reader class)
expect(Array.from(label.classList)).toContain('visually-hidden');
});
it('should derive the id from the name input so two instances do not collide', () => {
const input: HTMLInputElement = el.querySelector('input.suggestion_input');
const firstId: string = input.id;
comp.name = 'subject';
fixture.detectChanges();
const label: HTMLLabelElement = el.querySelector('label');
expect(input.id).not.toEqual(firstId);
expect(label.htmlFor).toEqual(input.id);
});
});
describe('when the input field is in focus', () => {
beforeEach(() => {
const inputElement = de.query(By.css('.suggestion_input'));
inputElement.nativeElement.focus();
fixture.detectChanges();
});
it('should not have any element in focus', () => {
const activeElement = el.ownerDocument.activeElement;
expect(activeElement.nodeName.toLowerCase()).not.toEqual('a');
});
describe('when key up is pressed', () => {
beforeEach(() => {
spyOn(comp, 'shiftFocusUp');
const form = de.query(By.css('form'));
form.triggerEventHandler('keydown.arrowup', {});
fixture.detectChanges();
});
it('should call shiftFocusUp()', () => {
expect(comp.shiftFocusUp).toHaveBeenCalled();
});
});
describe('when shiftFocusUp() is triggered', () => {
beforeEach(() => {
comp.shiftFocusUp(new KeyboardEvent('keydown.arrowup'));
fixture.detectChanges();
});
it('should put the focus on the last element ', () => {
const lastLink = de.query(By.css('.dropdown-list > div:last-child a'));
const activeElement = el.ownerDocument.activeElement;
expect(activeElement).toEqual(lastLink.nativeElement);
});
});
describe('when key down is pressed', () => {
beforeEach(() => {
spyOn(comp, 'shiftFocusDown');
const form = de.query(By.css('form'));
form.triggerEventHandler('keydown.arrowdown', {});
fixture.detectChanges();
});
it('should call shiftFocusDown()', () => {
expect(comp.shiftFocusDown).toHaveBeenCalled();
});
});
describe('when shiftFocusDown() is triggered', () => {
beforeEach(() => {
comp.shiftFocusDown(new KeyboardEvent('keydown.arrowdown'));
fixture.detectChanges();
});
it('should put the focus on the first element ', () => {
const firstLink = de.query(By.css('.dropdown-list > div:first-child a'));
const activeElement = el.ownerDocument.activeElement;
expect(activeElement).toEqual(firstLink.nativeElement);
});
});
describe('when changeFocus() is triggered when selectedIndex is 1', () => {
beforeEach(() => {
comp.selectedIndex = 1;
comp.changeFocus();
fixture.detectChanges();
});
it('should put the focus on the second element', () => {
const secondLink = de.query(By.css('.dropdown-list > div:nth-child(2) a'));
const activeElement = el.ownerDocument.activeElement;
expect(activeElement).toEqual(secondLink.nativeElement);
});
});
});
describe('when the first element is in focus', () => {
beforeEach(() => {
const firstLink = de.query(By.css('.dropdown-list > div:first-child a'));
firstLink.nativeElement.focus();
comp.selectedIndex = 0;
fixture.detectChanges();
});
describe('when shiftFocusUp() is triggered', () => {
beforeEach(() => {
comp.shiftFocusUp(new KeyboardEvent('keydown.arrowup'));
fixture.detectChanges();
});
it('should put the focus on the last element ', () => {
const lastLink = de.query(By.css('.dropdown-list > div:last-child a'));
const activeElement = el.ownerDocument.activeElement;
expect(activeElement).toEqual(lastLink.nativeElement);
});
});
describe('when shiftFocusDown() is triggered', () => {
beforeEach(() => {
comp.shiftFocusDown(new KeyboardEvent('keydown.arrowdown'));
fixture.detectChanges();
});
it('should put the focus on the second element ', () => {
const secondLink = de.query(By.css('.dropdown-list > div:nth-child(2) a'));
const activeElement = el.ownerDocument.activeElement;
expect(activeElement).toEqual(secondLink.nativeElement);
});
});
});
describe('when the last element is in focus', () => {
beforeEach(() => {
const lastLink = de.query(By.css('.dropdown-list > div:last-child a'));
lastLink.nativeElement.focus();
comp.selectedIndex = suggestions.length - 1;
fixture.detectChanges();
});
describe('when shiftFocusUp() is triggered', () => {
beforeEach(() => {
comp.shiftFocusUp(new KeyboardEvent('keydown.arrowup'));
fixture.detectChanges();
});
it('should put the focus on the second last element ', () => {
const secondLastLink = de.query(By.css('.dropdown-list > div:nth-last-child(2) a'));
const activeElement = el.ownerDocument.activeElement;
expect(activeElement).toEqual(secondLastLink.nativeElement);
});
});
describe('when shiftFocusDown() is triggered', () => {
beforeEach(() => {
comp.shiftFocusDown(new KeyboardEvent('keydown.arrowdown'));
fixture.detectChanges();
});
it('should put the focus on the first element ', () => {
const firstLink = de.query(By.css('.dropdown-list > div:first-child a'));
const activeElement = el.ownerDocument.activeElement;
expect(activeElement).toEqual(firstLink.nativeElement);
});
});
describe('when any key but is pressed in the form', () => {
beforeEach(() => {
spyOn(comp, 'onKeydown');
const form = de.query(By.css('form'));
form.triggerEventHandler('keydown', { key: 'Shift' });
fixture.detectChanges();
});
it('should call onKeydown', () => {
expect(comp.onKeydown).toHaveBeenCalled();
fixture.detectChanges();
});
});
describe('when onKeydown is triggered with the Enter key', () => {
beforeEach(() => {
spyOn(comp.queryInput.nativeElement, 'focus');
comp.onKeydown(new KeyboardEvent('keydown', { key: 'Enter' }));
fixture.detectChanges();
});
it('should not change the focus', () => {
expect(comp.queryInput.nativeElement.focus).not.toHaveBeenCalled();
});
});
describe('when onKeydown is triggered with the any other (not-Enter) key', () => {
beforeEach(() => {
spyOn(comp.queryInput.nativeElement, 'focus');
comp.onKeydown(new KeyboardEvent('keydown', { key: 'Shift' }));
fixture.detectChanges();
});
it('should change the focus', () => {
expect(comp.queryInput.nativeElement.focus).toHaveBeenCalled();
});
});
});
describe('when the suggestions list is not empty and show is true', () => {
beforeEach(() => {
comp.show.next(true);
fixture.detectChanges();
});
it('should contain an .autocomplete list with a \'show\' class', () => {
const autocomplete = de.query(By.css('div.autocomplete'));
expect(autocomplete.nativeElement.classList).toContain('show');
});
});
describe('when the suggestions list is not empty and show is false', () => {
beforeEach(() => {
comp.show.next(false);
fixture.detectChanges();
});
it('should contain an .autocomplete list without a \'show\' class', () => {
const autocomplete = de.query(By.css('div.autocomplete'));
expect(autocomplete.nativeElement.classList).not.toContain('show');
});
});
describe('when the suggestions list is empty and show is false', () => {
beforeEach(() => {
comp.suggestions = [];
comp.show.next(false);
fixture.detectChanges();
});
it('should contain an .autocomplete list without a \'show\' class', () => {
const autocomplete = de.query(By.css('div.autocomplete'));
expect(autocomplete.nativeElement.classList).not.toContain('show');
});
});
describe('when the suggestions list is empty and show is true', () => {
beforeEach(() => {
comp.suggestions = [];
comp.show.next(true);
fixture.detectChanges();
});
it('should contain an .autocomplete list without a \'show\' class', () => {
const autocomplete = de.query(By.css('div.autocomplete'));
expect(autocomplete.nativeElement.classList).not.toContain('show');
});
});
describe('when the variable \'show\' is set to true and close() is called', () => {
beforeEach(() => {
comp.show.next(true);
comp.close();
fixture.detectChanges();
});
it('should set \'show\' to false', () => {
expect(comp.show.getValue()).toBeFalsy();
});
});
describe('when an element is clicked', () => {
const clickedIndex = 0;
beforeEach(() => {
spyOn(comp, 'onClickSuggestion');
const clickedLink = de.query(By.css('.dropdown-list > div:nth-child(' + (clickedIndex + 1) + ') a'));
clickedLink.triggerEventHandler('click', {});
fixture.detectChanges();
});
it('should call onClickSuggestion() with the suggestion as a parameter', () => {
expect(comp.onClickSuggestion).toHaveBeenCalledWith(suggestions[clickedIndex].value);
});
});
});