Last active
December 4, 2019 14:23
-
-
Save jgwhite/3f5589be011d2cabd8fa9fb8b0d64ee0 to your computer and use it in GitHub Desktop.
Ember {{assert}} helper
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
import { helper } from '@ember/component/helper'; | |
import { assert } from '@ember/debug'; | |
/** | |
* Verify that a certain expectation is met, or throw a exception | |
* otherwise. | |
* | |
* Delegates directly to `assert` from `@ember/debug`. | |
* | |
* @param {string} description - Describes the expectation. This will | |
* become the text of the Error thrown if the assertion fails. | |
* @param {boolean} condition - Must be truthy for the assertion to | |
* pass. If falsy, an exception will be thrown. | |
* | |
* @example | |
* {{assert "Please pass @someRequiredArg" @someRequiredArg}} | |
* @example | |
* {{assert "Please ensure @amount is greater than 0" (gt @amount 0)}} | |
*/ | |
export default helper(([description, condition]) => { | |
assert(description, condition); | |
}); |
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
import { module, test } from 'qunit'; | |
import { setupRenderingTest } from 'ember-qunit'; | |
import { render, setupOnerror } from '@ember/test-helpers'; | |
import hbs from 'htmlbars-inline-precompile'; | |
module('Integration | Helper | assert', function(hooks) { | |
setupRenderingTest(hooks); | |
test('performs an assertion', async function(assert) { | |
const errors = []; | |
setupOnerror(e => errors.push(e)); | |
await render(hbs` | |
{{assert "Please set this.foo" this.foo}} | |
`); | |
assert.deepEqual( | |
errors.map(e => e.message), | |
['Assertion Failed: Please set this.foo'] | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment