-
-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `visit`, `currentRouteName`, and `currentURL` helpers.
- Loading branch information
Showing
4 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
addon-test-support/@ember/test-helpers/setup-application-context.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { get } from '@ember/object'; | ||
import { nextTickPromise } from './-utils'; | ||
import { getContext } from './setup-context'; | ||
import hasEmberVersion from './has-ember-version'; | ||
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'); | ||
} | ||
|
||
const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion(2, 13); | ||
export function currentURL() { | ||
let { owner } = getContext(); | ||
let router = owner.lookup('router:main'); | ||
|
||
if (HAS_CURRENT_URL_ON_ROUTER) { | ||
return get(router, 'currentURL'); | ||
} else { | ||
return get(router, 'location').getURL(); | ||
} | ||
} | ||
|
||
export default function() { | ||
return nextTickPromise(); | ||
} |
1 change: 1 addition & 0 deletions
1
addon-test-support/@ember/test-helpers/teardown-application-context.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default function() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
}); | ||
}); |