Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chernylu committed Oct 14, 2024
1 parent 88d7dea commit 6405046
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 20 deletions.
2 changes: 1 addition & 1 deletion site/src/common/blocks/RichTextBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import { hasRichTextBlockContent, PreviewSkeleton, PropsWithData, withPreview } from "@comet/cms-site";
import { LinkBlockData, RichTextBlockData } from "@src/blocks.generated";
import { Typography, TypographyProps } from "@src/common/components/Typography";
import { isValidLink } from "@src/common/helpers/HiddenIfInvalidLink";
import { PageLayout } from "@src/layout/PageLayout";
import redraft, { Renderers, TextBlockRenderFn } from "redraft";
import styled, { css } from "styled-components";

import { isValidLink } from "../helpers/isValidLink";
import { LinkBlock } from "./LinkBlock";

export const createTextBlockRenderFn =
Expand Down
22 changes: 3 additions & 19 deletions site/src/common/helpers/HiddenIfInvalidLink.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { usePreview } from "@comet/cms-site";
import {
DamFileDownloadLinkBlockData,
EmailLinkBlockData,
ExternalLinkBlockData,
InternalLinkBlockData,
LinkBlockData,
PhoneLinkBlockData,
} from "@src/blocks.generated";
import { LinkBlockData } from "@src/blocks.generated";
import { PropsWithChildren } from "react";

import { isValidLink } from "./isValidLink";

export function HiddenIfInvalidLink({ link, children }: PropsWithChildren<{ link: LinkBlockData }>) {
const { previewType } = usePreview();

Expand All @@ -22,14 +17,3 @@ export function HiddenIfInvalidLink({ link, children }: PropsWithChildren<{ link

return children;
}

export const isValidLink = (link: LinkBlockData) => {
return Boolean(
link.block &&
((link.block.type === "internal" && (link.block.props as InternalLinkBlockData).targetPage) ||
(link.block.type === "external" && (link.block.props as ExternalLinkBlockData).targetUrl) ||
(link.block.type === "damFileDownload" && (link.block.props as DamFileDownloadLinkBlockData).file) ||
(link.block.type === "email" && (link.block.props as EmailLinkBlockData).email) ||
(link.block.type === "phone" && (link.block.props as PhoneLinkBlockData).phone)),
);
};
79 changes: 79 additions & 0 deletions site/src/common/helpers/__tests__/isValidLink.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
DamFileDownloadLinkBlockData,
EmailLinkBlockData,
ExternalLinkBlockData,
InternalLinkBlockData,
PhoneLinkBlockData,
} from "@src/blocks.generated";

import { isValidLink } from "../isValidLink";

describe("isValidLink", () => {
it("should return true for valid internal link", () => {
const blockProps: InternalLinkBlockData = { targetPage: { id: "", name: "", path: "", documentType: "" } };
const link = { block: { type: "internal", props: blockProps }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(true);
});

it("should return false for invalid internal link", () => {
const link = { block: { type: "internal", props: {} }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(false);
});

it("should return true for valid external link", () => {
const blockProps: ExternalLinkBlockData = { targetUrl: "http://example.com", openInNewWindow: false };
const link = {
block: { type: "external", props: blockProps },
attachedBlocks: [],
};

expect(isValidLink(link)).toBe(true);
});

it("should return false for invalid external link", () => {
const link = { block: { type: "external", props: {} }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(false);
});

it("should return true for valid damFileDownload link", () => {
const blockProps: DamFileDownloadLinkBlockData = { openFileType: "Download", file: { id: "", name: "", fileUrl: "", size: 0 } };
const link = { block: { type: "damFileDownload", props: blockProps }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(true);
});

it("should return false for invalid damFileDownload link", () => {
const link = { block: { type: "damFileDownload", props: {} }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(false);
});

it("should return true for valid email link", () => {
const blockProps: EmailLinkBlockData = { email: "[email protected]" };
const link = { block: { type: "email", props: blockProps }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(true);
});

it("should return false for invalid email link", () => {
const link = { block: { type: "email", props: {} }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(false);
});

it("should return true for valid phone link", () => {
const blockProps: PhoneLinkBlockData = { phone: "1234567890" };
const link = { block: { type: "phone", props: blockProps }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(true);
});

it("should return false for invalid phone link", () => {
const link = { block: { type: "phone", props: {} }, attachedBlocks: [] };

expect(isValidLink(link)).toBe(false);
});
});
19 changes: 19 additions & 0 deletions site/src/common/helpers/isValidLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
DamFileDownloadLinkBlockData,
EmailLinkBlockData,
ExternalLinkBlockData,
InternalLinkBlockData,
LinkBlockData,
PhoneLinkBlockData,
} from "@src/blocks.generated";

export const isValidLink = (link: LinkBlockData) => {
return Boolean(
link.block &&
((link.block.type === "internal" && (link.block.props as InternalLinkBlockData).targetPage) ||
(link.block.type === "external" && (link.block.props as ExternalLinkBlockData).targetUrl) ||
(link.block.type === "damFileDownload" && (link.block.props as DamFileDownloadLinkBlockData).file) ||
(link.block.type === "email" && (link.block.props as EmailLinkBlockData).email) ||
(link.block.type === "phone" && (link.block.props as PhoneLinkBlockData).phone)),
);
};

0 comments on commit 6405046

Please sign in to comment.