From 640504662220e8442e0eb24ed5a4ab992c01a87d Mon Sep 17 00:00:00 2001 From: Lukas Kern Date: Mon, 14 Oct 2024 09:32:55 +0200 Subject: [PATCH] Add tests --- site/src/common/blocks/RichTextBlock.tsx | 2 +- .../common/helpers/HiddenIfInvalidLink.tsx | 22 +----- .../helpers/__tests__/isValidLink.test.ts | 79 +++++++++++++++++++ site/src/common/helpers/isValidLink.ts | 19 +++++ 4 files changed, 102 insertions(+), 20 deletions(-) create mode 100644 site/src/common/helpers/__tests__/isValidLink.test.ts create mode 100644 site/src/common/helpers/isValidLink.ts diff --git a/site/src/common/blocks/RichTextBlock.tsx b/site/src/common/blocks/RichTextBlock.tsx index 6e6c526e9..707800fda 100644 --- a/site/src/common/blocks/RichTextBlock.tsx +++ b/site/src/common/blocks/RichTextBlock.tsx @@ -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 = diff --git a/site/src/common/helpers/HiddenIfInvalidLink.tsx b/site/src/common/helpers/HiddenIfInvalidLink.tsx index 3be2088c6..60dcae1d8 100644 --- a/site/src/common/helpers/HiddenIfInvalidLink.tsx +++ b/site/src/common/helpers/HiddenIfInvalidLink.tsx @@ -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(); @@ -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)), - ); -}; diff --git a/site/src/common/helpers/__tests__/isValidLink.test.ts b/site/src/common/helpers/__tests__/isValidLink.test.ts new file mode 100644 index 000000000..f730f2134 --- /dev/null +++ b/site/src/common/helpers/__tests__/isValidLink.test.ts @@ -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: "foo@mail.com" }; + 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); + }); +}); diff --git a/site/src/common/helpers/isValidLink.ts b/site/src/common/helpers/isValidLink.ts new file mode 100644 index 000000000..e53302baf --- /dev/null +++ b/site/src/common/helpers/isValidLink.ts @@ -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)), + ); +};