|
| 1 | +import { |
| 2 | + ComponentFixture, |
| 3 | + async, |
| 4 | + TestBed, |
| 5 | +} from '@angular/core/testing'; |
| 6 | +import {Component, DebugElement} from '@angular/core'; |
| 7 | +import {By} from '@angular/platform-browser'; |
| 8 | +import { |
| 9 | + CdkOption, |
| 10 | + CdkListboxModule, ListboxSelectionChangeEvent |
| 11 | +} from './index'; |
| 12 | +import {dispatchMouseEvent} from '@angular/cdk/testing/private'; |
| 13 | + |
| 14 | +describe('CdkOption', () => { |
| 15 | + |
| 16 | + describe('selection state change', () => { |
| 17 | + let fixture: ComponentFixture<ListboxWithOptions>; |
| 18 | + let options: DebugElement[]; |
| 19 | + let optionInstances: CdkOption[]; |
| 20 | + let optionElements: HTMLElement[]; |
| 21 | + |
| 22 | + beforeEach(async(() => { |
| 23 | + TestBed.configureTestingModule({ |
| 24 | + imports: [CdkListboxModule], |
| 25 | + declarations: [ListboxWithOptions], |
| 26 | + }).compileComponents(); |
| 27 | + })); |
| 28 | + |
| 29 | + beforeEach(async(() => { |
| 30 | + fixture = TestBed.createComponent(ListboxWithOptions); |
| 31 | + fixture.detectChanges(); |
| 32 | + |
| 33 | + options = fixture.debugElement.queryAll(By.directive(CdkOption)); |
| 34 | + optionInstances = options.map(o => o.injector.get<CdkOption>(CdkOption)); |
| 35 | + optionElements = options.map(o => o.nativeElement); |
| 36 | + })); |
| 37 | + |
| 38 | + it('should generate a unique optionId for each option', () => { |
| 39 | + let optionIds: string[] = []; |
| 40 | + for (const instance of optionInstances) { |
| 41 | + expect(optionIds.indexOf(instance.id)).toBe(-1); |
| 42 | + optionIds.push(instance.id); |
| 43 | + |
| 44 | + expect(instance.id).toMatch(/cdk-option-\d+/); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it('should have set the selected input of the options to null by default', () => { |
| 49 | + for (const instance of optionInstances) { |
| 50 | + expect(instance.selected).toBeFalse(); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it('should update aria-selected when selected is changed programmatically', () => { |
| 55 | + expect(optionElements[0].getAttribute('aria-selected')).toBeNull(); |
| 56 | + optionInstances[1].selected = true; |
| 57 | + fixture.detectChanges(); |
| 58 | + |
| 59 | + expect(optionElements[1].getAttribute('aria-selected')).toBe('true'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should update selected option on click event', () => { |
| 63 | + let selectedOptions = optionInstances.filter(option => option.selected); |
| 64 | + |
| 65 | + expect(selectedOptions.length).toBe(0); |
| 66 | + expect(optionElements[0].getAttribute('aria-selected')).toBeNull(); |
| 67 | + expect(optionInstances[0].selected).toBeFalse(); |
| 68 | + expect(fixture.componentInstance.changedOption).toBeUndefined(); |
| 69 | + |
| 70 | + dispatchMouseEvent(optionElements[0], 'click'); |
| 71 | + fixture.detectChanges(); |
| 72 | + |
| 73 | + selectedOptions = optionInstances.filter(option => option.selected); |
| 74 | + expect(selectedOptions.length).toBe(1); |
| 75 | + expect(optionElements[0].getAttribute('aria-selected')).toBe('true'); |
| 76 | + expect(optionInstances[0].selected).toBeTrue(); |
| 77 | + expect(fixture.componentInstance.changedOption).toBeDefined(); |
| 78 | + expect(fixture.componentInstance.changedOption.id).toBe(optionInstances[0].id); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | +}); |
| 83 | + |
| 84 | +@Component({ |
| 85 | + template: ` |
| 86 | + <div cdkListbox (selectionChange)="onSelectionChange($event)"> |
| 87 | + <div cdkOption>Void</div> |
| 88 | + <div cdkOption>Solar</div> |
| 89 | + <div cdkOption>Arc</div> |
| 90 | + <div cdkOption>Stasis</div> |
| 91 | + </div>` |
| 92 | +}) |
| 93 | +class ListboxWithOptions { |
| 94 | + changedOption: CdkOption; |
| 95 | + |
| 96 | + onSelectionChange(event: ListboxSelectionChangeEvent) { |
| 97 | + this.changedOption = event.option; |
| 98 | + } |
| 99 | +} |
0 commit comments