Skip to content

Commit

Permalink
Setup basic infrastructure to ensure destroyables destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Oct 14, 2021
1 parent 41fb8fe commit 632f020
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
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

0 comments on commit 632f020

Please sign in to comment.