);
}
function GoBackProbe() {
const navigate = useNavigate();
return (
);
}
describe("AutomationsRoute", () => {
let container: HTMLDivElement;
let root: Root;
beforeEach(() => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
});
afterEach(() => {
act(() => root.unmount());
container.remove();
vi.unstubAllGlobals();
});
function locationText() {
return container.querySelector('[data-testid="location"]')?.textContent;
}
function findRowButton(name: string) {
return [...container.querySelectorAll("button")].find((button) =>
button.textContent?.includes(name),
);
}
it("writes the selected automation into the URL so it survives reload and back navigation", async () => {
await act(async () => {
root.render(
,
);
});
expect(locationText()).toBe("/automations");
const row = findRowButton(automationA.name);
expect(row).toBeTruthy();
await act(async () => {
row?.click();
});
expect(
container.querySelector('[data-testid="details-panel"]')?.textContent,
).toBe(automationA.name);
// Selecting a row must push the identity into the address bar — a plain
// local-state selection leaves the URL unchanged, which is exactly the
// bug this test guards against (no way to link/reload/Back to a row).
expect(locationText()).toBe(
`/automations?automationId=${encodeURIComponent(automationIdentity(automationA))}`,
);
const otherRow = findRowButton(automationB.name);
await act(async () => {
otherRow?.click();
});
expect(locationText()).toBe(
`/automations?automationId=${encodeURIComponent(automationIdentity(automationB))}`,
);
});
it("pushes a history entry per row selection so Back steps through prior selections", async () => {
await act(async () => {
root.render(
,
);
});
const goBack = container.querySelector(
'[data-testid="go-back"]',
);
expect(goBack).toBeTruthy();
await act(async () => {
findRowButton(automationA.name)?.click();
});
expect(locationText()).toBe(
`/automations?automationId=${encodeURIComponent(automationIdentity(automationA))}`,
);
await act(async () => {
findRowButton(automationB.name)?.click();
});
expect(locationText()).toBe(
`/automations?automationId=${encodeURIComponent(automationIdentity(automationB))}`,
);
// A row click is an explicit user selection, not URL canonicalization —
// it must push a new history entry so Back steps back through the prior
// selection (A) instead of skipping past the whole page's history in one
// jump. Replacing on every click (the bug this guards against) collapses
// all selections into a single entry.
await act(async () => {
goBack?.click();
});
expect(locationText()).toBe(
`/automations?automationId=${encodeURIComponent(automationIdentity(automationA))}`,
);
await act(async () => {
goBack?.click();
});
expect(locationText()).toBe("/automations");
});
it("reopens the matching detail panel when automationId is already in the URL", async () => {
const targetId = automationIdentity(automationB);
await act(async () => {
root.render(
,
);
});
expect(
container.querySelector('[data-testid="details-panel"]')?.textContent,
).toBe(automationB.name);
});
});