Skip to content

Commit

Permalink
Merge pull request #452 from Turbo87/import-defs
Browse files Browse the repository at this point in the history
Import type definitions from `@types/ember__test-helpers`
  • Loading branch information
Turbo87 authored Nov 7, 2018
2 parents cbcea83 + 19bf68f commit a6aae8a
Show file tree
Hide file tree
Showing 25 changed files with 113 additions and 50 deletions.
2 changes: 1 addition & 1 deletion addon-test-support/@ember/test-helpers/-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const futureTick = setTimeout;
@private
@returns {Promise<void>} Promise which can not be forced to be ran synchronously
*/
export function nextTickPromise() {
export function nextTickPromise(): RSVP.Promise<void> {
// Ember 3.4 removed the auto-run assertion, in 3.4+ we can (and should) avoid the "psuedo promisey" run loop configuration
// for our `nextTickPromise` implementation. This allows us to have real microtask based next tick timing...
if (hasEmberVersion(3, 4)) {
Expand Down
10 changes: 6 additions & 4 deletions addon-test-support/@ember/test-helpers/application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Application from '@ember/application';

import { getResolver, setResolver } from './resolver';

var __application__;
var __application__: Application;

/**
Stores the provided application instance so that tests being ran will be aware of the application under test.
Expand All @@ -11,11 +13,11 @@ var __application__;
@public
@param {Ember.Application} application the application that will be tested
*/
export function setApplication(application) {
export function setApplication(application: Application): void {
__application__ = application;

if (!getResolver()) {
let Resolver = application.Resolver;
let Resolver = (application as any).Resolver;
let resolver = Resolver.create({ namespace: application });

setResolver(resolver);
Expand All @@ -28,6 +30,6 @@ export function setApplication(application) {
@public
@returns {Ember.Application} the previously stored application instance under test
*/
export function getApplication() {
export function getApplication(): Application {
return __application__;
}
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/-target.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Target = string | Element;

export default Target;
3 changes: 2 additions & 1 deletion addon-test-support/@ember/test-helpers/dom/blur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fireEvent from './fire-event';
import settled from '../settled';
import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import Target from './-target';

/**
@private
Expand Down Expand Up @@ -42,7 +43,7 @@ export function __blur__(element) {
@param {string|Element} [target=document.activeElement] the element or selector to unfocus
@return {Promise<void>} resolves when settled
*/
export default function blur(target = document.activeElement) {
export default function blur(target: Target = document.activeElement!): Promise<void> {
return nextTickPromise().then(() => {
let element = getElement(target);
if (!element) {
Expand Down
5 changes: 3 additions & 2 deletions addon-test-support/@ember/test-helpers/dom/click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import settled from '../settled';
import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import isFormControl from './-is-form-control';
import Target from './-target';

/**
@private
Expand Down Expand Up @@ -46,14 +47,14 @@ export function __click__(element, options) {
The exact listing of events that are triggered may change over time as needed
to continue to emulate how actual browsers handle clicking a given element.
Use the `options` hash to change the parameters of the MouseEvents.
Use the `options` hash to change the parameters of the MouseEvents.
@public
@param {string|Element} target the element or selector to click on
@param {Object} options the options to be merged into the mouse events
@return {Promise<void>} resolves when settled
*/
export default function click(target, options = {}) {
export default function click(target: Target, options: object = {}): Promise<void> {
return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `click`.');
Expand Down
5 changes: 3 additions & 2 deletions addon-test-support/@ember/test-helpers/dom/double-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { __focus__ } from './focus';
import settled from '../settled';
import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import Target from './-target';

/**
@private
Expand Down Expand Up @@ -57,14 +58,14 @@ export function __doubleClick__(element, options) {
The exact listing of events that are triggered may change over time as needed
to continue to emulate how actual browsers handle clicking a given element.
Use the `options` hash to change the parameters of the MouseEvents.
Use the `options` hash to change the parameters of the MouseEvents.
@public
@param {string|Element} target the element or selector to double-click on
@param {Object} options the options to be merged into the mouse events
@return {Promise<void>} resolves when settled
*/
export default function doubleClick(target, options = {}) {
export default function doubleClick(target: Target, options: object = {}): Promise<void> {
return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `doubleClick`.');
Expand Down
3 changes: 2 additions & 1 deletion addon-test-support/@ember/test-helpers/dom/fill-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { __focus__ } from './focus';
import settled from '../settled';
import fireEvent from './fire-event';
import { nextTickPromise } from '../-utils';
import Target from './-target';

/**
Fill the provided text into the `value` property (or set `.innerHTML` when
Expand All @@ -15,7 +16,7 @@ import { nextTickPromise } from '../-utils';
@param {string} text the text to fill into the target element
@return {Promise<void>} resolves when the application is settled
*/
export default function fillIn(target: any, text: string): Promise<void> {
export default function fillIn(target: Target, text: string): Promise<void> {
return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `fillIn`.');
Expand Down
2 changes: 1 addition & 1 deletion addon-test-support/@ember/test-helpers/dom/find-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import toArray from './-to-array';
@param {string} selector the selector to search for
@return {Array} array of matched elements
*/
export default function find(selector) {
export default function find(selector: string): Element[] {
if (!selector) {
throw new Error('Must pass a selector to `findAll`.');
}
Expand Down
2 changes: 1 addition & 1 deletion addon-test-support/@ember/test-helpers/dom/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getElement from './-get-element';
@param {string} selector the selector to search for
@return {Element} matched element or null
*/
export default function find(selector) {
export default function find(selector: string): Element | null {
if (!selector) {
throw new Error('Must pass a selector to `find`.');
}
Expand Down
3 changes: 2 additions & 1 deletion addon-test-support/@ember/test-helpers/dom/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fireEvent from './fire-event';
import settled from '../settled';
import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import Target from './-target';

/**
@private
Expand Down Expand Up @@ -45,7 +46,7 @@ export function __focus__(element) {
@param {string|Element} target the element or selector to focus
@return {Promise<void>} resolves when the application is settled
*/
export default function focus(target) {
export default function focus(target: Target): Promise<void> {
return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `focus`.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getContext } from '../setup-context';
@public
@returns {Element} the root element
*/
export default function getRootElement() {
export default function getRootElement(): Element {
let context = getContext();
let owner = context && context.owner;

Expand All @@ -30,7 +30,7 @@ export default function getRootElement() {
) {
return rootElement;
} else if (typeof rootElement === 'string') {
return document.querySelector(rootElement);
return document.querySelector(rootElement)!; // TODO remove "!"
} else {
throw new Error('Application.rootElement must be an element or a selector string');
}
Expand Down
5 changes: 3 additions & 2 deletions addon-test-support/@ember/test-helpers/dom/tap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fireEvent from './fire-event';
import { __click__ } from './click';
import settled from '../settled';
import { nextTickPromise } from '../-utils';
import Target from './-target';

/**
Taps on the specified target.
Expand Down Expand Up @@ -32,14 +33,14 @@ import { nextTickPromise } from '../-utils';
The exact listing of events that are triggered may change over time as needed
to continue to emulate how actual browsers handle tapping on a given element.
Use the `options` hash to change the parameters of the tap events.
Use the `options` hash to change the parameters of the tap events.
@public
@param {string|Element} target the element or selector to tap on
@param {Object} options the options to be merged into the touch events
@return {Promise<void>} resolves when settled
*/
export default function tap(target, options = {}) {
export default function tap(target: Target, options: object = {}): Promise<void> {
return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `tap`.');
Expand Down
7 changes: 6 additions & 1 deletion addon-test-support/@ember/test-helpers/dom/trigger-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import getElement from './-get-element';
import fireEvent from './fire-event';
import settled from '../settled';
import { nextTickPromise } from '../-utils';
import Target from './-target';

/**
* Triggers an event on the specified target.
Expand All @@ -24,7 +25,11 @@ import { nextTickPromise } from '../-utils';
* [new Blob(['Ember Rules!'])]
* );
*/
export default function triggerEvent(target, eventType, options) {
export default function triggerEvent(
target: Target,
eventType: string,
options?: object
): Promise<void> {
return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `triggerEvent`.');
Expand Down
19 changes: 17 additions & 2 deletions addon-test-support/@ember/test-helpers/dom/trigger-key-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ import fireEvent from './fire-event';
import settled from '../settled';
import { KEYBOARD_EVENT_TYPES } from './fire-event';
import { nextTickPromise, isNumeric } from '../-utils';
import Target from './-target';

const DEFAULT_MODIFIERS = Object.freeze({
export type KeyEvent = 'keydown' | 'keyup' | 'keypress';

export interface KeyModifiers {
ctrlKey?: boolean;
altKey?: boolean;
shiftKey?: boolean;
metaKey?: boolean;
}

const DEFAULT_MODIFIERS: KeyModifiers = Object.freeze({
ctrlKey: false,
altKey: false,
shiftKey: false,
Expand Down Expand Up @@ -122,7 +132,12 @@ function keyCodeFromKey(key) {
@param {boolean} [modifiers.metaKey=false] if true the generated event will indicate the meta key was pressed during the key event
@return {Promise<void>} resolves when the application is settled
*/
export default function triggerKeyEvent(target, eventType, key, modifiers = DEFAULT_MODIFIERS) {
export default function triggerKeyEvent(
target: Target,
eventType: KeyEvent,
key: number | string,
modifiers: KeyModifiers = DEFAULT_MODIFIERS
): Promise<void> {
return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `triggerKeyEvent`.');
Expand Down
3 changes: 2 additions & 1 deletion addon-test-support/@ember/test-helpers/dom/type-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import isFormControl from './-is-form-control';
import { __focus__ } from './focus';
import { Promise } from 'rsvp';
import fireEvent from './fire-event';
import Target from './-target';

/**
* Mimics character by character entry into the target `input` or `textarea` element.
Expand All @@ -23,7 +24,7 @@ import fireEvent from './fire-event';
* @param {Object} options {delay: x} (default 50) number of milliseconds to wait per keypress
* @return {Promise<void>} resolves when the application is settled
*/
export default function typeIn(target, text, options = { delay: 50 }) {
export default function typeIn(target: Target, text, options = { delay: 50 }): Promise<void> {
return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `typeIn`.');
Expand Down
4 changes: 2 additions & 2 deletions addon-test-support/@ember/test-helpers/dom/wait-for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { nextTickPromise } from '../-utils';
export default function waitFor(
selector,
{ timeout = 1000, count = null, timeoutMessage = '' } = {}
) {
): Promise<Element | Element[]> {
return nextTickPromise().then(() => {
if (!selector) {
throw new Error('Must pass a selector to `waitFor`.');
Expand All @@ -27,7 +27,7 @@ export default function waitFor(
timeoutMessage = `waitFor timed out waiting for selector "${selector}"`;
}

let callback;
let callback: () => Element | Element[] | void;
if (count !== null) {
callback = () => {
let elements = getElements(selector);
Expand Down
8 changes: 5 additions & 3 deletions addon-test-support/@ember/test-helpers/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var __resolver__;
import Resolver from '@ember/application/resolver';

var __resolver__: Resolver;

/**
Stores the provided resolver instance so that tests being ran can resolve
Expand All @@ -9,7 +11,7 @@ var __resolver__;
@public
@param {Ember.Resolver} resolver the resolver to be used for testing
*/
export function setResolver(resolver) {
export function setResolver(resolver: Resolver): void {
__resolver__ = resolver;
}

Expand All @@ -19,6 +21,6 @@ export function setResolver(resolver) {
@public
@returns {Ember.Resolver} the previously stored resolver
*/
export function getResolver() {
export function getResolver(): Resolver {
return __resolver__;
}
12 changes: 10 additions & 2 deletions addon-test-support/@ember/test-helpers/settled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ function checkWaiters() {
return false;
}

export interface SettledState {
hasRunLoop: boolean;
hasPendingTimers: boolean;
hasPendingWaiters: boolean;
hasPendingRequests: boolean;
pendingRequestCount: number;
}

/**
Check various settledness metrics, and return an object with the following properties:
Expand All @@ -158,7 +166,7 @@ function checkWaiters() {
@public
@returns {Object} object with properties for each of the metrics used to determine settledness
*/
export function getSettledState() {
export function getSettledState(): SettledState {
let pendingRequestCount = pendingRequests();

return {
Expand All @@ -180,7 +188,7 @@ export function getSettledState() {
@public
@returns {boolean} `true` if settled, `false` otherwise
*/
export function isSettled() {
export function isSettled(): boolean {
let { hasPendingTimers, hasRunLoop, hasPendingRequests, hasPendingWaiters } = getSettledState();

if (hasPendingTimers || hasRunLoop || hasPendingRequests || hasPendingWaiters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import settled from './settled';
Navigate the application to the provided URL.
@public
@param {string} url The URL to visit (e.g. `/posts`)
@returns {Promise<void>} resolves when settled
*/
export function visit() {
export function visit(url: string): Promise<void> {
let context = getContext();
let { owner } = context;

Expand All @@ -33,7 +34,7 @@ export function visit() {
@public
@returns {string} the currently active route name
*/
export function currentRouteName() {
export function currentRouteName(): string {
let { owner } = getContext();
let router = owner.lookup('router:main');
return get(router, 'currentRouteName');
Expand All @@ -45,7 +46,7 @@ const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion(2, 13);
@public
@returns {string} the applications current url
*/
export function currentURL() {
export function currentURL(): string {
let { owner } = getContext();
let router = owner.lookup('router:main');

Expand All @@ -69,6 +70,8 @@ export function currentURL() {
@param {Object} context the context to setup
@returns {Promise<Object>} resolves with the context that was setup
*/
export default function setupApplicationContext() {
export default function setupApplicationContext<Context extends object>(
context: Context
): Promise<void> {
return nextTickPromise();
}
Loading

0 comments on commit a6aae8a

Please sign in to comment.