Skip to content
Merged
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
45 changes: 23 additions & 22 deletions addon-test-support/@ember/test-helpers/settled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@ import DebugInfo, { TestDebugInfo } from './-internal/debug-info';
//
// This utilizes a local utility method present in Ember since around 2.8.0 to
// properly consider pending AJAX requests done within legacy acceptance tests.
const _internalPendingRequests = (() => {
const _internalPendingRequestsModule = (() => {
let loader = (Ember as any).__loader;

if (loader.registry['ember-testing/test/pending_requests']) {
// Ember <= 3.1
return loader.require('ember-testing/test/pending_requests')
.pendingRequests;
return loader.require('ember-testing/test/pending_requests');
} else if (loader.registry['ember-testing/lib/test/pending_requests']) {
// Ember >= 3.2
return loader.require('ember-testing/lib/test/pending_requests')
.pendingRequests;
return loader.require('ember-testing/lib/test/pending_requests');
}

return () => 0;
return null;
})();

if (typeof jQuery !== 'undefined' && _internalPendingRequests) {
const _internalGetPendingRequestsCount = () => {
if (_internalPendingRequestsModule) {
return _internalPendingRequestsModule.pendingRequests();
}
return 0;
};

if (typeof jQuery !== 'undefined' && _internalPendingRequestsModule) {
// This exists to ensure that the AJAX listeners setup by Ember itself
// (which as of 2.17 are not properly torn down) get cleared and released
// when the application is destroyed. Without this, any AJAX requests
Expand All @@ -44,20 +49,16 @@ if (typeof jQuery !== 'undefined' && _internalPendingRequests) {
// This can be removed once Ember 4.0.0 is released
EmberApplicationInstance.reopen({
willDestroy(...args: any[]) {
const internalPendingRequests = _internalPendingRequests();

if (internalPendingRequests !== 0) {
Copy link
Contributor Author

@raycohen raycohen Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the outer if statement checks that _internalPendingRequestsModule exists, which I believe makes calling off and clearPendingRequests here safe and desirable regardless of what the PendingRequestsCount is

jQuery(document).off(
'ajaxSend',
internalPendingRequests.incrementPendingRequests
);
jQuery(document).off(
'ajaxComplete',
internalPendingRequests.decrementPendingRequests
);

internalPendingRequests.clearPendingRequests();
}
jQuery(document).off(
'ajaxSend',
_internalPendingRequestsModule.incrementPendingRequests
);
jQuery(document).off(
'ajaxComplete',
_internalPendingRequestsModule.decrementPendingRequests
);

_internalPendingRequestsModule.clearPendingRequests();

this._super(...args);
},
Expand All @@ -72,7 +73,7 @@ let requests: XMLHttpRequest[];
*/
function pendingRequests() {
let localRequestsPending = requests !== undefined ? requests.length : 0;
let internalRequestsPending = _internalPendingRequests();
let internalRequestsPending = _internalGetPendingRequestsCount();

return localRequestsPending + internalRequestsPending;
}
Expand Down