Skip to content

Commit

Permalink
Fix Trying to unregister a portal assertion when immediately unrend…
Browse files Browse the repository at this point in the history
…ering portal

Fixes #616
  • Loading branch information
simonihmig committed May 20, 2023
1 parent 43f0216 commit 6d3f319
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
11 changes: 6 additions & 5 deletions ember-stargate/src/services/-portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default class PortalService extends Service {

unregisterTarget(name: string): void {
this.#targets.delete(name);
this.#portalCount.delete(name);
}

registerPortal(name: string): void {
Expand All @@ -70,11 +71,11 @@ export default class PortalService extends Service {
}

unregisterPortal(name: string): void {
let count = this.#portalCount.get(name) ?? 0;
assert(
`Trying to unregister a portal "${name}" that hasn't been registered before`,
count > 0
);
let count = this.#portalCount.get(name);
if (count === undefined) {
return;
}

count--;
this.#portalCount.set(name, count);
const target = this.#targets.get(name);
Expand Down
4 changes: 3 additions & 1 deletion test-app/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function (defaults) {
let app = new EmberApp(defaults, {
// Add options here
autoImport: {
watchDependencies: ['ember-stargate'],
},
});

// Use `app.import` to add additional libraries to the generated
Expand Down
23 changes: 22 additions & 1 deletion test-app/tests/integration/components/portal-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, settled } from '@ember/test-helpers';
import { render, rerender, settled } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import sinon from 'sinon';

Expand Down Expand Up @@ -316,4 +316,25 @@ module('Integration | Component | portal', function (hooks) {
assert.ok(action.calledWithExactly(2));
assert.strictEqual(action.callCount, 2);
});

test('portal can be immediately unrendered', async function (assert) {
assert.expect(0);

this.set('show', true);
const promise = render(hbs`
{{#if this.show}}
xxx
<PortalTarget @name="main" />
<Portal @target="main">
foo
</Portal>
{{/if}}
`);

await rerender();

this.set('show', false);

await promise;
});
});

0 comments on commit 6d3f319

Please sign in to comment.