|
1 | 1 | import {async, ComponentFixture, TestBed} from '@angular/core/testing';
|
2 | 2 | import {Component, DebugElement} from '@angular/core';
|
3 | 3 | import {By} from '@angular/platform-browser';
|
4 |
| -import {dispatchFakeEvent} from '@angular/cdk/testing'; |
| 4 | +import { |
| 5 | + dispatchFakeEvent, |
| 6 | + dispatchKeyboardEvent, |
| 7 | + createKeyboardEvent, |
| 8 | + dispatchEvent, |
| 9 | +} from '@angular/cdk/testing'; |
| 10 | +import {SPACE, ENTER} from '@angular/cdk/keycodes'; |
5 | 11 | import {MatOption, MatOptionModule} from './index';
|
6 | 12 |
|
7 | 13 | describe('MatOption component', () => {
|
@@ -82,6 +88,65 @@ describe('MatOption component', () => {
|
82 | 88 | expect(optionInstance.id).toBe('custom-option');
|
83 | 89 | });
|
84 | 90 |
|
| 91 | + it('should select the option when pressing space', () => { |
| 92 | + const fixture = TestBed.createComponent(BasicOption); |
| 93 | + fixture.detectChanges(); |
| 94 | + |
| 95 | + const optionDebugElement = fixture.debugElement.query(By.directive(MatOption)); |
| 96 | + const optionNativeElement: HTMLElement = optionDebugElement.nativeElement; |
| 97 | + const optionInstance: MatOption = optionDebugElement.componentInstance; |
| 98 | + const spy = jasmine.createSpy('selection change spy'); |
| 99 | + const subscription = optionInstance.onSelectionChange.subscribe(spy); |
| 100 | + |
| 101 | + const event = dispatchKeyboardEvent(optionNativeElement, 'keydown', SPACE); |
| 102 | + fixture.detectChanges(); |
| 103 | + |
| 104 | + expect(spy).toHaveBeenCalled(); |
| 105 | + expect(event.defaultPrevented).toBe(true); |
| 106 | + subscription.unsubscribe(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should select the option when pressing enter', () => { |
| 110 | + const fixture = TestBed.createComponent(BasicOption); |
| 111 | + fixture.detectChanges(); |
| 112 | + |
| 113 | + const optionDebugElement = fixture.debugElement.query(By.directive(MatOption)); |
| 114 | + const optionNativeElement: HTMLElement = optionDebugElement.nativeElement; |
| 115 | + const optionInstance: MatOption = optionDebugElement.componentInstance; |
| 116 | + const spy = jasmine.createSpy('selection change spy'); |
| 117 | + const subscription = optionInstance.onSelectionChange.subscribe(spy); |
| 118 | + |
| 119 | + const event = dispatchKeyboardEvent(optionNativeElement, 'keydown', ENTER); |
| 120 | + fixture.detectChanges(); |
| 121 | + |
| 122 | + expect(spy).toHaveBeenCalled(); |
| 123 | + expect(event.defaultPrevented).toBe(true); |
| 124 | + subscription.unsubscribe(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should not do anything when pressing the selection keys with a modifier', () => { |
| 128 | + const fixture = TestBed.createComponent(BasicOption); |
| 129 | + fixture.detectChanges(); |
| 130 | + |
| 131 | + const optionDebugElement = fixture.debugElement.query(By.directive(MatOption)); |
| 132 | + const optionNativeElement: HTMLElement = optionDebugElement.nativeElement; |
| 133 | + const optionInstance: MatOption = optionDebugElement.componentInstance; |
| 134 | + const spy = jasmine.createSpy('selection change spy'); |
| 135 | + const subscription = optionInstance.onSelectionChange.subscribe(spy); |
| 136 | + |
| 137 | + [ENTER, SPACE].forEach(key => { |
| 138 | + const event = createKeyboardEvent('keydown', key); |
| 139 | + Object.defineProperty(event, 'shiftKey', {get: () => true}); |
| 140 | + dispatchEvent(optionNativeElement, event); |
| 141 | + fixture.detectChanges(); |
| 142 | + |
| 143 | + expect(event.defaultPrevented).toBe(false); |
| 144 | + }); |
| 145 | + |
| 146 | + expect(spy).not.toHaveBeenCalled(); |
| 147 | + subscription.unsubscribe(); |
| 148 | + }); |
| 149 | + |
85 | 150 | describe('ripples', () => {
|
86 | 151 | let fixture: ComponentFixture<BasicOption>;
|
87 | 152 | let optionDebugElement: DebugElement;
|
|
0 commit comments