Skip to content

Commit

Permalink
Merge pull request #1511 from eliasdawson/master
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic authored Nov 8, 2024
2 parents b7804e4 + af5f0b5 commit cbbadbe
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 51 deletions.
24 changes: 1 addition & 23 deletions addon/src/setup-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Resolver } from '@ember/owner';
import { setOwner } from '@ember/application';

import buildOwner, { type Owner } from './build-owner.ts';
import { _setupAJAXHooks, _teardownAJAXHooks } from './settled.ts';
import { _setupAJAXHooks } from './settled.ts';
import { _prepareOnerror } from './setup-onerror.ts';
import Ember from 'ember';
import {
Expand All @@ -19,10 +19,6 @@ import global from './global.ts';
import { getResolver } from './resolver.ts';
import { getApplication } from './application.ts';
import getTestMetadata from './test-metadata.ts';
import {
registerDestructor,
associateDestroyableChild,
} from '@ember/destroyable';
import {
getDeprecationsForContext,
getDeprecationsDuringCallbackForContext,
Expand Down Expand Up @@ -211,20 +207,6 @@ export function resumeTest(): void {
context.resumeTest();
}

/**
@private
@param {Object} context the test context being cleaned up
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function cleanup(context: BaseContext) {
_teardownAJAXHooks();

// SAFETY: this is intimate API *designed* for us to override.
(Ember as any).testing = false;

unsetContext();
}

/**
* Returns deprecations which have occurred so far for a the current test context
*
Expand Down Expand Up @@ -410,8 +392,6 @@ export default function setupContext<T extends object>(

_backburner.DEBUG = true;

registerDestructor(context, cleanup);

_prepareOnerror(context);

return Promise.resolve()
Expand All @@ -438,8 +418,6 @@ export default function setupContext<T extends object>(
return buildOwner(getApplication(), getResolver());
})
.then((owner) => {
associateDestroyableChild(context, owner);

Object.defineProperty(context, 'owner', {
configurable: true,
enumerable: true,
Expand Down
13 changes: 11 additions & 2 deletions addon/src/teardown-context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { TestContext } from './setup-context';
import settled from './settled.ts';
import Ember from 'ember';
import { unsetContext } from './setup-context.ts';
import settled, { _teardownAJAXHooks } from './settled.ts';
import { _cleanupOnerror } from './setup-onerror.ts';
import { destroy } from '@ember/destroyable';

Expand Down Expand Up @@ -30,7 +32,14 @@ export default function teardownContext(
.then(() => {
_cleanupOnerror(context);

destroy(context);
_teardownAJAXHooks();

// SAFETY: this is intimate API *designed* for us to override.
(Ember as any).testing = false;

unsetContext();

destroy(context.owner);
})
.finally(() => {
if (waitForSettled) {
Expand Down
10 changes: 0 additions & 10 deletions test-app/tests/unit/dom/fill-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ if (isFirefox || isChrome) {
clickSteps.push('selectionchange');
}

/**
* Prior to Chrome 129 (canary),
* Chrome 127.x emits an extra selectionchange event sometimes
*
* Delete this once we don't want to test against Chrome 127
*/
if (isChrome) {
clickSteps.push('selectionchange');
}

module('DOM Helper: fillIn', function (hooks) {
if (!hasEmberVersion(2, 4)) {
return;
Expand Down
13 changes: 13 additions & 0 deletions test-app/tests/unit/setup-application-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,17 @@ module('setupApplicationContext', function (hooks) {
'after click resolved',
]);
});

test('can reset context during a test', async function (assert) {
await visit('/');
assert.equal(currentURL(), '/');

await teardownContext(this);
await setupContext(this);
await setupApplicationContext(this);
assert.equal(currentURL(), null);

await visit('/');
assert.equal(currentURL(), '/');
});
});
14 changes: 14 additions & 0 deletions test-app/tests/unit/setup-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,5 +943,19 @@ module('setupContext', function (hooks) {
}
}
});

test('can reset context during a test', async function (assert) {
assert.expect(1);

let context = await setupContext({});
try {
await teardownContext(context);
context = await setupContext(context);
} finally {
await teardownContext(context);
}

assert.ok(true, 'No error calling setupContext after teardownContext');
});
});
});
20 changes: 4 additions & 16 deletions test-app/tests/unit/teardown-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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 @@ -65,28 +64,17 @@ 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');

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

assert.step('teardown completed');

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

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

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

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

0 comments on commit cbbadbe

Please sign in to comment.