Skip to content

Commit

Permalink
fix: reset id counter on cleanup (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlrawlings authored Feb 22, 2020
1 parent 209a21f commit 335ab49
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/__tests__/fixtures/scoped-id.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div role="main" id:scoped="test"/>
2 changes: 2 additions & 0 deletions src/__tests__/fixtures/scoped-id.marko.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare let x: any;
export = x;
9 changes: 9 additions & 0 deletions src/__tests__/render.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import UpdateCounter from "./fixtures/update-counter.marko";
import HelloWorld from "./fixtures/hello-world.marko";
import HelloName from "./fixtures/hello-name.marko";
import Clickable from "./fixtures/clickable.marko";
import ScopedId from "./fixtures/scoped-id.marko";

afterEach(cleanup);

Expand Down Expand Up @@ -143,3 +144,11 @@ test("fireEvent waits for pending updates", async () => {

expect(getByText("Value: 1")).toBeInTheDocument();
});

test("it renders a stable scoped id", async () => {
const r1 = await render(ScopedId);
expect(r1.getByRole("main")).toHaveProperty("id", "c0-test");
cleanup();
const r2 = await render(ScopedId);
expect(r2.getByRole("main")).toHaveProperty("id", "c0-test");
});
9 changes: 9 additions & 0 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export async function render<T extends Template>(

export function cleanup() {
mountedComponents.forEach(destroyComponent);
resetComponentIdCounter();
}

function destroyComponent(component) {
Expand All @@ -132,3 +133,11 @@ function destroyComponent(component) {

mountedComponents.delete(container);
}

function resetComponentIdCounter() {
const counter = (window as any).$MUID;
/* istanbul ignore else */
if (counter) {
counter.i = 0;
}
}

0 comments on commit 335ab49

Please sign in to comment.