Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup basic infrastructure to ensure destroyables destroyed #19787

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
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
30 changes: 28 additions & 2 deletions packages/internal-test-helpers/lib/module-for.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
/* globals URLSearchParams */
import { DEBUG } from '@glimmer/env';
import { isEnabled } from '@ember/canary-features';
import applyMixins from './apply-mixins';
import getAllPropertyNames from './get-all-property-names';
import { setContext, unsetContext } from './test-context';
import { all } from 'rsvp';
import { enableDestroyableTracking, assertDestroyablesDestroyed } from '@glimmer/destroyable';

const ASSERT_DESTROYABLES = (() => {
if (typeof URLSearchParams === 'undefined' || typeof document !== 'object') {
return false;
}

let queryParams = new URLSearchParams(document.location.search.substring(1));
let assertDestroyables = queryParams.get('assertDestroyables');

return assertDestroyables !== null;
})();

export default function moduleFor(description, TestClass, ...mixins) {
QUnit.module(description, function (hooks) {
setupTestClass(hooks, TestClass, ...mixins);
});
}

function afterEachFinally() {
unsetContext();

if (DEBUG && ASSERT_DESTROYABLES) {
assertDestroyablesDestroyed();
}
}

export function setupTestClass(hooks, TestClass, ...mixins) {
hooks.beforeEach(function (assert) {
if (DEBUG && ASSERT_DESTROYABLES) {
enableDestroyableTracking();
}

let instance = new TestClass(assert);
this.instance = instance;

Expand Down Expand Up @@ -41,10 +67,10 @@ export function setupTestClass(hooks, TestClass, ...mixins) {
// promise when it is not needed
let filteredPromises = promises.filter(Boolean);
if (filteredPromises.length > 0) {
return all(filteredPromises).finally(() => unsetContext());
return all(filteredPromises).finally(afterEachFinally);
}

unsetContext();
afterEachFinally();
});

if (mixins.length > 0) {
Expand Down
3 changes: 2 additions & 1 deletion packages/internal-test-helpers/lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
next,
run,
} from '@ember/runloop';
import { destroy } from '@glimmer/destroyable';

import { Promise } from 'rsvp';

Expand All @@ -15,7 +16,7 @@ export function runAppend(view: any): void {

export function runDestroy(toDestroy: any): void {
if (toDestroy) {
run(toDestroy, 'destroy');
run(destroy, toDestroy);
}
}

Expand Down