|
| 1 | +import {inject, async} from '@angular/core/testing'; |
| 2 | +import {TestComponentBuilder} from '@angular/compiler/testing'; |
| 3 | +import {By} from '@angular/platform-browser'; |
| 4 | + |
| 5 | +import {Component} from '@angular/core'; |
| 6 | + |
| 7 | +import {NgbTypeaheadWindow} from './typeahead-window'; |
| 8 | + |
| 9 | +function normalizeText(txt: string): string { |
| 10 | + return txt.trim().replace(/\s+/g, ' '); |
| 11 | +} |
| 12 | + |
| 13 | +function expectResults(nativeEl: HTMLElement, resultsDef: string[]): void { |
| 14 | + const pages = nativeEl.querySelectorAll('a'); |
| 15 | + |
| 16 | + expect(pages.length).toEqual(resultsDef.length); |
| 17 | + |
| 18 | + for (let i = 0; i < resultsDef.length; i++) { |
| 19 | + let resultDef = resultsDef[i]; |
| 20 | + let classIndicator = resultDef.charAt(0); |
| 21 | + |
| 22 | + expect(pages[i]).toHaveCssClass('dropdown-item'); |
| 23 | + if (classIndicator === '+') { |
| 24 | + expect(pages[i]).toHaveCssClass('active'); |
| 25 | + expect(normalizeText(pages[i].textContent)).toEqual(resultDef.substr(1)); |
| 26 | + } else { |
| 27 | + expect(pages[i]).not.toHaveCssClass('active'); |
| 28 | + expect(normalizeText(pages[i].textContent)).toEqual(resultDef); |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +describe('ngb-typeahead-window', () => { |
| 34 | + |
| 35 | + describe('display', () => { |
| 36 | + |
| 37 | + it('should display results with the first row active', async(inject([TestComponentBuilder], (tcb) => { |
| 38 | + const html = '<ngb-typeahead-window [results]="results" [term]="term"></ngb-typeahead-window>'; |
| 39 | + |
| 40 | + tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => { |
| 41 | + fixture.detectChanges(); |
| 42 | + expectResults(fixture.nativeElement, ['+bar', 'baz']); |
| 43 | + }); |
| 44 | + }))); |
| 45 | + |
| 46 | + it('should use a formatting function to display results', async(inject([TestComponentBuilder], (tcb) => { |
| 47 | + const html = |
| 48 | + '<ngb-typeahead-window [results]="results" [term]="term" [formatter]="formatterFn"></ngb-typeahead-window>'; |
| 49 | + |
| 50 | + tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => { |
| 51 | + fixture.detectChanges(); |
| 52 | + expectResults(fixture.nativeElement, ['+BAR', 'BAZ']); |
| 53 | + }); |
| 54 | + }))); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('active row', () => { |
| 58 | + |
| 59 | + it('should change active row on prev / next method call', async(inject([TestComponentBuilder], (tcb) => { |
| 60 | + const html = ` |
| 61 | + <button (click)="w.next()">+</button> |
| 62 | + <button (click)="w.prev()">-</button> |
| 63 | + <ngb-typeahead-window [results]="results" [term]="term" #w="ngbTypeaheadWindow"></ngb-typeahead-window>`; |
| 64 | + |
| 65 | + tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => { |
| 66 | + fixture.detectChanges(); |
| 67 | + const buttons = fixture.nativeElement.querySelectorAll('button'); |
| 68 | + |
| 69 | + expectResults(fixture.nativeElement, ['+bar', 'baz']); |
| 70 | + |
| 71 | + buttons[0].click(); |
| 72 | + fixture.detectChanges(); |
| 73 | + expectResults(fixture.nativeElement, ['bar', '+baz']); |
| 74 | + |
| 75 | + buttons[1].click(); |
| 76 | + fixture.detectChanges(); |
| 77 | + expectResults(fixture.nativeElement, ['+bar', 'baz']); |
| 78 | + }); |
| 79 | + }))); |
| 80 | + |
| 81 | + it('should wrap active row on prev / next method call', async(inject([TestComponentBuilder], (tcb) => { |
| 82 | + const html = ` |
| 83 | + <button (click)="w.next()">+</button> |
| 84 | + <button (click)="w.prev()">-</button> |
| 85 | + <ngb-typeahead-window [results]="results" [term]="term" #w="ngbTypeaheadWindow"></ngb-typeahead-window>`; |
| 86 | + |
| 87 | + tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => { |
| 88 | + fixture.detectChanges(); |
| 89 | + const buttons = fixture.nativeElement.querySelectorAll('button'); |
| 90 | + |
| 91 | + expectResults(fixture.nativeElement, ['+bar', 'baz']); |
| 92 | + |
| 93 | + buttons[1].click(); |
| 94 | + fixture.detectChanges(); |
| 95 | + expectResults(fixture.nativeElement, ['bar', '+baz']); |
| 96 | + |
| 97 | + buttons[0].click(); |
| 98 | + fixture.detectChanges(); |
| 99 | + expectResults(fixture.nativeElement, ['+bar', 'baz']); |
| 100 | + }); |
| 101 | + }))); |
| 102 | + |
| 103 | + it('should change active row on mouseenter', async(inject([TestComponentBuilder], (tcb) => { |
| 104 | + const html = `<ngb-typeahead-window [results]="results" [term]="term"></ngb-typeahead-window>`; |
| 105 | + |
| 106 | + tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => { |
| 107 | + fixture.detectChanges(); |
| 108 | + const rowDebugEls = fixture.debugElement.queryAll(By.css('a')); |
| 109 | + |
| 110 | + expectResults(fixture.nativeElement, ['+bar', 'baz']); |
| 111 | + |
| 112 | + rowDebugEls[1].triggerEventHandler('mouseenter', {}); |
| 113 | + fixture.detectChanges(); |
| 114 | + expectResults(fixture.nativeElement, ['bar', '+baz']); |
| 115 | + }); |
| 116 | + }))); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('result selection', () => { |
| 120 | + it('should select a given row on click', async(inject([TestComponentBuilder], (tcb) => { |
| 121 | + const html = |
| 122 | + '<ngb-typeahead-window [results]="results" [term]="term" (select)="selected = $event"></ngb-typeahead-window>'; |
| 123 | + |
| 124 | + tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => { |
| 125 | + fixture.detectChanges(); |
| 126 | + const rowDebugEls = fixture.debugElement.queryAll(By.css('a')); |
| 127 | + |
| 128 | + expectResults(fixture.nativeElement, ['+bar', 'baz']); |
| 129 | + |
| 130 | + rowDebugEls[1].triggerEventHandler('click', {}); |
| 131 | + fixture.detectChanges(); |
| 132 | + expect(fixture.componentInstance.selected).toBe('baz'); |
| 133 | + }); |
| 134 | + }))); |
| 135 | + }); |
| 136 | + |
| 137 | +}); |
| 138 | + |
| 139 | +@Component({selector: 'test-cmp', directives: [NgbTypeaheadWindow], template: ''}) |
| 140 | +class TestComponent { |
| 141 | + results = ['bar', 'baz']; |
| 142 | + term = 'ba'; |
| 143 | + selected: string; |
| 144 | + |
| 145 | + formatterFn = (result) => { return result.toUpperCase(); }; |
| 146 | +} |
0 commit comments