Skip to content

Commit

Permalink
Merge pull request #914 from emberjs/use-destroyables
Browse files Browse the repository at this point in the history
Migrate to using @ember/destroyable for teardown.
  • Loading branch information
rwjblue committed Aug 26, 2020
2 parents 208b737 + b0f94ba commit c7fbe04
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 21 deletions.
23 changes: 22 additions & 1 deletion addon-test-support/@ember/test-helpers/setup-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { set, setProperties, get, getProperties } from '@ember/object';
import Resolver from '@ember/application/resolver';

import buildOwner, { Owner } from './build-owner';
import { _setupAJAXHooks } from './settled';
import { _setupAJAXHooks, _teardownAJAXHooks } from './settled';
import Ember from 'ember';
import { Promise } from 'rsvp';
import { assert } from '@ember/debug';
Expand All @@ -12,6 +12,7 @@ import { getResolver } from './resolver';
import { getApplication } from './application';
import { nextTickPromise } from './-utils';
import getTestMetadata, { ITestMetadata } from './test-metadata';
import { registerDestructor, associateDestroyableChild } from '@ember/destroyable';

export interface BaseContext {
[key: string]: any;
Expand Down Expand Up @@ -132,6 +133,22 @@ export function resumeTest(): void {
context.resumeTest();
}

/**
@private
@param {Object} context the test context being cleaned up
*/
function cleanup(context: BaseContext) {
_teardownAJAXHooks();

(Ember as any).testing = false;

unsetContext();

// this should not be required, but until https://github.com/emberjs/ember.js/pull/19106
// lands in a 3.20 patch release
context.owner.destroy();
}

/**
Used by test framework addons to setup the provided context for testing.
Expand Down Expand Up @@ -161,6 +178,8 @@ export default function setupContext(

run.backburner.DEBUG = true;

registerDestructor(context, cleanup);

return nextTickPromise()
.then(() => {
let application = getApplication();
Expand All @@ -185,6 +204,8 @@ export default function setupContext(
return buildOwner(getApplication(), getResolver());
})
.then(owner => {
associateDestroyableChild(context, owner);

Object.defineProperty(context, 'owner', {
configurable: true,
enumerable: true,
Expand Down
24 changes: 5 additions & 19 deletions addon-test-support/@ember/test-helpers/teardown-context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { run } from '@ember/runloop';
import { _teardownAJAXHooks } from './settled';
import { unsetContext, TestContext } from './setup-context';
import { TestContext } from './setup-context';
import { nextTickPromise } from './-utils';
import settled from './settled';
import Ember from 'ember';
import { destroy } from '@ember/destroyable';

/**
Used by test framework addons to tear down the provided context after testing is completed.
Expand All @@ -28,28 +26,16 @@ export default function teardownContext(
if (options !== undefined && 'waitForSettled' in options) {
waitForSettled = options.waitForSettled!;
}

return nextTickPromise()
.then(() => {
let { owner } = context;

_teardownAJAXHooks();

run(owner, 'destroy');
(Ember as any).testing = false;

unsetContext();

if (waitForSettled) {
return settled();
}

return nextTickPromise();
destroy(context);
})
.finally(() => {
if (waitForSettled) {
return settled();
}

return nextTickPromise();
return;
});
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"broccoli-debug": "^0.6.5",
"broccoli-funnel": "^3.0.3",
"ember-cli-babel": "^7.22.1",
"ember-cli-htmlbars": "^5.2.0"
"ember-cli-htmlbars": "^5.2.0",
"ember-destroyable-polyfill": "^2.0.1"
},
"devDependencies": {
"@ember/optional-features": "^1.3.0",
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/teardown-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import hasjQuery from '../helpers/has-jquery';
import ajax from '../helpers/ajax';
import Pretender from 'pretender';
import setupManualTestWaiter from '../helpers/manual-test-waiter';
import { registerDestructor } from '@ember/destroyable';

module('teardownContext', function (hooks) {
if (!hasEmberVersion(2, 4)) {
Expand Down Expand Up @@ -64,6 +65,26 @@ module('teardownContext', function (hooks) {
assert.strictEqual(getContext(), undefined, 'context is unset');
});

test('destroyables registered with the context are invoked', async function (assert) {
registerDestructor(context, () => {
assert.step('destructor was ran');
});

assert.step('teardown started');

await teardownContext(context);

assert.step('teardown completed');

assert.verifySteps(['teardown started', 'destructor was ran', 'teardown completed']);
});

test('the owner is destroyed', async function (assert) {
await teardownContext(context);

assert.ok(context.owner.isDestroyed);
});

if (hasjQuery()) {
test('out of balance xhr semaphores are cleaned up on teardown', async function (assert) {
this.pretender.unhandledRequest = function (/* verb, path, request */) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@ember/test-helpers/*": [
"addon-test-support/@ember/test-helpers/*"
],
"@ember/destroyable": ["node_modules/ember-destroyable-polyfill"],
"*": [
"types/*"
]
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5151,6 +5151,15 @@ ember-compatibility-helpers@^1.1.1, ember-compatibility-helpers@^1.2.0:
ember-cli-version-checker "^2.1.1"
semver "^5.4.1"

ember-compatibility-helpers@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/ember-compatibility-helpers/-/ember-compatibility-helpers-1.2.1.tgz#87c92c4303f990ff455c28ca39fb3ee11441aa16"
integrity sha512-6wzYvnhg1ihQUT5yGqnLtleq3Nv5KNv79WhrEuNU9SwR4uIxCO+KpyC7r3d5VI0EM7/Nmv9Nd0yTkzmTMdVG1A==
dependencies:
babel-plugin-debug-macros "^0.2.0"
ember-cli-version-checker "^2.1.1"
semver "^5.4.1"

ember-data@~3.20.0:
version "3.20.0"
resolved "https://registry.yarnpkg.com/ember-data/-/ember-data-3.20.0.tgz#613cfca5276b16b4f4bbfb35432e58f3e5a2d9f3"
Expand All @@ -5171,6 +5180,15 @@ ember-data@~3.20.0:
ember-cli-typescript "^3.1.3"
ember-inflector "^3.0.1"

ember-destroyable-polyfill@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/ember-destroyable-polyfill/-/ember-destroyable-polyfill-2.0.1.tgz#391cd95a99debaf24148ce953054008d00f151a6"
integrity sha512-hyK+/GPWOIxM1vxnlVMknNl9E5CAFVbcxi8zPiM0vCRwHiFS8Wuj7PfthZ1/OFitNNv7ITTeU8hxqvOZVsrbnQ==
dependencies:
ember-cli-babel "^7.22.1"
ember-cli-version-checker "^5.1.1"
ember-compatibility-helpers "^1.2.1"

ember-disable-prototype-extensions@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/ember-disable-prototype-extensions/-/ember-disable-prototype-extensions-1.1.3.tgz#1969135217654b5e278f9fe2d9d4e49b5720329e"
Expand Down

0 comments on commit c7fbe04

Please sign in to comment.