Skip to content

Commit

Permalink
Did I reproduce a different issue?
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Nov 13, 2024
1 parent 31d208f commit 2202359
Showing 1 changed file with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ export class GlimmerishComponents extends RenderTest {
@test({ kind: 'glimmer' }) 'destruction is not autotracked'() {
class State {
@tracked willDestroyCalls = 0;
increment = () => this.willDestroyCalls++;
incrementWillDestroy = () => this.willDestroyCalls++;
}
let state = new State();
class Child extends GlimmerishComponent {
Expand All @@ -884,26 +884,62 @@ export class GlimmerishComponents extends RenderTest {
this.registerComponent(
'Glimmer',
'Example',
`
<p>willDestroyCalls: {{@willDestroyCalls}}</p>
`<p>willDestroyCalls: {{@willDestroyCalls}}</p>
<button {{on "click" this.toggleChild}}>Toggle child</button>
{{#if this.showChild}}
<Child @incrementWillDestroy={{@incrementWillDestroy}} />
{{/if}}
`,
{{/if}}`,
Example
);

this.render(
'<Example @incrementWillDestroy={{this.state.increment}} @willDestroyCalls={{this.state.willDestroyCalls}} />',
`<Example
@incrementWillDestroy={{this.state.incrementWillDestroy}}
@willDestroyCalls={{this.state.willDestroyCalls}}
/>`,
{ state }
);

this.assert.strictEqual(this.takeSnapshot(), '..');
this.assert.strictEqual(state.willDestroyCalls, 0);
this.assertHTML('', 'p', 'destroys correctly');
// Helper because assertHTML is invisible-character sensitive, and this test doesn't care about
// that.
// Where is qunit-dom?
let output = (calls: number, hasChild: boolean) => {
if (hasChild) {
return `<p>willDestroyCalls: ${calls}</p>
<button>Toggle child</button>
a child
`;
}
return `<p>willDestroyCalls: ${calls}</p>
<button>Toggle child</button>
<!---->
`;
};

const el = () => this.element as unknown as HTMLElement;
const click = () => {
el().querySelector('button')?.click();
this.rerender();
};

this.assert.strictEqual(state.willDestroyCalls, 0, '0 destructions');
this.assertHTML(output(0, true), 'initial render');

click();
this.assert.strictEqual(state.willDestroyCalls, 1, '1 destruction');
this.assertHTML(output(1, false), 'destroyed once');

click();
this.assert.strictEqual(state.willDestroyCalls, 1, '1 destruction');
this.assertHTML(output(1, true), 'shown again, no change');

click();
this.assert.strictEqual(state.willDestroyCalls, 2, '2 destruction');
this.assertHTML(output(2, false), 'destroyed twice');

this.destroy();
this.assertHTML('', 'destroys correctly');
}

Expand Down

0 comments on commit 2202359

Please sign in to comment.