Skip to content

Commit

Permalink
Implement setupApplicationContext.
Browse files Browse the repository at this point in the history
Add `visit`, `currentRouteName`, and `currentURL` helpers.
  • Loading branch information
rwjblue committed Dec 16, 2017
1 parent 9be2e74 commit 12fb639
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
7 changes: 7 additions & 0 deletions addon-test-support/@ember/test-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export {
export { default as teardownContext } from './teardown-context';
export { default as setupRenderingContext, render, clearRender } from './setup-rendering-context';
export { default as teardownRenderingContext } from './teardown-rendering-context';
export {
default as setupApplicationContext,
visit,
currentRouteName,
currentURL,
} from './setup-application-context';
export { default as teardownApplicationContext } from './teardown-application-context';
export { default as settled, isSettled, getState as getSettledState } from './settled';
export { default as waitUntil } from './wait-until';
export { default as validateErrorHandler } from './validate-error-handler';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { get } from '@ember/object';
import { nextTickPromise } from './-utils';
import { getContext } from './setup-context';
import settled from './settled';

export function visit() {
let context = getContext();
let { owner } = context;

return nextTickPromise()
.then(() => {
return owner.visit(...arguments);
})
.then(() => {
context.element = document.querySelector('#ember-testing > .ember-view');
})
.then(settled);
}

export function currentRouteName() {
let { owner } = getContext();
let router = owner.lookup('router:main');
return get(router, 'currentRouteName');
}

export function currentURL() {
let { owner } = getContext();
let router = owner.lookup('router:main');
return get(router, 'currentURL');
}

export default function() {
return nextTickPromise();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function() {}
132 changes: 132 additions & 0 deletions tests/unit/setup-application-context-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { module, test } from 'qunit';
import EmberRouter from '@ember/routing/router';
import Route from '@ember/routing/route';
import Service from '@ember/service';
import {
setupContext,
setupApplicationContext,
teardownContext,
teardownApplicationContext,
setApplication,
click,
visit,
currentRouteName,
currentURL,
} from '@ember/test-helpers';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import { setResolverRegistry, application } from '../helpers/resolver';
import hbs from 'htmlbars-inline-precompile';

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

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

hooks.beforeEach(async 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>`,
'service:foo': Service.extend({ isFoo: true }),
'route:posts/post': Route.extend({
model(params) {
return params;
},
}),
'route:widgets': Route.extend({
model() {
throw new Error('Model hook error from /widgets');
},
}),
});

setApplication(application);

await setupContext(this);
await setupApplicationContext(this);
});

hooks.afterEach(async function() {
await teardownApplicationContext(this);
await teardownContext(this);
});

test('can perform a basic template rendering', 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');
});

test('bubbles up errors', function(assert) {
assert.rejects(() => {
return visit('/widgets');
}, /Model hook error from \/widgets/);
});
});

0 comments on commit 12fb639

Please sign in to comment.