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

Remove simple alias for wait -> settled. #317

Merged
merged 1 commit into from
Feb 10, 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
25 changes: 2 additions & 23 deletions addon-test-support/@ember/test-helpers/settled.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,9 @@ export function getSettledState() {
@returns {boolean} `true` if settled, `false` otherwise
*/
export function isSettled() {
let waitForTimers = true;
let waitForAJAX = true;
let waitForWaiters = true;

if (arguments[0] !== undefined) {
let options = arguments[0];
waitForTimers = 'waitForTimers' in options ? options.waitForTimers : true;
waitForAJAX = 'waitForAJAX' in options ? options.waitForAJAX : true;
waitForWaiters = 'waitForWaiters' in options ? options.waitForWaiters : true;
}

let { hasPendingTimers, hasRunLoop, hasPendingRequests, hasPendingWaiters } = getSettledState();

if (waitForTimers && (hasPendingTimers || hasRunLoop)) {
return false;
}

if (waitForAJAX && hasPendingRequests) {
return false;
}

if (waitForWaiters && hasPendingWaiters) {
if (hasPendingTimers || hasRunLoop || hasPendingRequests || hasPendingWaiters) {
return false;
}

Expand All @@ -198,7 +179,5 @@ export function isSettled() {
@returns {Promise<void>} resolves when settled
*/
export default function settled() {
let options = arguments[0];

return waitUntil(() => isSettled(options), { timeout: Infinity });
return waitUntil(isSettled, { timeout: Infinity });
}
43 changes: 42 additions & 1 deletion addon-test-support/ember-test-helpers/wait.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
export {
default,
_setupAJAXHooks,
_setupPromiseListeners,
_teardownAJAXHooks,
_teardownPromiseListeners,
} from '@ember/test-helpers/settled';

import { waitUntil, getSettledState } from '@ember/test-helpers';

/**
Returns a promise that resolves when in a settled state (see `isSettled` for
a definition of "settled state").

@private
@deprecated
@param {Object} [options={}] the options to be used for waiting
@param {boolean} [options.waitForTimers=true] should timers be waited upon
@param {boolean} [options.waitForAjax=true] should $.ajax requests be waited upon
@param {boolean} [options.waitForWaiters=true] should test waiters be waited upon
@returns {Promise<void>} resolves when settled
*/
export default function wait(options = {}) {
if (typeof options !== 'object' || options === null) {
options = {};
}

return waitUntil(() => {
let waitForTimers = 'waitForTimers' in options ? options.waitForTimers : true;
let waitForAJAX = 'waitForAJAX' in options ? options.waitForAJAX : true;
let waitForWaiters = 'waitForWaiters' in options ? options.waitForWaiters : true;

let { hasPendingTimers, hasRunLoop, hasPendingRequests, hasPendingWaiters } = getSettledState();

if (waitForTimers && (hasPendingTimers || hasRunLoop)) {
return false;
}

if (waitForAJAX && hasPendingRequests) {
return false;
}

if (waitForWaiters && hasPendingWaiters) {
return false;
}

return true;
});
}
9 changes: 9 additions & 0 deletions tests/integration/settled-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ module('settled real-world scenarios', function(hooks) {
assert.equal(this.element.textContent, 'async value');
});

test('does not error for various argument types', async function(assert) {
assert.expect(0); // no assertions, just shouldn't error

await settled(3000);
await settled(null);
await settled(undefined);
await settled();
});

test('it works when async exists in an event/action', async function(assert) {
this.owner.register('component:x-test-2', TestComponent2);

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/wait-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import wait from 'ember-test-helpers/wait';

module('wait: unit tests', function() {
test('issues a helpful assertion for invalid arguments', async function(assert) {
assert.expect(0); // no assertions, just shouldn't error

await wait(3000);
await wait(null);
await wait();
});
});