Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add waitFor msg #356

Merged
merged 4 commits into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ interim DOM states (e.g. loading states, pending promises, etc).
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** the options to be used (optional, default `{}`)
- `options.timeout` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the time to wait (in ms) for a match (optional, default `1000`)
- `options.count` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the number of elements that should match the provided selector (null means one or more) (optional, default `null`)
- `options.timeoutMessage` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the message to use in the reject on timeout (optional, default `'waitFor timed out'`)

Returns **([Element](https://developer.mozilla.org/docs/Web/API/Element) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Element](https://developer.mozilla.org/docs/Web/API/Element)>)** the element (or array of elements) that were being waited upon

Expand Down
7 changes: 5 additions & 2 deletions addon-test-support/@ember/test-helpers/dom/wait-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { nextTickPromise } from '../-utils';
@param {number} [options.count=null] the number of elements that should match the provided selector (null means one or more)
@returns {Element|Array<Element>} the element (or array of elements) that were being waited upon
*/
export default function waitFor(selector, { timeout = 1000, count = null } = {}) {
export default function waitFor(
selector,
{ timeout = 1000, count = null, timeoutMessage = 'waitFor timed out' } = {}
) {
return nextTickPromise().then(() => {
if (!selector) {
throw new Error('Must pass a selector to `waitFor`.');
Expand All @@ -32,6 +35,6 @@ export default function waitFor(selector, { timeout = 1000, count = null } = {})
} else {
callback = () => getElement(selector);
}
return waitUntil(callback, { timeout });
return waitUntil(callback, { timeout, timeoutMessage });
});
}
14 changes: 13 additions & 1 deletion tests/unit/dom/wait-for-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ module('DOM Helper: waitFor', function(hooks) {
} catch (error) {
let end = Date.now();
assert.ok(end - start >= 100, 'timed out after correct time');
assert.equal(error.message, 'waitUntil timed out');
assert.equal(error.message, 'waitFor timed out');
}
});

test('wait for selector with timeoutMessage', async function(assert) {
assert.expect(1);

await setupContext(context);

try {
await waitFor('.something', { timeoutMessage: '.something timed out' });
} catch (error) {
assert.equal(error.message, '.something timed out');
}
});
});