Skip to content

Commit

Permalink
Import remaining type declarations from @types/ember__test-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Nov 1, 2018
1 parent e0a6e4d commit 1136de3
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 29 deletions.
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__;
}
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();
}
22 changes: 14 additions & 8 deletions addon-test-support/@ember/test-helpers/setup-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { run } from '@ember/runloop';
import { set, setProperties, get, getProperties } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import Resolver from '@ember/application/resolver';

import buildOwner from './build-owner';
import { _setupAJAXHooks } from './settled';
import Ember from 'ember';
Expand All @@ -11,7 +13,7 @@ import { getResolver } from './resolver';
import { getApplication } from './application';
import { nextTickPromise } from './-utils';

let __test_context__;
let __test_context__: any | undefined;

/**
Stores the provided context as the "global testing context".
Expand All @@ -21,7 +23,7 @@ let __test_context__;
@public
@param {Object} context the context to use
*/
export function setContext(context) {
export function setContext(context: any): void {
__test_context__ = context;
}

Expand All @@ -31,7 +33,7 @@ export function setContext(context) {
@public
@returns {Object} the previously stored testing context
*/
export function getContext() {
export function getContext(): any | undefined {
return __test_context__;
}

Expand All @@ -42,7 +44,7 @@ export function getContext() {
@public
*/
export function unsetContext() {
export function unsetContext(): void {
__test_context__ = undefined;
}

Expand Down Expand Up @@ -80,7 +82,7 @@ export function unsetContext() {
* });
* });
*/
export function pauseTest() {
export function pauseTest(): Promise<void> {
let context = getContext();

if (!context || typeof context.pauseTest !== 'function') {
Expand All @@ -97,7 +99,7 @@ export function pauseTest() {
@public
*/
export function resumeTest() {
export function resumeTest(): void {
let context = getContext();

if (!context || typeof context.resumeTest !== 'function') {
Expand Down Expand Up @@ -128,7 +130,10 @@ export const CLEANUP = Object.create(null);
@param {Resolver} [options.resolver] a resolver to use for customizing normal resolution
@returns {Promise<Object>} resolves with the context that was setup
*/
export default function(context, options: any = {}) {
export default function<Context extends any>(
context: Context,
options: { resolver?: Resolver } = {}
): Promise<Context> {
(Ember as any).testing = true;
setContext(context);

Expand All @@ -139,8 +144,9 @@ export default function(context, options: any = {}) {
.then(() => {
let application = getApplication();
if (application) {
return application.boot();
return application.boot().then(() => {});
}
return;
})
.then(() => {
let testElementContainer = document.getElementById('ember-testing-container')!; // TODO remove "!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import global from './global';
import { getContext } from './setup-context';
import { nextTickPromise } from './-utils';
import settled from './settled';
import hbs from 'htmlbars-inline-precompile';
import hbs, { TemplateFactory } from 'htmlbars-inline-precompile';
import getRootElement from './dom/get-root-element';

export const RENDERING_CLEANUP = Object.create(null);
Expand Down Expand Up @@ -49,7 +49,7 @@ let templateId = 0;
@param {CompiledTemplate} template the template to render
@returns {Promise<void>} resolves when settled
*/
export function render(template) {
export function render(template: TemplateFactory): Promise<void> {
let context = getContext();

if (!template) {
Expand Down Expand Up @@ -112,7 +112,7 @@ export function render(template) {
@public
@returns {Promise<void>} resolves when settled
*/
export function clearRender() {
export function clearRender(): Promise<void> {
let context = getContext();

if (!context || typeof context.clearRender !== 'function') {
Expand Down Expand Up @@ -143,7 +143,9 @@ export function clearRender() {
@param {Object} context the context to setup for rendering
@returns {Promise<Object>} resolves with the context that was setup
*/
export default function setupRenderingContext(context) {
export default function setupRenderingContext<Context extends { owner: any }>(
context: Context
): Promise<Context> {
let contextGuid = guidFor(context);
RENDERING_CLEANUP[contextGuid] = [];

Expand Down Expand Up @@ -182,7 +184,8 @@ export default function setupRenderingContext(context) {
// `Ember._ContainerProxyMixin` and `Ember._RegistryProxyMixin` in this scenario we need to
// manually start the event dispatcher.
if (owner._emberTestHelpersMockOwner) {
let dispatcher = owner.lookup('event_dispatcher:main') || (Ember.EventDispatcher as any).create();
let dispatcher =
owner.lookup('event_dispatcher:main') || (Ember.EventDispatcher as any).create();
dispatcher.setup({}, '#ember-testing');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import settled from './settled';
@param {Object} context the context to setup
@returns {Promise<void>} resolves when settled
*/
export default function() {
export default function(context: object): Promise<void> {
return settled();
}
4 changes: 3 additions & 1 deletion addon-test-support/@ember/test-helpers/teardown-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import Ember from 'ember';
@param {Object} context the context to setup
@returns {Promise<void>} resolves when settled
*/
export default function teardownContext(context) {
export default function teardownContext<Context extends { owner: any }>(
context: Context
): Promise<void> {
return nextTickPromise()
.then(() => {
let { owner } = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import settled from './settled';
@param {Object} context the context to setup
@returns {Promise<void>} resolves when settled
*/
export default function teardownRenderingContext(context) {
export default function teardownRenderingContext(context: object): Promise<void> {
return nextTickPromise().then(() => {
let contextGuid = guidFor(context);

Expand Down

0 comments on commit 1136de3

Please sign in to comment.