Skip to content

Commit 9cfe603

Browse files
devversionkara
authored andcommitted
test(): add testing utilities for components (#2943)
Closes #2902
1 parent 817dcfd commit 9cfe603

17 files changed

+178
-291
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
1414
import {MdAutocomplete} from './autocomplete';
1515
import {MdInputContainer} from '../input/input-container';
1616
import {Observable} from 'rxjs/Observable';
17+
import {dispatchFakeEvent} from '../core/testing/dispatch-events';
18+
1719
import 'rxjs/add/operator/map';
1820

1921
describe('MdAutocomplete', () => {
@@ -63,7 +65,7 @@ describe('MdAutocomplete', () => {
6365
expect(fixture.componentInstance.trigger.panelOpen)
6466
.toBe(false, `Expected panel state to start out closed.`);
6567

66-
dispatchEvent('focus', input);
68+
dispatchFakeEvent(input, 'focus');
6769
fixture.detectChanges();
6870

6971
expect(fixture.componentInstance.trigger.panelOpen)
@@ -90,11 +92,11 @@ describe('MdAutocomplete', () => {
9092
});
9193

9294
it('should close the panel when blurred', async(() => {
93-
dispatchEvent('focus', input);
95+
dispatchFakeEvent(input, 'focus');
9496
fixture.detectChanges();
9597

9698
fixture.whenStable().then(() => {
97-
dispatchEvent('blur', input);
99+
dispatchFakeEvent(input, 'blur');
98100
fixture.detectChanges();
99101

100102
expect(fixture.componentInstance.trigger.panelOpen)
@@ -105,7 +107,7 @@ describe('MdAutocomplete', () => {
105107
}));
106108

107109
it('should close the panel when an option is clicked', async(() => {
108-
dispatchEvent('focus', input);
110+
dispatchFakeEvent(input, 'focus');
109111
fixture.detectChanges();
110112

111113
fixture.whenStable().then(() => {
@@ -121,7 +123,7 @@ describe('MdAutocomplete', () => {
121123
}));
122124

123125
it('should close the panel when a newly created option is clicked', async(() => {
124-
dispatchEvent('focus', input);
126+
dispatchFakeEvent(input, 'focus');
125127
fixture.detectChanges();
126128

127129
fixture.whenStable().then(() => {
@@ -166,7 +168,7 @@ describe('MdAutocomplete', () => {
166168
});
167169

168170
it('should hide the panel when the options list is empty', async(() => {
169-
dispatchEvent('focus', input);
171+
dispatchFakeEvent(input, 'focus');
170172

171173
fixture.whenStable().then(() => {
172174
fixture.detectChanges();
@@ -214,7 +216,7 @@ describe('MdAutocomplete', () => {
214216
.toBe(false, `Expected panel state to start out closed.`);
215217

216218
input.value = 'Alabama';
217-
dispatchEvent('input', input);
219+
dispatchFakeEvent(input, 'input');
218220
fixture.detectChanges();
219221

220222
expect(fixture.componentInstance.trigger.panelOpen)
@@ -467,7 +469,7 @@ describe('MdAutocomplete', () => {
467469
expect(fixture.componentInstance.stateCtrl.touched)
468470
.toBe(false, `Expected control to start out untouched.`);
469471

470-
dispatchEvent('blur', input);
472+
dispatchFakeEvent(input, 'blur');
471473
fixture.detectChanges();
472474

473475
expect(fixture.componentInstance.stateCtrl.touched)
@@ -487,8 +489,8 @@ describe('MdAutocomplete', () => {
487489
fixture.detectChanges();
488490

489491
input = fixture.debugElement.query(By.css('input')).nativeElement;
490-
DOWN_ARROW_EVENT = new FakeKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
491-
ENTER_EVENT = new FakeKeyboardEvent(ENTER) as KeyboardEvent;
492+
DOWN_ARROW_EVENT = new MockKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
493+
ENTER_EVENT = new MockKeyboardEvent(ENTER) as KeyboardEvent;
492494

493495
fixture.componentInstance.trigger.openPanel();
494496
fixture.detectChanges();
@@ -549,7 +551,7 @@ describe('MdAutocomplete', () => {
549551
const optionEls =
550552
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
551553

552-
const UP_ARROW_EVENT = new FakeKeyboardEvent(UP_ARROW) as KeyboardEvent;
554+
const UP_ARROW_EVENT = new MockKeyboardEvent(UP_ARROW) as KeyboardEvent;
553555
fixture.componentInstance.trigger._handleKeydown(UP_ARROW_EVENT);
554556

555557
fixture.whenStable().then(() => {
@@ -615,7 +617,7 @@ describe('MdAutocomplete', () => {
615617
typeInElement('New', input);
616618
fixture.detectChanges();
617619

618-
const SPACE_EVENT = new FakeKeyboardEvent(SPACE) as KeyboardEvent;
620+
const SPACE_EVENT = new MockKeyboardEvent(SPACE) as KeyboardEvent;
619621
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
620622
fixture.componentInstance.trigger._handleKeydown(SPACE_EVENT);
621623
fixture.detectChanges();
@@ -724,7 +726,7 @@ describe('MdAutocomplete', () => {
724726
expect(input.hasAttribute('aria-activedescendant'))
725727
.toBe(false, 'Expected aria-activedescendant to be absent if no active item.');
726728

727-
const DOWN_ARROW_EVENT = new FakeKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
729+
const DOWN_ARROW_EVENT = new MockKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
728730
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
729731
fixture.detectChanges();
730732

@@ -866,7 +868,7 @@ describe('MdAutocomplete', () => {
866868
fixture.detectChanges();
867869

868870
const input = fixture.debugElement.query(By.css('input')).nativeElement;
869-
dispatchEvent('focus', input);
871+
dispatchFakeEvent(input, 'focus');
870872
fixture.detectChanges();
871873

872874
expect(fixture.componentInstance.trigger.panelOpen)
@@ -995,35 +997,20 @@ class AutocompleteWithoutForms {
995997

996998
}
997999

998-
/**
999-
* TODO: Move this to core testing utility until Angular has event faking
1000-
* support.
1001-
*
1002-
* Dispatches an event from an element.
1003-
* @param eventName Name of the event
1004-
* @param element The element from which the event will be dispatched.
1005-
*/
1006-
function dispatchEvent(eventName: string, element: HTMLElement): void {
1007-
let event = document.createEvent('Event');
1008-
event.initEvent(eventName, true, true);
1009-
element.dispatchEvent(event);
1010-
}
1011-
1012-
10131000
/**
10141001
* Focuses an input, sets its value and dispatches
10151002
* the `input` event, simulating the user typing.
10161003
* @param value Value to be set on the input.
10171004
* @param element Element onto which to set the value.
10181005
*/
1019-
function typeInElement(value: string, element: HTMLInputElement) {
1006+
function typeInElement(value: string, element: HTMLInputElement, autoFocus = true) {
10201007
element.focus();
10211008
element.value = value;
1022-
dispatchEvent('input', element);
1009+
dispatchFakeEvent(element, 'input');
10231010
}
10241011

10251012
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
1026-
class FakeKeyboardEvent {
1013+
class MockKeyboardEvent {
10271014
constructor(public keyCode: number) {}
10281015
preventDefault() {}
10291016
}

src/lib/checkbox/checkbox.spec.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {By} from '@angular/platform-browser';
55
import {MdCheckbox, MdCheckboxChange, MdCheckboxModule} from './checkbox';
66
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
77
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
8+
import {dispatchFakeEvent} from '../core/testing/dispatch-events';
89

910

1011
describe('MdCheckbox', () => {
@@ -785,10 +786,3 @@ class CheckboxWithChangeEvent {
785786
class CheckboxWithFormControl {
786787
formControl = new FormControl();
787788
}
788-
789-
// TODO(devversion): replace with global utility once pull request #2943 is merged.
790-
function dispatchFakeEvent(element: HTMLElement, eventName: string): void {
791-
let event = document.createEvent('Event');
792-
event.initEvent(eventName, true, true);
793-
element.dispatchEvent(event);
794-
}

src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {NgModule, Component, ViewChild, ElementRef} from '@angular/core';
33
import {ScrollDispatcher} from './scroll-dispatcher';
44
import {OverlayModule} from '../overlay-directives';
55
import {Scrollable} from './scrollable';
6+
import {dispatchFakeEvent} from '../../testing/dispatch-events';
67

78
describe('Scroll Dispatcher', () => {
89

@@ -53,9 +54,7 @@ describe('Scroll Dispatcher', () => {
5354
// Emit a scroll event from the scrolling element in our component.
5455
// This event should be picked up by the scrollable directive and notify.
5556
// The notification should be picked up by the service.
56-
const scrollEvent = document.createEvent('UIEvents');
57-
scrollEvent.initUIEvent('scroll', true, true, window, 0);
58-
fixture.componentInstance.scrollingElement.nativeElement.dispatchEvent(scrollEvent);
57+
dispatchFakeEvent(fixture.componentInstance.scrollingElement.nativeElement, 'scroll');
5958

6059
// The scrollable directive should have notified the service immediately.
6160
expect(hasDirectiveScrollNotified).toBe(true);

0 commit comments

Comments
 (0)