Skip to content

move some things from cdk/testing to cdk/private/testing #16621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
/src/cdk/coercion/** @jelbourn
/src/cdk/collections/** @jelbourn @crisbeto @andrewseguin
/src/cdk/drag-drop/** @crisbeto
/src/cdk/private/testing/** @devversion
/src/cdk/keycodes/** @jelbourn
/src/cdk/layout/** @josephperrott
/src/cdk/observers/** @jelbourn @crisbeto
Expand All @@ -75,7 +76,7 @@
/src/cdk/scrolling/** @andrewseguin @crisbeto
/src/cdk/stepper/** @mmalerba
/src/cdk/table/** @andrewseguin
/src/cdk/testing/** @devversion
/src/cdk/testing/** @mmalerba @devversion
/src/cdk/text-field/** @mmalerba
/src/cdk/tree/** @jelbourn @andrewseguin

Expand Down
4 changes: 2 additions & 2 deletions src/cdk-experimental/scrolling/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ ng_e2e_test_library(
name = "e2e_test_sources",
srcs = glob(["**/*.e2e.spec.ts"]),
deps = [
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)

e2e_test_suite(
name = "e2e_tests",
deps = [
":e2e_test_sources",
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)
5 changes: 3 additions & 2 deletions src/cdk/overlay/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ng_test_library(
"//src/cdk/keycodes",
"//src/cdk/platform",
"//src/cdk/portal",
"//src/cdk/private/testing",
"//src/cdk/scrolling",
"//src/cdk/testing",
"@npm//@angular/common",
Expand All @@ -72,15 +73,15 @@ ng_e2e_test_library(
name = "e2e_test_sources",
srcs = glob(["**/*.e2e.spec.ts"]),
deps = [
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)

e2e_test_suite(
name = "e2e_tests",
deps = [
":e2e_test_sources",
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)

Expand Down
3 changes: 2 additions & 1 deletion src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
NgZone,
} from '@angular/core';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {dispatchFakeEvent, MockNgZone} from '@angular/cdk/testing';
import {MockNgZone} from '@angular/cdk/private/testing';
import {dispatchFakeEvent} from '@angular/cdk/testing';
import {
ComponentPortal,
PortalModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ComponentPortal, PortalModule} from '@angular/cdk/portal';
import {CdkScrollable, ScrollingModule} from '@angular/cdk/scrolling';
import {MockNgZone} from '@angular/cdk/testing';
import {MockNgZone} from '@angular/cdk/private/testing';
import {Component, ElementRef, NgModule, NgZone} from '@angular/core';
import {inject, TestBed} from '@angular/core/testing';
import {Subscription} from 'rxjs';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ComponentPortal, PortalModule} from '@angular/cdk/portal';
import {CdkScrollable, ScrollingModule, ViewportRuler} from '@angular/cdk/scrolling';
import {MockNgZone} from '@angular/cdk/testing';
import {MockNgZone} from '@angular/cdk/private/testing';
import {Component, ElementRef, NgModule, NgZone} from '@angular/core';
import {inject, TestBed} from '@angular/core/testing';
import {Subscription} from 'rxjs';
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/overlay/position/global-position-strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {NgModule, NgZone, Component} from '@angular/core';
import {TestBed, inject} from '@angular/core/testing';
import {MockNgZone} from '@angular/cdk/testing';
import {MockNgZone} from '@angular/cdk/private/testing';
import {PortalModule, ComponentPortal} from '@angular/cdk/portal';
import {OverlayModule, Overlay, OverlayConfig, OverlayRef, OverlayContainer} from '../index';

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/overlay/scroll/block-scroll-strategy.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {browser, Key, element, by} from 'protractor';
import {getScrollPosition} from '@angular/cdk/testing/e2e';
import {getScrollPosition} from '@angular/cdk/private/testing/e2e';


describe('scroll blocking', () => {
Expand Down
15 changes: 15 additions & 0 deletions src/cdk/private/testing/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package(default_visibility = ["//visibility:public"])

load("//tools:defaults.bzl", "ts_library")

ts_library(
name = "testing",
srcs = glob(
["**/*.ts"],
exclude = ["**/*.spec.ts"],
),
module_name = "@angular/cdk/private/testing",
deps = [
"@npm//@angular/core",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ exports_files(["tsconfig-e2e.json"])
ng_e2e_test_library(
name = "e2e",
srcs = glob(["**/*.ts"]),
module_name = "@angular/cdk/testing/e2e",
module_name = "@angular/cdk/private/testing/e2e",
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions src/cdk/private/testing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './public-api';
10 changes: 10 additions & 0 deletions src/cdk/private/testing/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './wrapped-error-message';
export * from './mock-ng-zone';
25 changes: 20 additions & 5 deletions src/cdk/testing/dispatch-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,45 @@ import {
createTouchEvent
} from './event-objects';

/** Utility to dispatch any event on a Node. */
/**
* Utility to dispatch any event on a Node.
* @docs-private
*/
export function dispatchEvent(node: Node | Window, event: Event): Event {
node.dispatchEvent(event);
return event;
}

/** Shorthand to dispatch a fake event on a specified node. */
/**
* Shorthand to dispatch a fake event on a specified node.
* @docs-private
*/
export function dispatchFakeEvent(node: Node | Window, type: string, canBubble?: boolean): Event {
return dispatchEvent(node, createFakeEvent(type, canBubble));
}

/** Shorthand to dispatch a keyboard event with a specified key code. */
/**
* Shorthand to dispatch a keyboard event with a specified key code.
* @docs-private
*/
export function dispatchKeyboardEvent(node: Node, type: string, keyCode: number, target?: Element):
KeyboardEvent {
return dispatchEvent(node, createKeyboardEvent(type, keyCode, target)) as KeyboardEvent;
}

/** Shorthand to dispatch a mouse event on the specified coordinates. */
/**
* Shorthand to dispatch a mouse event on the specified coordinates.
* @docs-private
*/
export function dispatchMouseEvent(node: Node, type: string, x = 0, y = 0,
event = createMouseEvent(type, x, y)): MouseEvent {
return dispatchEvent(node, event) as MouseEvent;
}

/** Shorthand to dispatch a touch event on the specified coordinates. */
/**
* Shorthand to dispatch a touch event on the specified coordinates.
* @docs-private
*/
export function dispatchTouchEvent(node: Node, type: string, x = 0, y = 0) {
return dispatchEvent(node, createTouchEvent(type, x, y));
}
23 changes: 13 additions & 10 deletions src/cdk/testing/element-focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@

import {dispatchFakeEvent} from './dispatch-events';

/**
* Patches an elements focus and blur methods to emit events consistently and predictably.
* This is necessary, because some browsers, like IE11, will call the focus handlers asynchronously,
* while others won't fire them at all if the browser window is not focused.
*/
export function patchElementFocus(element: HTMLElement) {
element.focus = () => dispatchFakeEvent(element, 'focus');
element.blur = () => dispatchFakeEvent(element, 'blur');
}

function triggerFocusChange(element: HTMLElement, event: 'focus' | 'blur') {
let eventFired = false;
const handler = () => eventFired = true;
Expand All @@ -29,10 +19,23 @@ function triggerFocusChange(element: HTMLElement, event: 'focus' | 'blur') {
}
}

/**
* Patches an elements focus and blur methods to emit events consistently and predictably.
* This is necessary, because some browsers, like IE11, will call the focus handlers asynchronously,
* while others won't fire them at all if the browser window is not focused.
* @docs-private
*/
export function patchElementFocus(element: HTMLElement) {
element.focus = () => dispatchFakeEvent(element, 'focus');
element.blur = () => dispatchFakeEvent(element, 'blur');
}

/** @docs-private */
export function triggerFocus(element: HTMLElement) {
triggerFocusChange(element, 'focus');
}

/** @docs-private */
export function triggerBlur(element: HTMLElement) {
triggerFocusChange(element, 'blur');
}
20 changes: 16 additions & 4 deletions src/cdk/testing/event-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

/** Creates a browser MouseEvent with the specified options. */
/**
* Creates a browser MouseEvent with the specified options.
* @docs-private
*/
export function createMouseEvent(type: string, x = 0, y = 0, button = 0) {
const event = document.createEvent('MouseEvent');

Expand All @@ -33,7 +36,10 @@ export function createMouseEvent(type: string, x = 0, y = 0, button = 0) {
return event;
}

/** Creates a browser TouchEvent with the specified pointer coordinates. */
/**
* Creates a browser TouchEvent with the specified pointer coordinates.
* @docs-private
*/
export function createTouchEvent(type: string, pageX = 0, pageY = 0) {
// In favor of creating events that work for most of the browsers, the event is created
// as a basic UI Event. The necessary details for the event will be set manually.
Expand All @@ -53,7 +59,10 @@ export function createTouchEvent(type: string, pageX = 0, pageY = 0) {
return event;
}

/** Dispatches a keydown event from an element. */
/**
* Dispatches a keydown event from an element.
* @docs-private
*/
export function createKeyboardEvent(type: string, keyCode: number, target?: Element, key?: string) {
let event = document.createEvent('KeyboardEvent') as any;
let originalPreventDefault = event.preventDefault;
Expand Down Expand Up @@ -82,7 +91,10 @@ export function createKeyboardEvent(type: string, keyCode: number, target?: Elem
return event;
}

/** Creates a fake event object with any desired event type. */
/**
* Creates a fake event object with any desired event type.
* @docs-private
*/
export function createFakeEvent(type: string, canBubble = false, cancelable = true) {
const event = document.createEvent('Event');
event.initEvent(type, canBubble, cancelable);
Expand Down
2 changes: 0 additions & 2 deletions src/cdk/testing/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
export * from './dispatch-events';
export * from './event-objects';
export * from './type-in-element';
export * from './wrapped-error-message';
export * from './mock-ng-zone';
export * from './element-focus';
1 change: 1 addition & 0 deletions src/cdk/testing/type-in-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {dispatchFakeEvent} from './dispatch-events';
* the `input` event, simulating the user typing.
* @param value Value to be set on the input.
* @param element Element onto which to set the value.
* @docs-private
*/
export function typeInElement(value: string, element: HTMLInputElement) {
element.focus();
Expand Down
4 changes: 2 additions & 2 deletions src/material-experimental/mdc-button/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ ng_e2e_test_library(
name = "e2e_test_sources",
srcs = glob(["**/*.e2e.spec.ts"]),
deps = [
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)

e2e_test_suite(
name = "e2e_tests",
deps = [
":e2e_test_sources",
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)
4 changes: 2 additions & 2 deletions src/material-experimental/mdc-card/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ ng_e2e_test_library(
name = "e2e_test_sources",
srcs = glob(["**/*.e2e.spec.ts"]),
deps = [
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)

e2e_test_suite(
name = "e2e_tests",
deps = [
":e2e_test_sources",
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)
4 changes: 2 additions & 2 deletions src/material-experimental/mdc-checkbox/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ ng_e2e_test_library(
name = "e2e_test_sources",
srcs = glob(["**/*.e2e.spec.ts"]),
deps = [
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)

e2e_test_suite(
name = "e2e_tests",
deps = [
":e2e_test_sources",
"//src/cdk/testing/e2e",
"//src/cdk/private/testing/e2e",
],
)
1 change: 1 addition & 0 deletions src/material-experimental/mdc-chips/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ ng_test_library(
"//src/cdk/bidi",
"//src/cdk/keycodes",
"//src/cdk/platform",
"//src/cdk/private/testing",
"//src/cdk/testing",
"//src/material/core",
"//src/material/form-field",
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-chips/chip-grid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
SPACE,
TAB
} from '@angular/cdk/keycodes';
import {MockNgZone} from '@angular/cdk/private/testing';
import {
createFakeEvent,
createKeyboardEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
MockNgZone,
typeInElement,
} from '@angular/cdk/testing';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-chips/chip-listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
SPACE,
TAB,
} from '@angular/cdk/keycodes';
import {MockNgZone} from '@angular/cdk/private/testing';
import {
createKeyboardEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
MockNgZone,
} from '@angular/cdk/testing';
import {
Component,
Expand Down
Loading