Skip to content

Commit

Permalink
🧪 test(user-preferences): update theme tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Dec 3, 2023
1 parent 4190e76 commit a423952
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/__tests__/_/api-handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { integrationsApiHandlers } from "./integrations";
import { versionApiHandlers } from "./versions";
import { portalApiHandlers } from "./portal";
import { menuApiHandlers } from "./menu";
import { userPreferencesApiHandlers } from "./user-preferences";

export const apiHandlers = [
...setupApiHandlers,
Expand All @@ -24,4 +25,5 @@ export const apiHandlers = [
...versionApiHandlers,
...portalApiHandlers,
...menuApiHandlers,
...userPreferencesApiHandlers,
];
24 changes: 24 additions & 0 deletions src/__tests__/_/api-handlers/user-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { rest } from "msw";
import { BASE_TEST_URL } from "./_utils";

const USER_PREFERENCES = {
theme: "dark",
};

export const userPreferencesApiHandlers = [
rest.get(
BASE_TEST_URL("/api/user-preferences/:key"),
async (req, res, ctx) => {
return res(
ctx.json({ data: USER_PREFERENCES[req.params.key as string] })
);
}
),
rest.put(
BASE_TEST_URL("/api/user-preferences/:key"),
async (req, res, ctx) => {
USER_PREFERENCES[req.params.key as string] = (await req.json()).data;
return res(ctx.status(201));
}
),
];
4 changes: 3 additions & 1 deletion src/__tests__/account/password.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ describe("pages/account/password", () => {
</ApplicationRoot>
);

await userEvent.click(screen.getByRole("button", { name: "Close Toast" }));

await userEvent.type(
await screen.findByLabelText("Old Password"),
"Old Password"
Expand All @@ -70,7 +72,7 @@ describe("pages/account/password", () => {
screen.getByRole("button", { name: "Update Password" })
);

expect((await screen.findAllByRole("status"))[0]).toHaveTextContent(
expect(await screen.findByRole("status")).toHaveTextContent(
"Password will not be changed on demo account"
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/account/preferences.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("pages/account/preferences", () => {
);

expect(await screen.findByRole("status")).toHaveTextContent(
"Account Preferences Saved Successfully"
"Theme Preference Saved Successfully"
);
});

Expand Down
8 changes: 6 additions & 2 deletions src/__tests__/admin/[entity]/config/presentation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ describe("pages/admin/[entity]/config/presentation", () => {
</ApplicationRoot>
);

await userEvent.click(screen.getByRole("button", { name: "Close Toast" }));

await userEvent.type(screen.getByLabelText("Script"), "invalid");

await userEvent.click(
screen.getByRole("button", {
name: "Save Presentation Scripts",
})
);
expect((await screen.findAllByRole("status"))[0]).toHaveTextContent(
expect(await screen.findByRole("status")).toHaveTextContent(
"Expression: •JS-Error: SyntaxError: Unexpected identifier"
);
});
Expand All @@ -108,14 +110,16 @@ describe("pages/admin/[entity]/config/presentation", () => {
</ApplicationRoot>
);

await userEvent.click(screen.getByRole("button", { name: "Close Toast" }));

await userEvent.clear(screen.getByLabelText("Script"));

await userEvent.click(
screen.getByRole("button", {
name: "Save Presentation Scripts",
})
);
expect((await screen.findAllByRole("status"))[0]).toHaveTextContent(
expect(await screen.findByRole("status")).toHaveTextContent(
"Presentation Scripts Saved Successfully"
);
});
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/admin/settings/theme.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("pages/admin/settings/theme", () => {
);

expect((await screen.findAllByRole("status"))[1]).toHaveTextContent(
"Theme Settings Saved Successfully"
"Theme Preference Saved Successfully"
);
});

Expand Down Expand Up @@ -95,11 +95,11 @@ describe("pages/admin/settings/theme", () => {
);

expect((await screen.findAllByRole("status"))[2]).toHaveTextContent(
"Account Preferences Saved Successfully"
"Theme Settings Saved Successfully"
);

expect((await screen.findAllByRole("status"))[3]).toHaveTextContent(
"Theme Settings Saved Successfully"
"Theme Preference Saved Successfully"
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import { render, screen, waitFor } from "@testing-library/react";
import { ApplicationRoot } from "frontend/components/ApplicationRoot";

import UpdateDashboardWidget from "pages/dashboard/[dashboardId]/widget/[widgetId]/index";
Expand Down Expand Up @@ -110,7 +110,9 @@ describe("pages/dashboard/[dashboardId]/widget/[widgetId]/index", () => {
screen.getByRole("button", { name: "Update Dashboard Widget" })
);

expect(await screen.findAllByRole("status")).toHaveLength(2);
await waitFor(async () => {
expect(await screen.findAllByRole("status")).toHaveLength(2);
});
});

it("should render error when widget is not present", async () => {
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/dashboard/[dashboardId]/widget/create.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import { render, screen, waitFor } from "@testing-library/react";
import { ApplicationRoot } from "frontend/components/ApplicationRoot";

import CreateDashboardWidget from "pages/dashboard/[dashboardId]/widget/create";
Expand Down Expand Up @@ -114,6 +114,8 @@ describe("pages/dashboard/[dashboardId]/widget/create", () => {
screen.getByRole("button", { name: "Create Dashboard Widget" })
);

expect(await screen.findAllByRole("status")).toHaveLength(2);
await waitFor(async () => {
expect(await screen.findAllByRole("status")).toHaveLength(2);
});
});
});
2 changes: 1 addition & 1 deletion src/__tests__/dashboard/manage__actions.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("pages/admin/settings/dashboard", () => {
await within(confirmBox).findByRole("button", { name: "Confirm" })
);

expect((await screen.findAllByRole("status"))[0]).toHaveTextContent(
expect(await screen.findByRole("status")).toHaveTextContent(
"Widget Deleted Successfully"
);

Expand Down
12 changes: 10 additions & 2 deletions src/__tests__/integrations/variables__constants.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ describe("pages/integrations/variables => constants", () => {
</ApplicationRoot>
);

await userEvent.click(
screen.getByRole("button", { name: "Close Toast" })
);

const table = screen.getByRole("table");

const tableRows = await within(table).findAllByRole("row");
Expand All @@ -129,7 +133,7 @@ describe("pages/integrations/variables => constants", () => {
within(dialog).getByRole("button", { name: "Update Constant" })
);

expect((await screen.findAllByRole("status"))[0]).toHaveTextContent(
expect(await screen.findByRole("status")).toHaveTextContent(
"Constant Saved Successfully"
);
});
Expand Down Expand Up @@ -159,6 +163,10 @@ describe("pages/integrations/variables => constants", () => {
</ApplicationRoot>
);

await userEvent.click(
screen.getByRole("button", { name: "Close Toast" })
);

const table = screen.getByRole("table");

const tableRows = await within(table).findAllByRole("row");
Expand All @@ -181,7 +189,7 @@ describe("pages/integrations/variables => constants", () => {

expect(await within(table).findAllByRole("row")).toHaveLength(4);

expect((await screen.findAllByRole("status"))[0]).toHaveTextContent(
expect(await screen.findByRole("status")).toHaveTextContent(
"Constant Deleted Successfully"
);
});
Expand Down
1 change: 0 additions & 1 deletion src/backend/data/data-access/_Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export abstract class BaseDataAccessService<T> {
return query;
}

// :eyes
if (operator !== FilterOperators.IS_NULL && !value) {
return query;
}
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/hooks/auth/preferences.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const userPrefrencesApiPath = (key: UserPreferencesKeys) => {
export const MAKE_USER_PREFERENCE_CRUD_CONFIG = (key: UserPreferencesKeys) => {
return MAKE_CRUD_CONFIG({
path: "N/A",
plural: USER_PREFERENCES_CONFIG[key].label,
singular: USER_PREFERENCES_CONFIG[key].label,
plural: `${USER_PREFERENCES_CONFIG[key].label} Preference `,
singular: `${USER_PREFERENCES_CONFIG[key].label} Preference `,
});
};

Expand Down
1 change: 0 additions & 1 deletion src/frontend/hooks/configuration/configuration.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { AppStorage } from "frontend/lib/storage/app";
import { isRouterParamEnabled } from "..";
import { MAKE_APP_CONFIGURATION_CRUD_CONFIG } from "./configuration.constant";

// :eyes
export const configurationApiPath = (
key: AppConfigurationKeys,
entity?: string,
Expand Down
53 changes: 41 additions & 12 deletions src/frontend/lib/toast/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import toast from "react-hot-toast";

import toast, { Toast } from "react-hot-toast";
import { X } from "react-feather";
import { getBestErrorMessage } from "./utils";

const COLORS = {
Expand All @@ -14,29 +14,53 @@ const toastStyle = (color: keyof typeof COLORS) => ({
maxWidth: "550px",
});

function ToastMessage({ message, toastT }: { message: string; toastT: Toast }) {
return (
<span
style={{
display: "flex",
alignItems: "center",
}}
>
<span>{message}</span>
<X
size="18"
role="button"
style={{
cursor: "pointer",
marginLeft: "12px",
}}
aria-label="Close Toast"
onClick={() => {
toast.dismiss(toastT.id);
}}
/>
</span>
);
}

export const ToastService = {
success: (
message:
| string
| { message: string; action: { label: string; action: () => void } }
) => {
if (typeof message === "string") {
toast.success(message as string, {
toast.success((t) => <ToastMessage message={message} toastT={t} />, {
style: toastStyle("success"),
duration: 7000,
});
return;
}

toast.success(
(t) => (
<div>
<span>{message.message}</span>
<br />
<ToastMessage message={message.message} toastT={t} />
<div
style={{
display: "flex",
justifyContent: "end",
flexDirection: "row",
justifyContent: "center",
}}
>
<button
Expand All @@ -46,13 +70,14 @@ export const ToastService = {
toast.dismiss(t.id);
}}
style={{
cursor: "pointer",
border: 0,
display: "inline-block",
background: "inherit",
padding: 0,
color: COLORS.success,
fontSize: 14,
marginTop: 4,
fontSize: 15,
marginTop: 8,
}}
>
{message.action.label}
Expand All @@ -68,7 +93,11 @@ export const ToastService = {
},

error: (message: unknown) =>
toast.error(getBestErrorMessage(message), {
style: toastStyle("danger"),
}),
toast.error(
(t) => <ToastMessage message={getBestErrorMessage(message)} toastT={t} />,
{
style: toastStyle("danger"),
duration: 7000,
}
),
};
1 change: 0 additions & 1 deletion src/pages/api/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default requestHandler({
return await usersApiController.listUsers();
},

// :eyes
POST: async (getValidatedRequest) => {
const validatedRequest = await getValidatedRequest([
{
Expand Down
2 changes: 0 additions & 2 deletions src/shared/user-preferences/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { z } from "zod";
import { BaseUserPreferencesKeys } from "./base-types";
import { PortalUserPreferencesKeys, PORTAL_CONFIGURATION_KEYS } from "./portal";
import { IUserPreferencesBag } from "./types";
Expand All @@ -15,6 +14,5 @@ export const USER_PREFERENCES_CONFIG: Record<
theme: {
label: "Theme",
defaultValue: "light",
validation: z.enum(["light", "dark"]),
},
};
3 changes: 0 additions & 3 deletions src/shared/user-preferences/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { z } from "zod";

export interface IUserPreferencesBag {
defaultValue: unknown;
label: string;
validation?: z.ZodType;
}

0 comments on commit a423952

Please sign in to comment.