Create RTL tests for Meta component (#10478)

Co-authored-by: gitstart-calcom <gitstart@users.noreply.github.com>
Co-authored-by: Shivam Kalra <shivamkalra98@gmail.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
chore/app-dir-exploration
GitStart-Cal.com 2023-08-18 07:58:30 -03:00 committed by GitHub
parent 2403d77166
commit 761f66fc5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,54 @@
import { render } from "@testing-library/react";
import { vi } from "vitest";
import Meta, { useMeta } from "./Meta";
vi.mock("./Meta", async () => {
const actualMeta = (await vi.importActual("./Meta")) as object;
const MockMeta = ({ title, description }: { title: string; description: string }) => (
<div>
<h1>{title}</h1>
<p>{description}</p>
</div>
);
type MetaProviderProps = {
children: React.ReactNode;
};
return {
...actualMeta,
default: MockMeta,
useMeta: vi.fn(),
MetaProvider: ({ children }: MetaProviderProps) => children,
};
});
describe("Meta Component", () => {
test("Should render with default metadata", () => {
(useMeta as jest.Mock).mockReturnValue({
meta: { title: "", description: "", backButton: false, CTA: null },
});
const { getByText } = render(<Meta title="Page Title" description="Page Description" />);
expect(getByText("Page Title")).toBeInTheDocument();
expect(getByText("Page Description")).toBeInTheDocument();
});
test("Should update metadata when props change", () => {
(useMeta as jest.Mock).mockReturnValue({
meta: { title: "", description: "", backButton: false, CTA: null },
});
const { rerender, getByText } = render(<Meta title="Page Title" description="Page Description" />);
expect(getByText("Page Title")).toBeInTheDocument();
expect(getByText("Page Description")).toBeInTheDocument();
rerender(<Meta title="New Title" description="New Description" />);
expect(getByText("New Title")).toBeInTheDocument();
expect(getByText("New Description")).toBeInTheDocument();
});
});