Skip to content

Commit

Permalink
Provide consistent testing API
Browse files Browse the repository at this point in the history
  • Loading branch information
devversion committed Feb 9, 2017
1 parent 6aa94a8 commit 1fdbd21
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/lib/core/style/focus-classes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('FocusOriginMonitor', () => {

it('should detect focus via keyboard', async(() => {
// Simulate focus via keyboard.
dispatchKeyboardEvent(document, TAB);
dispatchKeyboardEvent(document, 'keydown', TAB);
buttonElement.focus();
fixture.detectChanges();

Expand Down Expand Up @@ -186,7 +186,7 @@ describe('cdkFocusClasses', () => {

it('should detect focus via keyboard', async(() => {
// Simulate focus via keyboard.
dispatchKeyboardEvent(document, TAB);
dispatchKeyboardEvent(document, 'keydown', TAB);
buttonElement.focus();
fixture.detectChanges();

Expand Down
6 changes: 3 additions & 3 deletions src/lib/core/testing/dispatch-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
} from './event-objects';

/** Shorthand to dispatch a fake event on a specified node. */
export function dispatchFakeEvent(node: Node, eventName: string) {
node.dispatchEvent(createFakeEvent(eventName));
export function dispatchFakeEvent(node: Node, type: string) {
node.dispatchEvent(createFakeEvent(type));
}

/** Shorthand to dispatch a keyboard event with a specified key code. */
export function dispatchKeyboardEvent(node: Node, keyCode: number, type = 'keydown') {
export function dispatchKeyboardEvent(node: Node, type: string, keyCode: number) {
node.dispatchEvent(createKeyboardEvent(type, keyCode));
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/core/testing/event-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export function createMouseEvent(type: string, x = 0, y = 0) {
}

/** Dispatches a keydown event from an element. */
export function createKeyboardEvent(eventType: string, keyCode: number) {
export function createKeyboardEvent(type: string, keyCode: number) {
let event = document.createEvent('KeyboardEvent') as any;
// Firefox does not support `initKeyboardEvent`, but supports `initKeyEvent`.
let initEventFn = (event.initKeyEvent || event.initKeyboardEvent).bind(event);

initEventFn(eventType, true, true, window, 0, 0, 0, 0, 0, keyCode);
initEventFn(type, true, true, window, 0, 0, 0, 0, 0, keyCode);

// Webkit Browsers don't set the keyCode when calling the init function.
// See related bug https://bugs.webkit.org/show_bug.cgi?id=16735
Expand Down Expand Up @@ -55,8 +55,8 @@ export function createTransitionEndEvent(propertyName: string, elapsedTime = 0)
}

/** Creates a fake event object with any desired event type. */
export function createFakeEvent(eventName: string) {
export function createFakeEvent(type: string) {
let event = document.createEvent('Event');
event.initEvent(eventName, true, true);
event.initEvent(type, true, true);
return event;
}
30 changes: 15 additions & 15 deletions src/lib/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ describe('MdSlider', () => {
it('should update the model on keydown', () => {
expect(testComponent.val).toBe(0);

dispatchKeyboardEvent(sliderNativeElement, UP_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', UP_ARROW);
fixture.detectChanges();

expect(testComponent.val).toBe(1);
Expand Down Expand Up @@ -949,14 +949,14 @@ describe('MdSlider', () => {
});

it('should increment slider by 1 on up arrow pressed', () => {
dispatchKeyboardEvent(sliderNativeElement, UP_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', UP_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(1);
});

it('should increment slider by 1 on right arrow pressed', () => {
dispatchKeyboardEvent(sliderNativeElement, RIGHT_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', RIGHT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(1);
Expand All @@ -965,7 +965,7 @@ describe('MdSlider', () => {
it('should decrement slider by 1 on down arrow pressed', () => {
sliderInstance.value = 100;

dispatchKeyboardEvent(sliderNativeElement, DOWN_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', DOWN_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(99);
Expand All @@ -974,14 +974,14 @@ describe('MdSlider', () => {
it('should decrement slider by 1 on left arrow pressed', () => {
sliderInstance.value = 100;

dispatchKeyboardEvent(sliderNativeElement, LEFT_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(99);
});

it('should increment slider by 10 on page up pressed', () => {
dispatchKeyboardEvent(sliderNativeElement, PAGE_UP);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', PAGE_UP);
fixture.detectChanges();

expect(sliderInstance.value).toBe(10);
Expand All @@ -990,14 +990,14 @@ describe('MdSlider', () => {
it('should decrement slider by 10 on page down pressed', () => {
sliderInstance.value = 100;

dispatchKeyboardEvent(sliderNativeElement, PAGE_DOWN);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', PAGE_DOWN);
fixture.detectChanges();

expect(sliderInstance.value).toBe(90);
});

it('should set slider to max on end pressed', () => {
dispatchKeyboardEvent(sliderNativeElement, END);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', END);
fixture.detectChanges();

expect(sliderInstance.value).toBe(100);
Expand All @@ -1006,7 +1006,7 @@ describe('MdSlider', () => {
it('should set slider to min on home pressed', () => {
sliderInstance.value = 100;

dispatchKeyboardEvent(sliderNativeElement, HOME);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', HOME);
fixture.detectChanges();

expect(sliderInstance.value).toBe(0);
Expand Down Expand Up @@ -1067,7 +1067,7 @@ describe('MdSlider', () => {
testComponent.invert = true;
fixture.detectChanges();

dispatchKeyboardEvent(sliderNativeElement, RIGHT_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', RIGHT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(1);
Expand All @@ -1078,7 +1078,7 @@ describe('MdSlider', () => {
sliderInstance.value = 100;
fixture.detectChanges();

dispatchKeyboardEvent(sliderNativeElement, LEFT_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(99);
Expand All @@ -1089,7 +1089,7 @@ describe('MdSlider', () => {
sliderInstance.value = 100;
fixture.detectChanges();

dispatchKeyboardEvent(sliderNativeElement, RIGHT_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', RIGHT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(99);
Expand All @@ -1099,7 +1099,7 @@ describe('MdSlider', () => {
testComponent.dir = 'rtl';
fixture.detectChanges();

dispatchKeyboardEvent(sliderNativeElement, LEFT_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(1);
Expand All @@ -1111,7 +1111,7 @@ describe('MdSlider', () => {
sliderInstance.value = 100;
fixture.detectChanges();

dispatchKeyboardEvent(sliderNativeElement, RIGHT_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', RIGHT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(99);
Expand All @@ -1122,7 +1122,7 @@ describe('MdSlider', () => {
testComponent.invert = true;
fixture.detectChanges();

dispatchKeyboardEvent(sliderNativeElement, LEFT_ARROW);
dispatchKeyboardEvent(sliderNativeElement, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(1);
Expand Down
8 changes: 5 additions & 3 deletions src/lib/tabs/tab-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,21 @@ describe('MdTabHeader', () => {
fixture.detectChanges();
expect(appComponent.mdTabHeader.focusIndex).toBe(0);

let tabListContainer = appComponent.mdTabHeader._tabListContainer.nativeElement;

// Move focus right to 2
dispatchKeyboardEvent(appComponent.mdTabHeader._tabListContainer.nativeElement, RIGHT_ARROW);
dispatchKeyboardEvent(tabListContainer, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
expect(appComponent.mdTabHeader.focusIndex).toBe(2);

// Select the focused index 2
expect(appComponent.selectedIndex).toBe(0);
dispatchKeyboardEvent(appComponent.mdTabHeader._tabListContainer.nativeElement, ENTER);
dispatchKeyboardEvent(tabListContainer, 'keydown', ENTER);
fixture.detectChanges();
expect(appComponent.selectedIndex).toBe(2);

// Move focus right to 0
dispatchKeyboardEvent(appComponent.mdTabHeader._tabListContainer.nativeElement, LEFT_ARROW);
dispatchKeyboardEvent(tabListContainer, 'keydown', LEFT_ARROW);
fixture.detectChanges();
expect(appComponent.mdTabHeader.focusIndex).toBe(0);
});
Expand Down

0 comments on commit 1fdbd21

Please sign in to comment.