Skip to content

Commit 542324f

Browse files
committed
Tidy unsaved-changes guard tests
1 parent 01117eb commit 542324f

2 files changed

Lines changed: 14 additions & 17 deletions

File tree

test/common/navigate.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,20 @@ describe("navigate", () => {
6666
describe("unsaved changes guard", () => {
6767
const registeredGuards: UnsavedChangesGuard[] = [];
6868

69-
const registerGuard = (isDirty: boolean, promptResult = true) => {
70-
const guard = {
71-
isDirty: vi.fn(() => isDirty),
72-
prompt: vi.fn(async () => promptResult),
73-
};
69+
// Registering through this keeps afterEach able to clean up the module-level
70+
// registry, which outlives the test that filled it.
71+
const trackGuard = <T extends UnsavedChangesGuard>(guard: T): T => {
7472
registerUnsavedChangesGuard(guard);
7573
registeredGuards.push(guard);
7674
return guard;
7775
};
7876

77+
const registerGuard = (isDirty: boolean, promptResult = true) =>
78+
trackGuard({
79+
isDirty: vi.fn(() => isDirty),
80+
prompt: vi.fn(async () => promptResult),
81+
});
82+
7983
beforeEach(() => {
8084
setEntry("/config");
8185
});
@@ -126,17 +130,15 @@ describe("unsaved changes guard", () => {
126130
it("skips a pending prompt once no guard is dirty anymore", async () => {
127131
let dirty = true;
128132
let resolvePrompt!: (value: boolean) => void;
129-
const guard: UnsavedChangesGuard = {
133+
const guard = trackGuard({
130134
isDirty: () => dirty,
131135
prompt: vi.fn(
132136
() =>
133137
new Promise<boolean>((resolve) => {
134138
resolvePrompt = resolve;
135139
})
136140
),
137-
};
138-
registerUnsavedChangesGuard(guard);
139-
registeredGuards.push(guard);
141+
});
140142

141143
const first = navigate("/config/areas");
142144
dirty = false;
@@ -151,17 +153,15 @@ describe("unsaved changes guard", () => {
151153

152154
it("shares one pending prompt between concurrent navigations", async () => {
153155
let resolvePrompt!: (value: boolean) => void;
154-
const guard: UnsavedChangesGuard = {
156+
const guard = trackGuard({
155157
isDirty: () => true,
156158
prompt: vi.fn(
157159
() =>
158160
new Promise<boolean>((resolve) => {
159161
resolvePrompt = resolve;
160162
})
161163
),
162-
};
163-
registerUnsavedChangesGuard(guard);
164-
registeredGuards.push(guard);
164+
});
165165

166166
const first = navigate("/config/areas");
167167
const second = navigate("/config/devices/dashboard");

test/mixins/prevent-unsaved-mixin.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ const setEntry = (path: string) => {
5252
};
5353

5454
const mountClean = async () => {
55-
const element = document.createElement(
56-
"test-prevent-unsaved"
57-
) as TestPreventUnsaved;
55+
const element = document.createElement("test-prevent-unsaved");
5856
document.body.append(element);
5957
element.initialize({ name: "Kitchen" });
6058
await element.updateComplete;
@@ -77,7 +75,6 @@ describe("PreventUnsavedMixin", () => {
7775
document.querySelectorAll("test-prevent-unsaved").forEach((element) => {
7876
element.remove();
7977
});
80-
window.isDirtyState = false;
8178
});
8279

8380
it("blocks navigation while dirty when the prompt is declined", async () => {

0 commit comments

Comments
 (0)