-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make
<Link>
inert for setupRenderingTest
Fixes #126.
- Loading branch information
1 parent
0b4ea4c
commit 2d21117
Showing
3 changed files
with
129 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
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,47 @@ | ||
import { waitUntil } from '@ember/test-helpers'; | ||
|
||
import Ember from 'ember'; | ||
|
||
import { on, off } from 'rsvp'; | ||
|
||
/** | ||
* I would be using `ember-qunit-assert-helpers` here, but it does not work with | ||
* async rendering. :( | ||
* | ||
* Adapted from https://github.com/workmanw/ember-qunit-assert-helpers/issues/18#issuecomment-390003905 | ||
*/ | ||
export default async function waitForError( | ||
callback: () => Promise<unknown>, | ||
options?: Parameters<typeof waitUntil>[1] | ||
) { | ||
const originalEmberListener = Ember.onerror; | ||
const originalWindowListener = window.onerror; | ||
|
||
let error: Error | undefined; | ||
Ember.onerror = uncaughtError => { | ||
error = uncaughtError; | ||
}; | ||
window.onerror = ( | ||
_message, | ||
_source, | ||
_lineNumber, | ||
_columnNumber, | ||
uncaughtError | ||
) => { | ||
error = uncaughtError; | ||
}; | ||
on('error', Ember.onerror); | ||
|
||
await Promise.all([ | ||
waitUntil(() => error, options).finally(() => { | ||
Ember.onerror = originalEmberListener; | ||
window.onerror = originalWindowListener; | ||
off('error', Ember.onerror); | ||
}), | ||
callback() | ||
]); | ||
|
||
if (!error) throw new Error('No Error was thrown.'); | ||
|
||
return error; | ||
} |
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,52 @@ | ||
import { render, click } from '@ember/test-helpers'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { module, test } from 'qunit'; | ||
|
||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
import waitForError from 'dummy/tests/helpers/wait-for-error'; | ||
|
||
module('Integration | Component | link', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
// Regression for: https://github.com/buschtoens/ember-link/issues/126 | ||
test('it renders', async function(assert) { | ||
await render(hbs` | ||
<Link @route="foo" as |l|> | ||
<a | ||
data-test-link | ||
href={{l.href}} | ||
class={{if l.isActive "is-active"}} | ||
{{on "click" l.transitionTo}} | ||
> | ||
Link | ||
</a> | ||
</Link> | ||
`); | ||
|
||
assert.dom('[data-test-link]').hasAttribute('href', ''); | ||
assert.dom('[data-test-link]').hasNoClass('is-active'); | ||
}); | ||
|
||
test('triggering a transition has no effect', async function(assert) { | ||
await render(hbs` | ||
<Link @route="foo" as |l|> | ||
<a | ||
data-test-link | ||
href={{l.href}} | ||
class={{if l.isActive "is-active"}} | ||
{{on "click" l.transitionTo}} | ||
> | ||
Link | ||
</a> | ||
</Link> | ||
`); | ||
|
||
const error = await waitForError(() => click('[data-test-link]')); | ||
assert.ok(error instanceof Error); | ||
assert.strictEqual( | ||
error.message, | ||
'Assertion Failed: You can only call `transitionTo`, when the router is initialized, e.g. when using `setupApplicationTest`.' | ||
); | ||
}); | ||
}); |