test: Create unit tests for react components in packages/ui/components/alert (tests-alert-component) (#9897)

Co-authored-by: gitstart-calcom <gitstart@users.noreply.github.com>
pull/9549/head^2
GitStart-Cal.com 2023-07-19 16:22:41 +01:00 committed by GitHub
parent 02e34a7bde
commit bd7eadd721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 0 deletions

View File

@ -77,6 +77,7 @@
"@deploysentinel/playwright": "^0.3.3", "@deploysentinel/playwright": "^0.3.3",
"@playwright/test": "^1.31.2", "@playwright/test": "^1.31.2",
"@snaplet/copycat": "^0.3.0", "@snaplet/copycat": "^0.3.0",
"@testing-library/jest-dom": "^5.16.5",
"@types/dompurify": "^2.4.0", "@types/dompurify": "^2.4.0",
"c8": "^7.13.0", "c8": "^7.13.0",
"dotenv-checker": "^1.1.5", "dotenv-checker": "^1.1.5",

View File

@ -23,6 +23,7 @@ export const Alert = forwardRef<HTMLDivElement, AlertProps>((props, ref) => {
return ( return (
<div <div
data-testid="alert"
ref={ref} ref={ref}
className={classNames( className={classNames(
"rounded-md p-3", "rounded-md p-3",
@ -37,6 +38,7 @@ export const Alert = forwardRef<HTMLDivElement, AlertProps>((props, ref) => {
{CustomIcon ? ( {CustomIcon ? (
<div className="flex-shrink-0"> <div className="flex-shrink-0">
<CustomIcon <CustomIcon
data-testid="custom-icon"
aria-hidden="true" aria-hidden="true"
className={classNames(`h-5 w-5`, iconClassName, customIconColor ?? "text-default")} className={classNames(`h-5 w-5`, iconClassName, customIconColor ?? "text-default")}
/> />
@ -45,30 +47,35 @@ export const Alert = forwardRef<HTMLDivElement, AlertProps>((props, ref) => {
<div className="flex-shrink-0"> <div className="flex-shrink-0">
{severity === "error" && ( {severity === "error" && (
<XCircle <XCircle
data-testid="x-circle"
className={classNames("h-5 w-5 text-red-900 dark:text-red-200", iconClassName)} className={classNames("h-5 w-5 text-red-900 dark:text-red-200", iconClassName)}
aria-hidden="true" aria-hidden="true"
/> />
)} )}
{severity === "warning" && ( {severity === "warning" && (
<AlertTriangle <AlertTriangle
data-testid="alert-triangle"
className={classNames("text-attention h-5 w-5 dark:text-orange-200", iconClassName)} className={classNames("text-attention h-5 w-5 dark:text-orange-200", iconClassName)}
aria-hidden="true" aria-hidden="true"
/> />
)} )}
{severity === "info" && ( {severity === "info" && (
<Info <Info
data-testid="info"
className={classNames("h-5 w-5 text-blue-900 dark:text-blue-200", iconClassName)} className={classNames("h-5 w-5 text-blue-900 dark:text-blue-200", iconClassName)}
aria-hidden="true" aria-hidden="true"
/> />
)} )}
{severity === "neutral" && ( {severity === "neutral" && (
<Info <Info
data-testid="neutral"
className={classNames("text-default h-5 w-5 fill-transparent", iconClassName)} className={classNames("text-default h-5 w-5 fill-transparent", iconClassName)}
aria-hidden="true" aria-hidden="true"
/> />
)} )}
{severity === "success" && ( {severity === "success" && (
<CheckCircle2 <CheckCircle2
data-testid="check-circle-2"
className={classNames("fill-muted text-default h-5 w-5", iconClassName)} className={classNames("fill-muted text-default h-5 w-5", iconClassName)}
aria-hidden="true" aria-hidden="true"
/> />

View File

@ -0,0 +1,41 @@
import { render, screen } from "@testing-library/react";
import { Alert } from "./Alert";
describe("Tests for Alert component", () => {
test("Should render text", () => {
render(<Alert severity="info" title="I'm an Alert!" message="Hello World" />);
expect(screen.getByText("I'm an Alert!")).toBeInTheDocument();
expect(screen.getByText("Hello World")).toBeInTheDocument();
});
test("Should render actions", () => {
render(<Alert severity="info" actions={<button>Click Me</button>} />);
expect(screen.getByText("Click Me")).toBeInTheDocument();
});
test("Should render corresponding icon: error", () => {
render(<Alert severity="error" />);
expect(screen.getByTestId("x-circle")).toBeInTheDocument();
});
test("Should render corresponding icon: warning", () => {
render(<Alert severity="warning" />);
expect(screen.getByTestId("alert-triangle")).toBeInTheDocument();
});
test("Should render corresponding icon: info", () => {
render(<Alert severity="info" />);
expect(screen.getByTestId("info")).toBeInTheDocument();
});
test("Should render corresponding icon: neutral", () => {
render(<Alert severity="neutral" />);
expect(screen.getByTestId("neutral")).toBeInTheDocument();
});
test("Should render corresponding icon: success", () => {
render(<Alert severity="success" />);
expect(screen.getByTestId("check-circle-2")).toBeInTheDocument();
});
});

View File

@ -0,0 +1,9 @@
import matchers from "@testing-library/jest-dom/matchers";
import { cleanup } from "@testing-library/react";
import { afterEach, expect } from "vitest";
expect.extend(matchers);
afterEach(() => {
cleanup();
});

View File

@ -27,6 +27,15 @@ const workspaces = packagedEmbedTestsOnly
setupFiles: ["packages/app-store/closecom/test/globals.ts"], setupFiles: ["packages/app-store/closecom/test/globals.ts"],
}, },
}, },
{
test: {
globals: true,
name: "ui/components",
include: ["packages/ui/components/**/*.{test,spec}.{ts,js,tsx}"],
environment: "jsdom",
setupFiles: ["packages/ui/components/test-setup.ts"],
},
},
]; ];
export default defineWorkspace(workspaces); export default defineWorkspace(workspaces);