|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Component, computed, signal, provideZoneChangeDetection} from '@angular/core'; |
| 10 | +import {ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing'; |
| 11 | +import {By} from '@angular/platform-browser'; |
| 12 | +import {Combobox} from './combobox'; |
| 13 | +import {ComboboxPopup} from './combobox-popup'; |
| 14 | +import {ComboboxWidget} from './combobox-widget'; |
| 15 | +import {Listbox, Option} from '../listbox'; |
| 16 | + |
| 17 | +describe('Combobox Zone.js integration', () => { |
| 18 | + let fixture: ComponentFixture<ComboboxListboxZoneExample>; |
| 19 | + let inputElement: HTMLInputElement; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + providers: [provideZoneChangeDetection()], |
| 24 | + }); |
| 25 | + fixture = TestBed.createComponent(ComboboxListboxZoneExample); |
| 26 | + await fixture.whenStable(); |
| 27 | + const inputDebugElement = fixture.debugElement.query(By.directive(Combobox)); |
| 28 | + inputElement = inputDebugElement.nativeElement as HTMLInputElement; |
| 29 | + }); |
| 30 | + |
| 31 | + const focus = () => { |
| 32 | + inputElement.dispatchEvent(new FocusEvent('focusin', {bubbles: true})); |
| 33 | + fixture.detectChanges(); |
| 34 | + }; |
| 35 | + |
| 36 | + const keydown = (key: string) => { |
| 37 | + focus(); |
| 38 | + inputElement.dispatchEvent( |
| 39 | + new KeyboardEvent('keydown', { |
| 40 | + key, |
| 41 | + bubbles: true, |
| 42 | + }), |
| 43 | + ); |
| 44 | + fixture.detectChanges(); |
| 45 | + }; |
| 46 | + |
| 47 | + function getOption(text: string): HTMLElement | null { |
| 48 | + const options = Array.from(document.querySelectorAll('[ngoption]')) as HTMLElement[]; |
| 49 | + return options.find(option => option.textContent?.trim() === text) || null; |
| 50 | + } |
| 51 | + |
| 52 | + it('should relay ArrowDown to the listbox and update active descendant', fakeAsync(() => { |
| 53 | + // Open the popup (sets active descendant to Alabama via default state) |
| 54 | + keydown('ArrowDown'); |
| 55 | + tick(); |
| 56 | + fixture.detectChanges(); |
| 57 | + |
| 58 | + // Check if expanded is true |
| 59 | + expect(inputElement.getAttribute('aria-expanded')).toBe('true'); |
| 60 | + // Active descendant is bound to host: |
| 61 | + const alabama = getOption('Alabama')!; |
| 62 | + expect(inputElement.getAttribute('aria-activedescendant')).toBe(alabama.id); |
| 63 | + |
| 64 | + // Press ArrowDown again to move to Alaska |
| 65 | + keydown('ArrowDown'); |
| 66 | + tick(); |
| 67 | + fixture.detectChanges(); |
| 68 | + |
| 69 | + const alaska = getOption('Alaska')!; |
| 70 | + expect(inputElement.getAttribute('aria-activedescendant')).toBe(alaska.id); |
| 71 | + })); |
| 72 | +}); |
| 73 | + |
| 74 | +@Component({ |
| 75 | + template: ` |
| 76 | + <div class="parent"> |
| 77 | + <input |
| 78 | + ngCombobox |
| 79 | + #combobox="ngCombobox" |
| 80 | + [(value)]="searchString" |
| 81 | + [(expanded)]="popupExpanded" |
| 82 | + (click)="popupExpanded.set(true)" |
| 83 | + /> |
| 84 | + <ng-template ngComboboxPopup [combobox]="combobox"> |
| 85 | + <ul ngListbox ngComboboxWidget #listbox="ngListbox" [activeDescendant]="listbox.activeDescendant()" focusMode="activedescendant" selectionMode="explicit"> |
| 86 | + @for (option of options(); track option) { |
| 87 | + <li ngOption [value]="option">{{option}}</li> |
| 88 | + } |
| 89 | + </ul> |
| 90 | + </ng-template> |
| 91 | + </div> |
| 92 | + `, |
| 93 | + imports: [Combobox, ComboboxPopup, ComboboxWidget, Listbox, Option], |
| 94 | +}) |
| 95 | +class ComboboxListboxZoneExample { |
| 96 | + popupExpanded = signal(false); |
| 97 | + searchString = signal(''); |
| 98 | + value = signal<string[]>([]); |
| 99 | + options = computed(() => ['Alabama', 'Alaska', 'Arizona', 'Arkansas']); |
| 100 | +} |
0 commit comments