test: simplify publish component scenarios

This commit is contained in:
Oleg Isonen committed 2026-11-15 12:15:01 +00:00
1 parent 8c861628c2
commit cdb1a52b0e
5 files changed
+55 -229

No files matched your search

@@ -1,5 +1,4 @@
import { act } from "react-dom/test-utils";
import { userEvent } from "@vitest/browser/context";
import { createRoot, type Root } from "react-dom/client";
import {
afterAll,
@@ -22,7 +21,6 @@ const originalScrollIntoView = Object.getOwnPropertyDescriptor(
);
let root: Root | undefined;
let resetStores: (() => void) | undefined;
let publishModule: typeof import("./publish");
let stores: typeof import("~/shared/sync/data-stores");
let nanoStates: typeof import("~/shared/nano-states");
@@ -31,7 +29,6 @@ const requests: Array<{ path: string; input: unknown }> = [];
let publishResponse: unknown = { success: false, error: "Publish blocked" };
let jobStatus: "failed" | "success" = "failed";
let didPublish = false;
const logCursors: Array<number | undefined> = [];
let interceptor: ReturnType<typeof createTrpcInterceptor>;
beforeAll(async () => {
@@ -49,31 +46,6 @@ beforeAll(async () => {
"api.publish.getJob": () => {
return { status: jobStatus, code: "BUILD_FAILED" };
},
"api.publish.getLog": (input) => {
const cursor = (input as { cursor?: number }).cursor;
logCursors.push(cursor);
return {
availability: "available",
startedAt: "2026-11-13T10:00:00.000Z",
completedAt: "2026-11-13T10:00:03.000Z",
log: {
encoding: "base64url",
chunk: btoa(
String.fromCharCode(
...(cursor === undefined ? [0xe2] : [0x82, 0xac])
)
),
complete: cursor !== undefined,
nextCursor: cursor === undefined ? 1 : undefined,
issues: [
{
severity: cursor === undefined ? "warning" : "error",
message: cursor === undefined ? "Early warning" : "Late error",
},
],
},
};
},
"domain.project": () => {
return {
success: true,
@@ -118,21 +90,13 @@ beforeEach(() => {
requests.length = 0;
jobStatus = "failed";
didPublish = false;
logCursors.length = 0;
nanoStates.$planFeatures.set({
...nanoStates.$planFeatures.get(),
allowPublishLogs: false,
});
});
test("a successful site runner finishes the current publish and retains its report", async () => {
jobStatus = "success";
publishResponse = {
success: true,
buildId: "build-1",
logHandle: "log-handle",
findings: [],
};
const mount = async (dialog: "publish" | "export" = "publish") => {
stores.$project.set({
id: "project-1",
domain: "example",
@@ -144,11 +108,7 @@ test("a successful site runner finishes the current publish and retains its repo
canCopy: true,
canPublish: true,
});
builderStates.$publishDialog.set("publish");
resetStores = () => {
stores.$project.set(undefined);
builderStates.$publishDialog.set("none");
};
builderStates.$publishDialog.set(dialog);
const container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
@@ -159,6 +119,17 @@ test("a successful site runner finishes the current publish and retains its repo
</TooltipProvider>
)
);
};
test("a successful site runner finishes the current publish and retains its report", async () => {
jobStatus = "success";
publishResponse = {
success: true,
buildId: "build-1",
logHandle: "log-handle",
findings: [],
};
await mount();
const domain = document.body.querySelector<HTMLInputElement>(
'input[name="domainToPublish[]"]'
)!;
@@ -167,6 +138,7 @@ test("a successful site runner finishes the current publish and retains its repo
document.body.querySelector<HTMLElement>('[role="checkbox"]')?.click()
);
}
const publish = Array.from(
document.body.querySelectorAll<HTMLButtonElement>("button")
)
@@ -205,11 +177,16 @@ test("a successful site runner finishes the current publish and retains its repo
afterEach(() => {
act(() => {
root?.unmount();
resetStores?.();
stores.$project.set(undefined);
builderStates.$publishDialog.set("none");
});
root = undefined;
resetStores = undefined;
document.body.innerHTML = "";
nanoStates.$authTokenPermissions.set({
canClone: true,
canCopy: true,
canPublish: false,
});
nanoStates.$planFeatures.set({
...nanoStates.$planFeatures.get(),
allowPublishLogs: false,
@@ -217,91 +194,6 @@ afterEach(() => {
vi.restoreAllMocks();
});
test("a paid publish loads every log page and hides it after downgrade", async () => {
jobStatus = "success";
publishResponse = {
success: true,
buildId: "build-1",
logHandle: "log-handle",
findings: [],
};
stores.$project.set({
id: "project-1",
domain: "example",
domainsVirtual: [],
latestStaticBuild: null,
} as unknown as Project);
nanoStates.$authTokenPermissions.set({
canClone: true,
canCopy: true,
canPublish: true,
});
nanoStates.$planFeatures.set({
...nanoStates.$planFeatures.get(),
allowPublishLogs: true,
});
builderStates.$publishDialog.set("publish");
resetStores = () => {
stores.$project.set(undefined);
builderStates.$publishDialog.set("none");
};
const container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
await act(async () =>
root?.render(
<TooltipProvider>
<publishModule.PublishButton projectId="project-1" />
</TooltipProvider>
)
);
const domain = document.body.querySelector<HTMLInputElement>(
'input[name="domainToPublish[]"]'
)!;
if (!domain.checked) {
await act(async () =>
document.body.querySelector<HTMLElement>('[role="checkbox"]')?.click()
);
}
const publish = Array.from(
document.body.querySelectorAll<HTMLButtonElement>("button")
)
.filter((button) => button.textContent === "Publish")
.at(-1)!;
await act(async () => publish.click());
await act(async () =>
vi.waitFor(() => expect(logCursors).toEqual([undefined, 1]))
);
const report = document.body.querySelector<HTMLButtonElement>(
'button[aria-label^="Publish log:"]'
)!;
await act(async () => report.click());
expect(document.body.textContent).toContain("€");
const selectTab = async (name: string) => {
const tab = Array.from(
document.body.querySelectorAll<HTMLElement>('[role="tab"]')
).find((candidate) => candidate.textContent?.startsWith(name));
expect(tab).toBeDefined();
await act(async () => userEvent.click(tab!));
};
await selectTab("Errors");
expect(document.body.textContent).toContain("Late error");
await selectTab("Warnings");
expect(document.body.textContent).toContain("Early warning");
await selectTab("Publish");
await act(async () =>
nanoStates.$planFeatures.set({
...nanoStates.$planFeatures.get(),
allowPublishLogs: false,
})
);
expect(document.body.textContent).not.toContain("€");
expect(document.body.textContent).toContain(
"Upgrade to Pro to view publish logs."
);
});
afterAll(() => {
interceptor.stop();
vi.resetModules();
@@ -318,38 +210,7 @@ afterAll(() => {
});
test("static export reaches the server without waiting for diagnostics", async () => {
requests.length = 0;
stores.$project.set({
id: "project-1",
domain: "example",
domainsVirtual: [],
latestStaticBuild: null,
} as unknown as Project);
nanoStates.$authTokenPermissions.set({
canClone: true,
canCopy: true,
canPublish: true,
});
builderStates.$publishDialog.set("export");
resetStores = () => {
stores.$project.set(undefined);
nanoStates.$authTokenPermissions.set({
canClone: true,
canCopy: true,
canPublish: false,
});
builderStates.$publishDialog.set("none");
};
const container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
await act(async () => {
root?.render(
<TooltipProvider>
<publishModule.PublishButton projectId="project-1" />
</TooltipProvider>
);
});
await mount("export");
const destination = document.body.querySelector<HTMLButtonElement>(
'button[role="combobox"]'
@@ -400,32 +261,7 @@ test("static export reaches the server without waiting for diagnostics", async (
}, 10_000);
test("keeps Diagnostics visible but disabled before there is anything to show", async () => {
stores.$project.set({
id: "project-1",
domain: "example",
domainsVirtual: [],
latestStaticBuild: null,
} as unknown as Project);
nanoStates.$authTokenPermissions.set({
canClone: true,
canCopy: true,
canPublish: true,
});
builderStates.$publishDialog.set("publish");
resetStores = () => {
stores.$project.set(undefined);
builderStates.$publishDialog.set("none");
};
const container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
await act(async () => {
root?.render(
<TooltipProvider>
<publishModule.PublishButton projectId="project-1" />
</TooltipProvider>
);
});
await mount();
const options = document.body.querySelector<HTMLButtonElement>(
'button[aria-label="Publish options"]'
);
@@ -461,32 +297,7 @@ test.each([
findings: [],
};
}
stores.$project.set({
id: "project-1",
domain: "example",
domainsVirtual: [],
latestStaticBuild: null,
} as unknown as Project);
nanoStates.$authTokenPermissions.set({
canClone: true,
canCopy: true,
canPublish: true,
});
builderStates.$publishDialog.set("publish");
resetStores = () => {
stores.$project.set(undefined);
builderStates.$publishDialog.set("none");
};
const container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
await act(async () => {
root?.render(
<TooltipProvider>
<publishModule.PublishButton projectId="project-1" />
</TooltipProvider>
);
});
await mount();
const domain = document.body.querySelector<HTMLInputElement>(
'input[name="domainToPublish[]"]'
@@ -14,7 +14,11 @@ import { TooltipProvider, toast } from "@webstudio-is/design-system";
import { createAssetContentSession } from "@webstudio-is/content-engine/asset-content-session";
import type { Project } from "@webstudio-is/project";
import type { Pages } from "@webstudio-is/sdk";
import { createTrpcInterceptor, deferred } from "./publish-test-utils";
import {
createTrpcInterceptor,
deferred,
selectTab,
} from "./publish-test-utils";
let checks = deferred<unknown>();
let dispatch = deferred<unknown>();
@@ -260,9 +264,7 @@ const openErrors = async (target: "site" | "static") => {
document.body.querySelectorAll<HTMLElement>('[role="tab"]')
).find((tab) => tab.textContent?.startsWith("Errors"));
await act(async () => vi.waitFor(() => expect(findErrors()).toBeDefined()));
const errors = findErrors();
expect(errors).toBeDefined();
await act(async () => userEvent.click(errors!));
await selectTab("Errors");
return document.body.querySelector<HTMLElement>(
'[role="tabpanel"][data-state="active"]'
);
@@ -13,7 +13,11 @@ import {
import { TooltipProvider, toast } from "@webstudio-is/design-system";
import type { Project } from "@webstudio-is/project";
import { createAssetContentSession } from "@webstudio-is/content-engine/asset-content-session";
import { createTrpcInterceptor, deferred } from "./publish-test-utils";
import {
createTrpcInterceptor,
deferred,
selectTab,
} from "./publish-test-utils";
const reactEnvironment = globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean };
const originalActEnvironment = reactEnvironment.IS_REACT_ACT_ENVIRONMENT;
@@ -231,6 +235,7 @@ const toggle = async (value: "publish" | "none" | "export") => {
}
}
};
const waitFor = async (check: () => void) => {
await vi.waitFor(async () => {
await act(async () => {});
@@ -249,11 +254,7 @@ const openReport = async (tab: "Errors" | "Warnings") => {
await waitFor(() =>
expect(document.body.querySelector('[role="tablist"]')).not.toBeNull()
);
const target = Array.from(
document.body.querySelectorAll<HTMLElement>('[role="tab"]')
).find((item) => item.textContent?.startsWith(tab));
expect(target).toBeDefined();
await act(async () => userEvent.click(target!));
await selectTab(tab);
const report = document.body.querySelector(
'[role="tabpanel"][data-state="active"]'
);
@@ -1,6 +1,5 @@
import { act } from "react-dom/test-utils";
import { createRoot, type Root } from "react-dom/client";
import { userEvent } from "@vitest/browser/context";
import {
afterAll,
afterEach,
@@ -12,7 +11,11 @@ import {
} from "vitest";
import { TooltipProvider } from "@webstudio-is/design-system";
import type { Project } from "@webstudio-is/project";
import { createTrpcInterceptor, deferred } from "./publish-test-utils";
import {
createTrpcInterceptor,
deferred,
selectTab,
} from "./publish-test-utils";
const encoded = (bytes: number[]) => btoa(String.fromCharCode(...bytes));
const logRequests: Array<{
@@ -248,6 +251,7 @@ const panel = () =>
document.body.querySelector<HTMLElement>(
'[role="tabpanel"][data-state="active"]'
)!;
const waitFor = async (assertion: () => void) => {
await vi.waitFor(async () => {
await act(async () => {});
@@ -305,10 +309,7 @@ test("Free does not fetch logs; upgrading reads UTF-8 from byte zero and preserv
["Errors", "Late error"],
["Warnings", "Early warning"],
]) {
const tab = Array.from(
document.body.querySelectorAll<HTMLElement>('[role="tab"]')
).find((candidate) => candidate.textContent?.startsWith(name))!;
await act(async () => userEvent.click(tab));
await selectTab(name);
expect(panel().textContent).toContain(message);
// Issue truncation is returned by the runner, not inferred from visible issues.
expect(panel().textContent).toContain("1 more issues not shown.");
@@ -1,4 +1,15 @@
import { FetchInterceptor } from "@mswjs/interceptors/fetch";
import { userEvent } from "@vitest/browser/context";
import { act } from "react-dom/test-utils";
import { expect } from "vitest";
export const selectTab = async (name: string) => {
const tab = Array.from(
document.body.querySelectorAll<HTMLElement>('[role="tab"]')
).find((candidate) => candidate.textContent?.startsWith(name));
expect(tab).toBeDefined();
await act(async () => userEvent.click(tab!));
};
export const deferred = <T>() => {
let resolve!: (value: T) => void;