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:
1 parent
29ccc58806
commit
5f5cf92456
16 files changed
+730
-27
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import { render } from "@testing-library/react";
|
||||
import React from "react";
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
|
||||
import { getOrgFullOrigin } from "@calcom/ee/organizations/lib/orgDomains";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { HeadSeo } from "@calcom/ui";
|
||||
|
||||
import { BookerSeo } from "./BookerSeo";
|
||||
|
||||
// Mocking necessary modules and hooks
|
||||
vi.mock("@calcom/trpc/react", () => ({
|
||||
trpc: {
|
||||
viewer: {
|
||||
public: {
|
||||
event: {
|
||||
useQuery: vi.fn(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@calcom/lib/hooks/useLocale", () => ({
|
||||
useLocale: () => ({ t: (key: string) => key }),
|
||||
}));
|
||||
|
||||
vi.mock("@calcom/ee/organizations/lib/orgDomains", () => ({
|
||||
getOrgFullOrigin: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@calcom/ui", () => ({
|
||||
HeadSeo: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("BookerSeo Component", () => {
|
||||
it("renders HeadSeo with correct props", () => {
|
||||
const mockData = {
|
||||
event: {
|
||||
slug: "event-slug",
|
||||
profile: { name: "John Doe", image: "image-url", username: "john" },
|
||||
title: "30min",
|
||||
hidden: false,
|
||||
users: [{ name: "Jane Doe", username: "jane" }],
|
||||
},
|
||||
entity: { fromRedirectOfNonOrgLink: false, orgSlug: "org1" },
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
trpc.viewer.public.event.useQuery.mockReturnValueOnce({
|
||||
data: mockData.event,
|
||||
});
|
||||
|
||||
vi.mocked(getOrgFullOrigin).mockImplementation((text: string | null) => `${text}.cal.local`);
|
||||
|
||||
render(
|
||||
<BookerSeo
|
||||
username={mockData.event.profile.username}
|
||||
eventSlug={mockData.event.slug}
|
||||
rescheduleUid={undefined}
|
||||
entity={mockData.entity}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(HeadSeo).toHaveBeenCalledWith(
|
||||
{
|
||||
origin: `${mockData.entity.orgSlug}.cal.local`,
|
||||
isBrandingHidden: undefined,
|
||||
// Don't know why we are adding space in the beginning
|
||||
title: ` ${mockData.event.title} | ${mockData.event.profile.name}`,
|
||||
description: ` ${mockData.event.title}`,
|
||||
meeting: {
|
||||
profile: {
|
||||
name: mockData.event.profile.name,
|
||||
image: mockData.event.profile.image,
|
||||
},
|
||||
title: mockData.event.title,
|
||||
users: [
|
||||
{
|
||||
name: mockData.event.users[0].name,
|
||||
username: mockData.event.users[0].username,
|
||||
},
|
||||
],
|
||||
},
|
||||
nextSeoProps: {
|
||||
nofollow: true,
|
||||
noindex: true,
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getOrgFullOrigin } from "@calcom/ee/organizations/lib/orgDomains";
|
||||
import type { GetBookingType } from "@calcom/features/bookings/lib/get-booking";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
@@ -47,6 +48,7 @@ export const BookerSeo = (props: BookerSeoProps) => {
|
||||
const title = event?.title ?? "";
|
||||
return (
|
||||
<HeadSeo
|
||||
origin={getOrgFullOrigin(entity.orgSlug ?? null)}
|
||||
title={`${rescheduleUid && !!bookingData ? t("reschedule") : ""} ${title} | ${profileName}`}
|
||||
description={`${rescheduleUid ? t("reschedule") : ""} ${title}`}
|
||||
meeting={{
|
||||
|
||||
@@ -110,9 +110,10 @@ export function subdomainSuffix() {
|
||||
return urlSplit.length === 3 ? urlSplit.slice(1).join(".") : urlSplit.join(".");
|
||||
}
|
||||
|
||||
export function getOrgFullOrigin(slug: string, options: { protocol: boolean } = { protocol: true }) {
|
||||
export function getOrgFullOrigin(slug: string | null, options: { protocol: boolean } = { protocol: true }) {
|
||||
if (!slug)
|
||||
return options.protocol ? WEBSITE_URL : WEBSITE_URL.replace("https://", "").replace("http://", "");
|
||||
|
||||
const orgFullOrigin = `${
|
||||
options.protocol ? `${new URL(WEBSITE_URL).protocol}//` : ""
|
||||
}${slug}.${subdomainSuffix()}`;
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { getOrgSlug, getOrgDomainConfigFromHostname } from "@calcom/features/ee/organizations/lib/orgDomains";
|
||||
import {
|
||||
getOrgSlug,
|
||||
getOrgDomainConfigFromHostname,
|
||||
getOrgFullOrigin,
|
||||
} from "@calcom/features/ee/organizations/lib/orgDomains";
|
||||
import * as constants from "@calcom/lib/constants";
|
||||
|
||||
function setupEnvs({ WEBAPP_URL = "https://app.cal.com" } = {}) {
|
||||
function setupEnvs({ WEBAPP_URL = "https://app.cal.com", WEBSITE_URL } = {}) {
|
||||
Object.defineProperty(constants, "WEBAPP_URL", { value: WEBAPP_URL });
|
||||
Object.defineProperty(constants, "WEBSITE_URL", { value: WEBSITE_URL });
|
||||
Object.defineProperty(constants, "ALLOWED_HOSTNAMES", {
|
||||
value: ["cal.com", "cal.dev", "cal-staging.com", "cal.community", "cal.local:3000", "localhost:3000"],
|
||||
});
|
||||
@@ -82,4 +87,22 @@ describe("Org Domains Utils", () => {
|
||||
expect(getOrgSlug("acme.cal.com:3000")).toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getOrgFullOrigin", () => {
|
||||
it("should return the regular(non-org) origin if slug is null", () => {
|
||||
setupEnvs({
|
||||
WEBAPP_URL: "https://app.cal.com",
|
||||
WEBSITE_URL: "https://abc.com",
|
||||
});
|
||||
expect(getOrgFullOrigin(null)).toEqual("https://abc.com");
|
||||
});
|
||||
it("should return the org origin if slug is set", () => {
|
||||
setupEnvs({
|
||||
WEBAPP_URL: "https://app.cal-app.com",
|
||||
WEBSITE_URL: "https://cal.com",
|
||||
});
|
||||
// We are supposed to use WEBAPP_URL to derive the origin from and not WEBSITE_URL
|
||||
expect(getOrgFullOrigin("org")).toEqual("https://org.cal-app.com");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user