fix: Use origin as provided in HeadSeo. origin would be different for Domain cases (#14935)

* fix: Use origin as provided in HeadSeo. origin would be different for Domain cases

* Add a test

* add more test

* Add tests

* Cleanup tests

* Add an e2e as well

* fix-test

* Reset mocks
This commit is contained in:
Hariom Balhara authored and GitHub committed 2024-05-23 11:47:46 +01:00
1 parent 29ccc58806
commit 5f5cf92456
16 files changed
+730 -27

No files matched your search

+3 -1
View File
@@ -18,6 +18,7 @@ export type HeadSeoProps = {
app?: AppImageProps;
meeting?: MeetingImageProps;
isBrandingHidden?: boolean;
origin?: string;
};
/**
@@ -69,10 +70,11 @@ const buildSeoMeta = (pageProps: {
export const HeadSeo = (props: HeadSeoProps): JSX.Element => {
const path = usePathname();
// The below code sets the defaultUrl for our canonical tags
// Get the router's path
// Set the default URL to either the current URL (if self-hosted) or https://cal.com canonical URL
const defaultUrl = buildCanonical({ path, origin: CAL_URL });
const defaultUrl = buildCanonical({ path, origin: props.origin || CAL_URL });
const {
title,
@@ -1,11 +1,27 @@
import { render, waitFor } from "@testing-library/react";
import type { NextSeoProps } from "next-seo";
import type { OpenGraph } from "next/dist/lib/metadata/types/opengraph-types";
import { usePathname } from "next/navigation";
import { vi } from "vitest";
import { CAL_URL } from "@calcom/lib/constants";
import type { HeadSeoProps } from "./HeadSeo";
import HeadSeo from "./HeadSeo";
vi.mock("next/navigation", () => {
return {
usePathname: vi.fn(),
};
});
vi.mock("@calcom/lib/constants", () => {
return {
SEO_IMG_DEFAULT: "",
SEO_IMG_OGIMG: "",
APP_NAME: "Cal.com",
CAL_URL: "http://cal.com",
};
});
vi.mock("next-seo", () => {
return {
NextSeo: (props: NextSeoProps) => {
@@ -16,7 +32,7 @@ vi.mock("next-seo", () => {
image: images[0].url,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
return <title {...mockedProps} />;
return <div id="mocked-next-seo" {...mockedProps} />;
},
};
});
@@ -48,43 +64,97 @@ describe("Tests for HeadSeo component", () => {
test("Should render mocked NextSeo", async () => {
const { container } = render(<HeadSeo {...basicProps} />);
await waitFor(async () => {
const titleEl = container.querySelector("title");
expect(titleEl?.getAttribute("canonical")).toEqual(basicProps.canonical);
expect(titleEl?.getAttribute("description")).toEqual(basicProps.description);
expect(titleEl?.getAttribute("site_name")).toEqual(basicProps.siteName);
expect(titleEl?.getAttribute("image")).toContain("constructGenericImage");
const mockedNextSeoEl = container.querySelector("#mocked-next-seo");
expect(mockedNextSeoEl?.getAttribute("canonical")).toEqual(basicProps.canonical);
expect(mockedNextSeoEl?.getAttribute("description")).toEqual(basicProps.description);
expect(mockedNextSeoEl?.getAttribute("site_name")).toEqual(basicProps.siteName);
expect(mockedNextSeoEl?.getAttribute("image")).toContain("constructGenericImage");
});
});
describe("Canonical Url", () => {
test("Should provide `canonical` prop to NextSeo derived from `origin` prop if provided", async () => {
const props = {
title: "Test Title",
description: "Test Description",
origin: "http://acme.cal.local",
siteName: "Cal.com",
};
vi.mocked(usePathname).mockReturnValue("/mocked-path");
const { container } = render(<HeadSeo {...props} />);
await waitFor(async () => {
const mockedNextSeoEl = container.querySelector("#mocked-next-seo");
expect(mockedNextSeoEl?.getAttribute("canonical")).toEqual(`${props.origin}/mocked-path`);
expect(mockedNextSeoEl?.getAttribute("description")).toEqual(props.description);
expect(mockedNextSeoEl?.getAttribute("site_name")).toEqual(props.siteName);
expect(mockedNextSeoEl?.getAttribute("image")).toContain("constructGenericImage");
});
});
test("Should provide `canonical` prop to NextSeo derived from `CAL_URL` prop if origin not provided", async () => {
const props = {
title: "Test Title",
description: "Test Description",
siteName: "Cal.com",
};
vi.mocked(usePathname).mockReturnValue("/mocked-path");
const { container } = render(<HeadSeo {...props} />);
await waitFor(async () => {
const mockedNextSeoEl = container.querySelector("#mocked-next-seo");
expect(mockedNextSeoEl?.getAttribute("canonical")).toEqual(`${CAL_URL}/mocked-path`);
expect(mockedNextSeoEl?.getAttribute("description")).toEqual(props.description);
expect(mockedNextSeoEl?.getAttribute("site_name")).toEqual(props.siteName);
expect(mockedNextSeoEl?.getAttribute("image")).toContain("constructGenericImage");
});
});
test("Should provide `canonical` prop to NextSeo from canonical prop if provided", async () => {
const props = {
title: "Test Title",
description: "Test Description",
siteName: "Cal.com",
origin: "http://acme.cal.local",
canonical: "http://acme.cal.local/some-path",
};
vi.mocked(usePathname).mockReturnValue("/mocked-path");
const { container } = render(<HeadSeo {...props} />);
await waitFor(async () => {
const mockedNextSeoEl = container.querySelector("#mocked-next-seo");
expect(mockedNextSeoEl?.getAttribute("canonical")).toEqual(props.canonical);
expect(mockedNextSeoEl?.getAttribute("description")).toEqual(props.description);
expect(mockedNextSeoEl?.getAttribute("site_name")).toEqual(props.siteName);
expect(mockedNextSeoEl?.getAttribute("image")).toContain("constructGenericImage");
});
});
});
test("Should render title with brand", async () => {
const { container } = render(<HeadSeo {...basicProps} />);
await waitFor(async () => {
const titleEl = container.querySelector("title");
expect(titleEl?.getAttribute("title")).toEqual(`${basicProps.title} | Cal.com`);
const mockedNextSeoEl = container.querySelector("#mocked-next-seo");
expect(mockedNextSeoEl?.getAttribute("title")).toEqual(`${basicProps.title} | Cal.com`);
});
});
test("Should render title without brand", async () => {
const { container } = render(<HeadSeo {...basicProps} isBrandingHidden />);
await waitFor(async () => {
const titleEl = container.querySelector("title");
expect(titleEl?.getAttribute("title")).toEqual(`${basicProps.title}`);
const mockedNextSeoEl = container.querySelector("#mocked-next-seo");
expect(mockedNextSeoEl?.getAttribute("title")).toEqual(`${basicProps.title}`);
});
});
test("Should render with app props", async () => {
const { container } = render(<HeadSeo {...basicProps} app={{} as HeadSeoProps["app"]} />);
await waitFor(async () => {
const titleEl = container.querySelector("title");
expect(titleEl?.getAttribute("image")).toContain("constructAppImage");
const mockedNextSeoEl = container.querySelector("#mocked-next-seo");
expect(mockedNextSeoEl?.getAttribute("image")).toContain("constructAppImage");
});
});
test("Should render with meeting props", async () => {
const { container } = render(<HeadSeo {...basicProps} meeting={{} as HeadSeoProps["meeting"]} />);
await waitFor(async () => {
const titleEl = container.querySelector("title");
expect(titleEl?.getAttribute("image")).toContain("constructMeetingImage");
const mockedNextSeoEl = container.querySelector("#mocked-next-seo");
expect(mockedNextSeoEl?.getAttribute("image")).toContain("constructMeetingImage");
});
});
});