-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename `wait` to `settled` * Ensure existing `import wait from 'ember-test-helpers/wait'` code works properly * Export `settled` as public API from the main `index.js` * Move existing `wait` tests into `legacy-0-6-x` folder Primary reasoning for the rename from `wait` to `settled`: ```js test('can do xyz', async function(assert) { await this.render(hbs`{{some-thing}}`); await click('.some-thing'); await wait(); // <- WAT?!?!!? }); ``` This looks much better: ```js test('can do xyz', async function(assert) { await this.render(hbs`{{some-thing}}`); await click('.some-thing'); await settled(); }); ```
- Loading branch information
Showing
7 changed files
with
111 additions
and
103 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
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,98 @@ | ||
/* globals self */ | ||
|
||
import { run } from '@ember/runloop'; | ||
|
||
import { Promise as EmberPromise } from 'rsvp'; | ||
import jQuery from 'jquery'; | ||
|
||
import Ember from 'ember'; | ||
|
||
let requests; | ||
function incrementAjaxPendingRequests(_, xhr) { | ||
requests.push(xhr); | ||
} | ||
|
||
function decrementAjaxPendingRequests(_, xhr) { | ||
// In most Ember versions to date (current version is 2.16) RSVP promises are | ||
// configured to flush in the actions queue of the Ember run loop, however it | ||
// is possible that in the future this changes to use "true" micro-task | ||
// queues. | ||
// | ||
// The entire point here, is that _whenever_ promises are resolved, this | ||
// counter will decrement. In the specific case of AJAX, this means that any | ||
// promises chained off of `$.ajax` will properly have their `.then` called | ||
// _before_ this is decremented (and testing continues) | ||
EmberPromise.resolve().then(() => { | ||
for (let i = 0; i < requests.length; i++) { | ||
if (xhr === requests[i]) { | ||
requests.splice(i, 1); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
export function _teardownAJAXHooks() { | ||
if (!jQuery) { | ||
return; | ||
} | ||
|
||
jQuery(document).off('ajaxSend', incrementAjaxPendingRequests); | ||
jQuery(document).off('ajaxComplete', decrementAjaxPendingRequests); | ||
} | ||
|
||
export function _setupAJAXHooks() { | ||
requests = []; | ||
|
||
if (!jQuery) { | ||
return; | ||
} | ||
|
||
jQuery(document).on('ajaxSend', incrementAjaxPendingRequests); | ||
jQuery(document).on('ajaxComplete', decrementAjaxPendingRequests); | ||
} | ||
|
||
let _internalCheckWaiters; | ||
if (Ember.__loader.registry['ember-testing/test/waiters']) { | ||
_internalCheckWaiters = Ember.__loader.require('ember-testing/test/waiters').checkWaiters; | ||
} | ||
|
||
function checkWaiters() { | ||
if (_internalCheckWaiters) { | ||
return _internalCheckWaiters(); | ||
} else if (Ember.Test.waiters) { | ||
if (Ember.Test.waiters.any(([context, callback]) => !callback.call(context))) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default function settled(_options) { | ||
let options = _options || {}; | ||
let waitForTimers = options.hasOwnProperty('waitForTimers') ? options.waitForTimers : true; | ||
let waitForAJAX = options.hasOwnProperty('waitForAJAX') ? options.waitForAJAX : true; | ||
let waitForWaiters = options.hasOwnProperty('waitForWaiters') ? options.waitForWaiters : true; | ||
|
||
return new EmberPromise(function(resolve) { | ||
let watcher = self.setInterval(function() { | ||
if (waitForTimers && (run.hasScheduledTimers() || run.currentRunLoop)) { | ||
return; | ||
} | ||
|
||
if (waitForAJAX && requests && requests.length > 0) { | ||
return; | ||
} | ||
|
||
if (waitForWaiters && checkWaiters()) { | ||
return; | ||
} | ||
|
||
// Stop polling | ||
self.clearInterval(watcher); | ||
|
||
// Synchronously resolve the promise | ||
run(null, resolve); | ||
}, 10); | ||
}); | ||
} |
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
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 |
---|---|---|
@@ -1,98 +1,7 @@ | ||
/* globals self */ | ||
|
||
import { run } from '@ember/runloop'; | ||
|
||
import { Promise as EmberPromise } from 'rsvp'; | ||
import jQuery from 'jquery'; | ||
|
||
import Ember from 'ember'; | ||
|
||
var requests; | ||
function incrementAjaxPendingRequests(_, xhr) { | ||
requests.push(xhr); | ||
} | ||
|
||
function decrementAjaxPendingRequests(_, xhr) { | ||
// In most Ember versions to date (current version is 2.16) RSVP promises are | ||
// configured to flush in the actions queue of the Ember run loop, however it | ||
// is possible that in the future this changes to use "true" micro-task | ||
// queues. | ||
// | ||
// The entire point here, is that _whenever_ promises are resolved, this | ||
// counter will decrement. In the specific case of AJAX, this means that any | ||
// promises chained off of `$.ajax` will properly have their `.then` called | ||
// _before_ this is decremented (and testing continues) | ||
EmberPromise.resolve().then(() => { | ||
for (var i = 0; i < requests.length; i++) { | ||
if (xhr === requests[i]) { | ||
requests.splice(i, 1); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
export function _teardownAJAXHooks() { | ||
if (!jQuery) { | ||
return; | ||
} | ||
|
||
jQuery(document).off('ajaxSend', incrementAjaxPendingRequests); | ||
jQuery(document).off('ajaxComplete', decrementAjaxPendingRequests); | ||
} | ||
|
||
export function _setupAJAXHooks() { | ||
requests = []; | ||
|
||
if (!jQuery) { | ||
return; | ||
} | ||
|
||
jQuery(document).on('ajaxSend', incrementAjaxPendingRequests); | ||
jQuery(document).on('ajaxComplete', decrementAjaxPendingRequests); | ||
} | ||
|
||
var _internalCheckWaiters; | ||
if (Ember.__loader.registry['ember-testing/test/waiters']) { | ||
_internalCheckWaiters = Ember.__loader.require('ember-testing/test/waiters').checkWaiters; | ||
} | ||
|
||
function checkWaiters() { | ||
if (_internalCheckWaiters) { | ||
return _internalCheckWaiters(); | ||
} else if (Ember.Test.waiters) { | ||
if (Ember.Test.waiters.any(([context, callback]) => !callback.call(context))) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default function wait(_options) { | ||
var options = _options || {}; | ||
var waitForTimers = options.hasOwnProperty('waitForTimers') ? options.waitForTimers : true; | ||
var waitForAJAX = options.hasOwnProperty('waitForAJAX') ? options.waitForAJAX : true; | ||
var waitForWaiters = options.hasOwnProperty('waitForWaiters') ? options.waitForWaiters : true; | ||
|
||
return new EmberPromise(function(resolve) { | ||
var watcher = self.setInterval(function() { | ||
if (waitForTimers && (run.hasScheduledTimers() || run.currentRunLoop)) { | ||
return; | ||
} | ||
|
||
if (waitForAJAX && requests && requests.length > 0) { | ||
return; | ||
} | ||
|
||
if (waitForWaiters && checkWaiters()) { | ||
return; | ||
} | ||
|
||
// Stop polling | ||
self.clearInterval(watcher); | ||
|
||
// Synchronously resolve the promise | ||
run(null, resolve); | ||
}, 10); | ||
}); | ||
} | ||
export { | ||
default, | ||
_setupAJAXHooks, | ||
_setupPromiseListeners, | ||
_teardownAJAXHooks, | ||
_teardownPromiseListeners, | ||
} from './settled'; |
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