Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,220 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { MemoryRouter } from "react-router";
|
||||
|
||||
vi.mock("@toolctrl/db/notifications.server", () => ({
|
||||
getUserNotificationPreferences: vi.fn(),
|
||||
upsertUserNotificationPreference: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/utils/loaders.middleware", () => ({
|
||||
requireAuth: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/utils/services/.server/urlService", () => ({
|
||||
getTenantIdFromUrl: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
getUserInfo: vi.fn(),
|
||||
validateCSRFToken: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/utils/data/useRootData", () => ({
|
||||
useRootData: () => ({ csrf: "csrf-test-token" }),
|
||||
}));
|
||||
const routeTranslations: Record<string, string> = {
|
||||
"settings.notifications.heading": "Notification preferences",
|
||||
"settings.notifications.description": "Choose which workspace updates you receive in-app and by email. Preferences are scoped to your current workspace and user account.",
|
||||
"settings.notifications.saved": "Notification preference saved.",
|
||||
"settings.notifications.categories.default": "Default",
|
||||
"settings.notifications.categories.billing": "Billing",
|
||||
"settings.notifications.categories.team": "Team",
|
||||
"settings.notifications.channels.inApp": "In-app",
|
||||
"settings.notifications.channels.email": "Email",
|
||||
"settings.notifications.errors.invalidPreferenceAction": "Invalid notification preference action.",
|
||||
"settings.notifications.errors.invalidPreferenceCategory": "Invalid notification preference category.",
|
||||
"settings.notifications.errors.invalidPreferenceChannel": "Invalid notification preference channel.",
|
||||
"shared.save": "Save",
|
||||
};
|
||||
vi.mock("~/locale/i18next.server", () => ({
|
||||
getTranslations: vi.fn(async () => ({
|
||||
t: (key: string) => routeTranslations[key] ?? key,
|
||||
})),
|
||||
}));
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string, options?: Record<string, string>) => {
|
||||
if (key === "settings.notifications.categoryDescription") {
|
||||
return `Manage ${options?.category ?? ""} workspace notifications.`;
|
||||
}
|
||||
return routeTranslations[key] ?? key;
|
||||
},
|
||||
}),
|
||||
}));
|
||||
vi.mock("react-router", async () => {
|
||||
const actual = await vi.importActual<typeof import("react-router")>("react-router");
|
||||
return {
|
||||
...actual,
|
||||
useActionData: () => routeActionData,
|
||||
useLoaderData: () => routeLoaderData,
|
||||
};
|
||||
});
|
||||
|
||||
import { getUserNotificationPreferences, upsertUserNotificationPreference } from "@toolctrl/db/notifications.server";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
import { getUserInfo } from "~/utils/session.server";
|
||||
import WorkspaceNotificationSettingsRoute, { action, loader } from "./notifications";
|
||||
|
||||
let routeLoaderData: unknown;
|
||||
let routeActionData: unknown;
|
||||
|
||||
const request = new Request("https://app.example.test/acme/settings/notifications");
|
||||
const params = { tenant: "acme" };
|
||||
|
||||
function routeArgs(customRequest: Request = request) {
|
||||
return { request: customRequest, params, context: {}, unstable_pattern: "app.$tenant/settings/notifications" };
|
||||
}
|
||||
|
||||
function postFormRequest(formData: FormData) {
|
||||
return new Request(request.url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams(formData as unknown as Record<string, string>),
|
||||
});
|
||||
}
|
||||
|
||||
function preferenceForm(overrides?: Record<string, string>) {
|
||||
const form = new FormData();
|
||||
form.set("action", "update-preference");
|
||||
form.set("category", "billing");
|
||||
form.set("channel", "EMAIL");
|
||||
form.set("enabled", "on");
|
||||
Object.entries(overrides ?? {}).forEach(([key, value]) => form.set(key, value));
|
||||
return form;
|
||||
}
|
||||
|
||||
describe("workspace notification settings route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(getTenantIdFromUrl).mockResolvedValue("tenant_1");
|
||||
vi.mocked(getUserInfo).mockResolvedValue({ userId: "user_1" } as never);
|
||||
vi.mocked(getUserNotificationPreferences).mockResolvedValue([]);
|
||||
vi.mocked(upsertUserNotificationPreference).mockResolvedValue({ id: "pref_1" });
|
||||
routeLoaderData = {
|
||||
preferences: {
|
||||
default: { IN_APP: true, EMAIL: true },
|
||||
billing: { IN_APP: true, EMAIL: false },
|
||||
team: { IN_APP: false, EMAIL: true },
|
||||
},
|
||||
};
|
||||
routeActionData = undefined;
|
||||
});
|
||||
|
||||
it("requires auth and loads notification preferences only for the current tenant and user", async () => {
|
||||
vi.mocked(getUserNotificationPreferences).mockResolvedValue([
|
||||
{ tenantId: "tenant_1", userId: "user_1", category: "billing", channel: "EMAIL", enabled: false },
|
||||
{ tenantId: "tenant_1", userId: "user_1", category: "team", channel: "IN_APP", enabled: false },
|
||||
{ tenantId: "tenant_1", userId: "user_1", category: "unknown", channel: "EMAIL", enabled: false },
|
||||
] as never);
|
||||
|
||||
const response = (await loader(routeArgs())) as Response;
|
||||
const body = await response.json();
|
||||
|
||||
expect(requireAuth).toHaveBeenCalledWith({ request, params });
|
||||
expect(getTenantIdFromUrl).toHaveBeenCalledWith(params);
|
||||
expect(getUserInfo).toHaveBeenCalledWith(request);
|
||||
expect(getUserNotificationPreferences).toHaveBeenCalledWith({ tenantId: "tenant_1", userId: "user_1" });
|
||||
expect(body).toEqual({
|
||||
preferences: {
|
||||
default: { IN_APP: true, EMAIL: true },
|
||||
billing: { IN_APP: true, EMAIL: false },
|
||||
team: { IN_APP: false, EMAIL: true },
|
||||
},
|
||||
});
|
||||
expect(JSON.stringify(body)).not.toContain("unknown");
|
||||
});
|
||||
|
||||
it("shows category preferences using the same default fallback as the worker", async () => {
|
||||
vi.mocked(getUserNotificationPreferences).mockResolvedValue([{ tenantId: "tenant_1", userId: "user_1", category: "default", channel: "EMAIL", enabled: false }] as never);
|
||||
|
||||
const response = (await loader(routeArgs())) as Response;
|
||||
const body = await response.json();
|
||||
|
||||
expect(body).toEqual({
|
||||
preferences: {
|
||||
default: { IN_APP: true, EMAIL: false },
|
||||
billing: { IN_APP: true, EMAIL: false },
|
||||
team: { IN_APP: true, EMAIL: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("returns bounded 400 JSON for an invalid category without calling upsert", async () => {
|
||||
const form = preferenceForm({ category: "security" });
|
||||
|
||||
const response = (await action(routeArgs(postFormRequest(form)))) as Response;
|
||||
const body = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(body).toEqual({ error: "Invalid notification preference category." });
|
||||
expect(upsertUserNotificationPreference).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns bounded 400 JSON for an invalid notification action without calling upsert", async () => {
|
||||
const form = preferenceForm({ action: "delete-preference" });
|
||||
|
||||
const response = (await action(routeArgs(postFormRequest(form)))) as Response;
|
||||
const body = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(body).toEqual({ error: "Invalid notification preference action." });
|
||||
expect(upsertUserNotificationPreference).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns bounded 400 JSON for an invalid channel without calling upsert", async () => {
|
||||
const form = preferenceForm({ channel: "SMS" });
|
||||
|
||||
const response = (await action(routeArgs(postFormRequest(form)))) as Response;
|
||||
const body = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(body).toEqual({ error: "Invalid notification preference channel." });
|
||||
expect(upsertUserNotificationPreference).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("writes a valid preference update with tenant, user, normalized category, channel, and enabled state", async () => {
|
||||
const form = preferenceForm({ category: " Team ", channel: "in-app", enabled: "true" });
|
||||
|
||||
const response = (await action(routeArgs(postFormRequest(form)))) as Response;
|
||||
const body = await response.json();
|
||||
|
||||
expect(requireAuth).toHaveBeenCalledWith({ request: expect.any(Request), params });
|
||||
expect(getTenantIdFromUrl).toHaveBeenCalledWith(params);
|
||||
expect(getUserInfo).toHaveBeenCalledWith(expect.any(Request));
|
||||
expect(upsertUserNotificationPreference).toHaveBeenCalledWith({
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
channel: "IN_APP",
|
||||
category: "team",
|
||||
enabled: true,
|
||||
});
|
||||
expect(body).toEqual({ ok: true });
|
||||
});
|
||||
|
||||
it("renders compact controls for default, billing, and team notification preferences", () => {
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<WorkspaceNotificationSettingsRoute />
|
||||
</MemoryRouter>,
|
||||
);
|
||||
|
||||
expect(screen.getByRole("heading", { name: "Notification preferences" })).toBeInTheDocument();
|
||||
expect(screen.getByText("Preferences are scoped to your current workspace and user account.", { exact: false })).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: "Default" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: "Billing" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: "Team" })).toBeInTheDocument();
|
||||
expect(screen.getAllByLabelText("In-app")).toHaveLength(3);
|
||||
expect(screen.getAllByLabelText("Email")).toHaveLength(3);
|
||||
expect(screen.getByLabelText("Email", { selector: "input#notification-billing-EMAIL" })).not.toBeChecked();
|
||||
expect(screen.getByLabelText("In-app", { selector: "input#notification-team-IN_APP" })).not.toBeChecked();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user