Skip to content

Commit

Permalink
Merge pull request #302 from emberjs/application-tests
Browse files Browse the repository at this point in the history
Implement `startApplicationTest` from emberjs/rfcs#268.
  • Loading branch information
rwjblue authored Dec 17, 2017
2 parents 712768c + d4313f9 commit 39f1ac6
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 36 deletions.
23 changes: 15 additions & 8 deletions addon-test-support/ember-qunit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ export { default as moduleFor } from './legacy-2-x/module-for';
export { default as moduleForComponent } from './legacy-2-x/module-for-component';
export { default as moduleForModel } from './legacy-2-x/module-for-model';
export { default as QUnitAdapter } from './adapter';
export {
setResolver,
render,
clearRender,
settled,
pauseTest,
resumeTest,
} from '@ember/test-helpers';
export { setResolver } from '@ember/test-helpers';
export { module, test, skip, only, todo } from 'qunit';
export { loadTests } from './test-loader';

Expand All @@ -22,6 +15,8 @@ import {
teardownContext,
setupRenderingContext,
teardownRenderingContext,
setupApplicationContext,
teardownApplicationContext,
} from '@ember/test-helpers';

export function setupTest(hooks, options) {
Expand Down Expand Up @@ -53,6 +48,18 @@ export function setupRenderingTest(hooks, options) {
});
}

export function setupApplicationTest(hooks, options) {
setupTest(hooks, options);

hooks.beforeEach(function() {
return setupApplicationContext(this);
});

hooks.afterEach(function() {
return teardownApplicationContext(this);
});
}

/**
Uses current URL configuration to setup the test container.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"test": "ember try:each"
},
"dependencies": {
"@ember/test-helpers": "^0.7.1",
"@ember/test-helpers": "^0.7.9",
"broccoli-funnel": "^2.0.1",
"broccoli-merge-trees": "^2.0.0",
"common-tags": "^1.4.0",
Expand Down
102 changes: 102 additions & 0 deletions tests/acceptance/basic-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { module, test } from 'qunit';
import EmberRouter from '@ember/routing/router';
import Route from '@ember/routing/route';
import hbs from 'htmlbars-inline-precompile';
import { setupApplicationTest } from 'ember-qunit';
import { visit, currentRouteName, currentURL, click } from '@ember/test-helpers';
import { setResolverRegistry } from '../helpers/resolver';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';

module('setupApplicationTest tests', function(hooks) {
if (!hasEmberVersion(2, 4)) {
return;
}

const Router = EmberRouter.extend({ location: 'none' });
Router.map(function() {
this.route('widgets');
this.route('posts', function() {
this.route('post', { path: ':post_id' });
});
});

hooks.beforeEach(function() {
setResolverRegistry({
'router:main': Router,
'template:application': hbs`
<div class="nav">{{link-to 'posts' 'posts'}} | {{link-to 'widgets' 'widgets'}}</div>
{{outlet}}
`,
'template:index': hbs`<h1>Hello World!</h1>`,
'template:posts': hbs`<h1>Posts Page</h1>{{outlet}}`,
'template:posts/post': hbs`<div class="post-id">{{model.post_id}}</div>`,
'route:posts/post': Route.extend({
model(params) {
return params;
},
}),
});
});

setupApplicationTest(hooks);

test('can render', async function(assert) {
await visit('/');

assert.equal(currentRouteName(), 'index');
assert.equal(currentURL(), '/');

assert.equal(this.element.querySelector('.nav').textContent, 'posts | widgets');
assert.equal(this.element.querySelector('h1').textContent, 'Hello World!');
});

test('can perform a basic template rendering for nested route', async function(assert) {
await visit('/posts/1');

assert.equal(currentRouteName(), 'posts.post');
assert.equal(currentURL(), '/posts/1');

assert.equal(this.element.querySelector('.nav').textContent, 'posts | widgets');
assert.equal(this.element.querySelector('.post-id').textContent, '1');
});

test('can visit multiple times', async function(assert) {
await visit('/posts/1');

assert.equal(currentRouteName(), 'posts.post');
assert.equal(currentURL(), '/posts/1');

assert.equal(this.element.querySelector('.nav').textContent, 'posts | widgets');
assert.equal(this.element.querySelector('.post-id').textContent, '1');

await visit('/');

assert.equal(currentRouteName(), 'index');
assert.equal(currentURL(), '/');

assert.equal(this.element.querySelector('.nav').textContent, 'posts | widgets');
assert.equal(this.element.querySelector('h1').textContent, 'Hello World!');

await visit('/posts/2');

assert.equal(currentRouteName(), 'posts.post');
assert.equal(currentURL(), '/posts/2');

assert.equal(this.element.querySelector('.nav').textContent, 'posts | widgets');
assert.equal(this.element.querySelector('.post-id').textContent, '2');
});

test('can navigate amongst routes', async function(assert) {
await visit('/');

assert.equal(currentRouteName(), 'index');
assert.equal(currentURL(), '/');

await click('a[href="/posts"]');

assert.equal(currentRouteName(), 'posts.index');
assert.equal(currentURL(), '/posts');

assert.equal(this.element.querySelector('h1').textContent, 'Posts Page');
});
});
11 changes: 10 additions & 1 deletion tests/dummy/app/resolver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import Resolver from 'ember-resolver';

export default Resolver;
export let registry = Object.create(null);
export function setRegistry(newRegistry) {
registry = newRegistry;
}

export default Resolver.extend({
resolve(fullName) {
return registry[fullName] || this._super(...arguments);
},
});
1 change: 1 addition & 0 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = function(environment) {
ENV.APP.LOG_VIEW_LOOKUPS = false;

ENV.APP.rootElement = '#ember-testing';
ENV.APP.autoboot = false;
}

return ENV;
Expand Down
24 changes: 7 additions & 17 deletions tests/helpers/resolver.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import Ember from 'ember';
import AppResolver from '../../resolver';
import App from '../../app';
import { setRegistry } from '../../resolver';
import config from '../../config/environment';
import { setResolver } from 'ember-test-helpers';

const Resolver = AppResolver.extend({
resolve: function(fullName) {
return this.registry[fullName] || this._super.apply(this, arguments);
},

normalize: function(fullName) {
return Ember.String.dasherize(fullName);
},
export const application = App.create(config.APP);
export const resolver = application.Resolver.create({
namespace: application,
isResolverFromTestHelpers: true,
});
const resolver = Resolver.create();

resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
};

export default resolver;

setResolver(resolver);

export function setResolverRegistry(registry) {
resolver.set('registry', registry);
setRegistry(registry);
}
3 changes: 2 additions & 1 deletion tests/integration/setup-rendering-test-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { module, test } from 'qunit';
import Component from '@ember/component';
import { helper } from '@ember/component/helper';
import hbs from 'htmlbars-inline-precompile';
import { setupRenderingTest, render } from 'ember-qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { setResolverRegistry } from '../helpers/resolver';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';

Expand Down
7 changes: 4 additions & 3 deletions tests/test-helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import resolver from './helpers/resolver';
import { setResolver, start } from 'ember-qunit';
import { application } from './helpers/resolver';
import { start } from 'ember-qunit';
import { setApplication } from '@ember/test-helpers';

setResolver(resolver);
setApplication(application);
start();
34 changes: 29 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# yarn lockfile v1


"@ember/test-helpers@^0.7.1":
version "0.7.1"
resolved "https://registry.yarnpkg.com/@ember/test-helpers/-/test-helpers-0.7.1.tgz#14ba828ebc5b7b0e6eb7889352cb40af0c995349"
"@ember/test-helpers@^0.7.9":
version "0.7.9"
resolved "https://registry.yarnpkg.com/@ember/test-helpers/-/test-helpers-0.7.9.tgz#97d06e077a70028ca8e7a11ede7e8725e1a53167"
dependencies:
broccoli-funnel "^2.0.1"
ember-cli-babel "^6.8.1"
ember-cli-babel "^6.10.0"

"@glimmer/compiler@^0.25.3":
version "0.25.3"
Expand Down Expand Up @@ -519,6 +519,12 @@ babel-plugin-ember-modules-api-polyfill@^2.0.1:
dependencies:
ember-rfc176-data "^0.3.0"

babel-plugin-ember-modules-api-polyfill@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/babel-plugin-ember-modules-api-polyfill/-/babel-plugin-ember-modules-api-polyfill-2.3.0.tgz#0c01f359658cfb9c797f705af6b09f6220205ae0"
dependencies:
ember-rfc176-data "^0.3.0"

babel-plugin-feature-flags@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/babel-plugin-feature-flags/-/babel-plugin-feature-flags-0.3.1.tgz#9c827cf9a4eb9a19f725ccb239e85cab02036fc1"
Expand Down Expand Up @@ -1792,6 +1798,24 @@ ember-cli-babel@^6.0.0, ember-cli-babel@^6.0.0-beta.7, ember-cli-babel@^6.3.0, e
clone "^2.0.0"
ember-cli-version-checker "^2.0.0"

ember-cli-babel@^6.10.0:
version "6.11.0"
resolved "https://registry.yarnpkg.com/ember-cli-babel/-/ember-cli-babel-6.11.0.tgz#79cb184bac3c05bfe181ddc306bac100ab1f9493"
dependencies:
amd-name-resolver "0.0.7"
babel-plugin-debug-macros "^0.1.11"
babel-plugin-ember-modules-api-polyfill "^2.3.0"
babel-plugin-transform-es2015-modules-amd "^6.24.0"
babel-polyfill "^6.16.0"
babel-preset-env "^1.5.1"
broccoli-babel-transpiler "^6.1.2"
broccoli-debug "^0.6.2"
broccoli-funnel "^1.0.0"
broccoli-source "^1.1.0"
clone "^2.0.0"
ember-cli-version-checker "^2.1.0"
semver "^5.4.1"

ember-cli-broccoli-sane-watcher@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/ember-cli-broccoli-sane-watcher/-/ember-cli-broccoli-sane-watcher-2.0.4.tgz#f43f42f75b7509c212fb926cd9aea86ae19264c6"
Expand Down Expand Up @@ -4697,7 +4721,7 @@ sane@^1.1.1, sane@^1.6.0:
walker "~1.0.5"
watch "~0.10.0"

semver@^5.1.0, semver@^5.1.1, semver@^5.3.0:
semver@^5.1.0, semver@^5.1.1, semver@^5.3.0, semver@^5.4.1:
version "5.4.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.4.1.tgz#e059c09d8571f0540823733433505d3a2f00b18e"

Expand Down

0 comments on commit 39f1ac6

Please sign in to comment.