Skip to content

Commit

Permalink
task(test metadata): Adding type specific metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
scalvert committed Oct 21, 2019
1 parent 312fadc commit 846fff8
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 107 deletions.
220 changes: 117 additions & 103 deletions API.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BaseContext, getContext, isTestContext, TestContext } from './setup-con
import global from './global';
import hasEmberVersion from './has-ember-version';
import settled from './settled';
import getTestMetadata, { ITestMetadata } from './test-metadata';

export interface ApplicationTestContext extends TestContext {
element?: Element | null;
Expand Down Expand Up @@ -114,6 +115,8 @@ export function visit(url: string, options?: { [key: string]: any }): Promise<vo
}

let { owner } = context;
let testMetadata = getTestMetadata(context);
testMetadata.usedHelpers.push('visit');

return nextTickPromise()
.then(() => {
Expand Down Expand Up @@ -186,5 +189,8 @@ export function currentURL(): string {
@returns {Promise<Object>} resolves with the context that was setup
*/
export default function setupApplicationContext(context: TestContext): Promise<void> {
let testMetadata: ITestMetadata = getTestMetadata(context);
testMetadata.setupTypes.push('setupApplicationContext');

return nextTickPromise();
}
4 changes: 4 additions & 0 deletions addon-test-support/@ember/test-helpers/setup-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import global from './global';
import { getResolver } from './resolver';
import { getApplication } from './application';
import { nextTickPromise } from './-utils';
import getTestMetadata, { ITestMetadata } from './test-metadata';

export interface BaseContext {
[key: string]: any;
Expand Down Expand Up @@ -161,6 +162,9 @@ export default function setupContext(
let contextGuid = guidFor(context);
CLEANUP[contextGuid] = [];

let testMetadata: ITestMetadata = getTestMetadata(context);
testMetadata.setupTypes.push('setupContext');

run.backburner.DEBUG = true;

return nextTickPromise()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import hbs, { TemplateFactory } from 'htmlbars-inline-precompile';
import getRootElement from './dom/get-root-element';
import { Owner } from './build-owner';
import { deprecate } from '@ember/application/deprecations';
import getTestMetadata, { ITestMetadata } from './test-metadata';

export const RENDERING_CLEANUP = Object.create(null);
const OUTLET_TEMPLATE = hbs`{{outlet}}`;
Expand Down Expand Up @@ -105,6 +106,8 @@ export function render(template: TemplateFactory): Promise<void> {
}

let { owner } = context;
let testMetadata = getTestMetadata(context);
testMetadata.usedHelpers.push('render');

let toplevelView = owner.lookup('-top-level-view:main');
let OutletTemplate = lookupOutletTemplate(owner);
Expand Down Expand Up @@ -194,6 +197,9 @@ export default function setupRenderingContext(context: TestContext): Promise<Ren
let contextGuid = guidFor(context);
RENDERING_CLEANUP[contextGuid] = [];

let testMetadata: ITestMetadata = getTestMetadata(context);
testMetadata.setupTypes.push('setupRenderingContext');

return nextTickPromise()
.then(() => {
let { owner } = context;
Expand Down
23 changes: 20 additions & 3 deletions addon-test-support/@ember/test-helpers/test-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@ import { BaseContext } from './setup-context';

export interface ITestMetadata {
testName?: string;
types: string[];
setupTypes: string[];
usedHelpers: string[];
[key: string]: any;

readonly isRendering: boolean;
readonly isApplication: boolean;
}

export class TestMetadata implements ITestMetadata {
[key: string]: any;
testName?: string;
types: string[];
setupTypes: string[];
usedHelpers: string[];

constructor() {
this.types = [];
this.setupTypes = [];
this.usedHelpers = [];
}

get isRendering() {
return (
this.setupTypes.indexOf('setupRenderingContext') > -1 &&
this.usedHelpers.indexOf('render') > -1
);
}

get isApplication() {
return this.setupTypes.indexOf('setupApplicationContext') > -1;
}
}

Expand Down
1 change: 1 addition & 0 deletions documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ toc:
- validateErrorHandler
- setupOnerror
- resetOnerror
- getTestMetadata
13 changes: 13 additions & 0 deletions tests/unit/setup-application-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
setupApplicationContext,
teardownContext,
teardownApplicationContext,
getTestMetadata,
click,
visit,
currentRouteName,
Expand Down Expand Up @@ -76,6 +77,18 @@ module('setupApplicationContext', function(hooks) {
await teardownContext(this);
});

test('it sets up test metadata', function(assert) {
let testMetadata = getTestMetadata(this);

assert.deepEqual(testMetadata.setupTypes, ['setupContext', 'setupApplicationContext']);
});

test('it returns true for isApplication in an application test', function(assert) {
let testMetadata = getTestMetadata(this);

assert.ok(testMetadata.isApplication);
});

test('can perform a basic template rendering', async function(assert) {
await visit('/');

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/setup-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
resumeTest,
setApplication,
setResolver,
getTestMetadata,
} from '@ember/test-helpers';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
import {
Expand Down Expand Up @@ -123,6 +124,19 @@ module('setupContext', function(hooks) {
assert.equal(getContext(), context);
});

test('it sets up test metadata', function(assert) {
let testMetadata = getTestMetadata(context);

assert.deepEqual(testMetadata.setupTypes, ['setupContext']);
});

test('it retutns false for isRendering/isApplication in non-rendering/application tests', function(assert) {
let testMetadata = getTestMetadata(this);

assert.ok(!testMetadata.isRendering);
assert.ok(!testMetadata.isApplication);
});

test('can be used for unit style testing', function(assert) {
context.owner.register(
'service:foo',
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/setup-rendering-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
setupRenderingContext,
teardownContext,
teardownRenderingContext,
getTestMetadata,
render,
clearRender,
setApplication,
Expand Down Expand Up @@ -85,6 +86,18 @@ module('setupRenderingContext', function(hooks) {

overwriteTest('element');

test('it sets up test metadata', function(assert) {
let testMetadata = getTestMetadata(this);

assert.deepEqual(testMetadata.setupTypes, ['setupContext', 'setupRenderingContext']);
});

test('it retutns true for isRendering in an rendering test', function(assert) {
let testMetadata = getTestMetadata(this);

assert.ok(testMetadata.isRendering);
});

test('render can be used multiple times', async function(assert) {
await this.render(hbs`<p>Hello!</p>`);
assert.equal(this.element.textContent, 'Hello!');
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test-metadata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module('Test Metadata', function() {
let testMetadata = getTestMetadata(test);

assert.ok(testMetadata instanceof TestMetadata);
assert.deepEqual(testMetadata.types, []);
assert.deepEqual(testMetadata.setupTypes, []);
});

module('Annotated Test Metadata', function(hooks) {
Expand Down

0 comments on commit 846fff8

Please sign in to comment.