Skip to content

Commit cbbadbe

Browse files
authored
Merge pull request #1511 from eliasdawson/master
2 parents b7804e4 + af5f0b5 commit cbbadbe

6 files changed

+43
-51
lines changed

addon/src/setup-context.ts

+1-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Resolver } from '@ember/owner';
77
import { setOwner } from '@ember/application';
88

99
import buildOwner, { type Owner } from './build-owner.ts';
10-
import { _setupAJAXHooks, _teardownAJAXHooks } from './settled.ts';
10+
import { _setupAJAXHooks } from './settled.ts';
1111
import { _prepareOnerror } from './setup-onerror.ts';
1212
import Ember from 'ember';
1313
import {
@@ -19,10 +19,6 @@ import global from './global.ts';
1919
import { getResolver } from './resolver.ts';
2020
import { getApplication } from './application.ts';
2121
import getTestMetadata from './test-metadata.ts';
22-
import {
23-
registerDestructor,
24-
associateDestroyableChild,
25-
} from '@ember/destroyable';
2622
import {
2723
getDeprecationsForContext,
2824
getDeprecationsDuringCallbackForContext,
@@ -211,20 +207,6 @@ export function resumeTest(): void {
211207
context.resumeTest();
212208
}
213209

214-
/**
215-
@private
216-
@param {Object} context the test context being cleaned up
217-
*/
218-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
219-
function cleanup(context: BaseContext) {
220-
_teardownAJAXHooks();
221-
222-
// SAFETY: this is intimate API *designed* for us to override.
223-
(Ember as any).testing = false;
224-
225-
unsetContext();
226-
}
227-
228210
/**
229211
* Returns deprecations which have occurred so far for a the current test context
230212
*
@@ -410,8 +392,6 @@ export default function setupContext<T extends object>(
410392

411393
_backburner.DEBUG = true;
412394

413-
registerDestructor(context, cleanup);
414-
415395
_prepareOnerror(context);
416396

417397
return Promise.resolve()
@@ -438,8 +418,6 @@ export default function setupContext<T extends object>(
438418
return buildOwner(getApplication(), getResolver());
439419
})
440420
.then((owner) => {
441-
associateDestroyableChild(context, owner);
442-
443421
Object.defineProperty(context, 'owner', {
444422
configurable: true,
445423
enumerable: true,

addon/src/teardown-context.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { TestContext } from './setup-context';
2-
import settled from './settled.ts';
2+
import Ember from 'ember';
3+
import { unsetContext } from './setup-context.ts';
4+
import settled, { _teardownAJAXHooks } from './settled.ts';
35
import { _cleanupOnerror } from './setup-onerror.ts';
46
import { destroy } from '@ember/destroyable';
57

@@ -30,7 +32,14 @@ export default function teardownContext(
3032
.then(() => {
3133
_cleanupOnerror(context);
3234

33-
destroy(context);
35+
_teardownAJAXHooks();
36+
37+
// SAFETY: this is intimate API *designed* for us to override.
38+
(Ember as any).testing = false;
39+
40+
unsetContext();
41+
42+
destroy(context.owner);
3443
})
3544
.finally(() => {
3645
if (waitForSettled) {

test-app/tests/unit/dom/fill-in-test.js

-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ if (isFirefox || isChrome) {
1616
clickSteps.push('selectionchange');
1717
}
1818

19-
/**
20-
* Prior to Chrome 129 (canary),
21-
* Chrome 127.x emits an extra selectionchange event sometimes
22-
*
23-
* Delete this once we don't want to test against Chrome 127
24-
*/
25-
if (isChrome) {
26-
clickSteps.push('selectionchange');
27-
}
28-
2919
module('DOM Helper: fillIn', function (hooks) {
3020
if (!hasEmberVersion(2, 4)) {
3121
return;

test-app/tests/unit/setup-application-context-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,17 @@ module('setupApplicationContext', function (hooks) {
257257
'after click resolved',
258258
]);
259259
});
260+
261+
test('can reset context during a test', async function (assert) {
262+
await visit('/');
263+
assert.equal(currentURL(), '/');
264+
265+
await teardownContext(this);
266+
await setupContext(this);
267+
await setupApplicationContext(this);
268+
assert.equal(currentURL(), null);
269+
270+
await visit('/');
271+
assert.equal(currentURL(), '/');
272+
});
260273
});

test-app/tests/unit/setup-context-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -943,5 +943,19 @@ module('setupContext', function (hooks) {
943943
}
944944
}
945945
});
946+
947+
test('can reset context during a test', async function (assert) {
948+
assert.expect(1);
949+
950+
let context = await setupContext({});
951+
try {
952+
await teardownContext(context);
953+
context = await setupContext(context);
954+
} finally {
955+
await teardownContext(context);
956+
}
957+
958+
assert.ok(true, 'No error calling setupContext after teardownContext');
959+
});
946960
});
947961
});

test-app/tests/unit/teardown-context-test.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import hasjQuery from '../helpers/has-jquery';
1515
import ajax from '../helpers/ajax';
1616
import Pretender from 'pretender';
1717
import setupManualTestWaiter from '../helpers/manual-test-waiter';
18-
import { registerDestructor } from '@ember/destroyable';
1918

2019
module('teardownContext', function (hooks) {
2120
if (!hasEmberVersion(2, 4)) {
@@ -65,28 +64,17 @@ module('teardownContext', function (hooks) {
6564
assert.strictEqual(getContext(), undefined, 'context is unset');
6665
});
6766

68-
test('destroyables registered with the context are invoked', async function (assert) {
69-
registerDestructor(context, () => {
70-
assert.step('destructor was ran');
71-
});
72-
73-
assert.step('teardown started');
74-
67+
test('the owner is destroyed', async function (assert) {
7568
await teardownContext(context);
7669

77-
assert.step('teardown completed');
78-
79-
assert.verifySteps([
80-
'teardown started',
81-
'destructor was ran',
82-
'teardown completed',
83-
]);
70+
assert.ok(context.owner.isDestroyed);
8471
});
8572

86-
test('the owner is destroyed', async function (assert) {
73+
test('the context is not destroyed', async function (assert) {
8774
await teardownContext(context);
8875

8976
assert.ok(context.owner.isDestroyed);
77+
assert.notOk(context.isDestroyed);
9078
});
9179

9280
test('the application instance is destroyed and unwatched', async function (assert) {

0 commit comments

Comments
 (0)