Skip to content

Commit

Permalink
Add tests for memory leak prevention coming from lastGlobalRef (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
tniezurawski committed Aug 2, 2023
1 parent a199e47 commit 1492778
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export function nodeFor(context, name) {
return bucketFor(context).get(name);
}

export { resolveGlobalRef };

function maybeReturnCreated(value, createdValues, fn, ctx) {
if (value === null || value === undefined) {
return null;
Expand Down
35 changes: 34 additions & 1 deletion tests/integration/modifiers/create-ref-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, find, waitUntil } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { ref, globalRef, nodeFor } from 'ember-ref-bucket';
import { ref, globalRef, nodeFor, resolveGlobalRef } from 'ember-ref-bucket';

module('Integration | Modifier | create-ref', function (hooks) {
setupRenderingTest(hooks);

hooks.after(function (assert) {
assert.equal(
resolveGlobalRef(),
null,
'global ref is cleared after all tests done'
);
});

// Replace this with your real tests.
test('it renders', async function (assert) {
class Item {
Expand Down Expand Up @@ -128,4 +136,29 @@ module('Integration | Modifier | create-ref', function (hooks) {
this.set('isToggled', false);
assert.equal(nodeFor(this.owner, 'foo').textContent, 'ember');
});

test('it keeps proper reference to lastGlobalRef', async function (assert) {
this.set('isHidden', false);
await render(
hbs`<div {{create-global-ref "foo"}}>stable</div>{{#if this.isHidden}}<div {{create-global-ref "bar"}}>unstable</div>{{/if}}`
);
assert.equal(nodeFor(this.owner, 'foo').textContent, 'stable');
assert.notOk(nodeFor(this.owner, 'bar'), 'ref to "bar" does NOT exist');
assert.equal(
nodeFor(resolveGlobalRef(), 'foo').outerHTML,
'<div>stable</div>'
);

this.set('isHidden', true);
assert.equal(nodeFor(this.owner, 'foo').textContent, 'stable');
assert.equal(nodeFor(this.owner, 'bar').textContent, 'unstable');
assert.equal(
nodeFor(resolveGlobalRef(), 'foo').outerHTML,
'<div>stable</div>'
);
assert.equal(
nodeFor(resolveGlobalRef(), 'bar').outerHTML,
'<div>unstable</div>'
);
});
});

0 comments on commit 1492778

Please sign in to comment.