-
Notifications
You must be signed in to change notification settings - Fork 0
Add jest setup for site #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d942b77
Add jest setup
vps-chernylu 78b1d4b
Add tests
vps-chernylu 5c5ab5a
Remove comments
vps-chernylu 4a76c3e
Install @testing-library/jest-dom and include jest.setup.ts
vps-chernylu ae2b484
Add tests
vps-chernylu 6f05a3d
Add test:ci:site script to run site tests in CI workflow
vps-chernylu 711b998
Merge remote-tracking branch 'origin/main' into add-site-jest-setup
vps-chernylu 199878e
Run install
vps-chernylu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| # testing | ||
| /coverage | ||
| junit.xml | ||
|
|
||
| # next.js | ||
| /.next/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { Config } from "jest"; | ||
| import nextJest from "next/jest.js"; | ||
|
|
||
| const createJestConfig = nextJest({ | ||
| dir: "./", | ||
| }); | ||
|
|
||
| const config: Config = { | ||
| coverageProvider: "v8", | ||
| testEnvironment: "jsdom", | ||
| setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"], | ||
| }; | ||
|
|
||
| export default createJestConfig(config); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import "@testing-library/jest-dom"; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
site/src/common/helpers/__tests__/HiddenIfInvalidLink.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { render } from "@testing-library/react"; | ||
|
|
||
| const usePreview = jest.fn(); | ||
|
|
||
| jest.mock("@comet/cms-site", () => { | ||
| return { | ||
| usePreview: () => usePreview(), | ||
| }; | ||
| }); | ||
|
|
||
| import { HiddenIfInvalidLink } from "../HiddenIfInvalidLink"; | ||
|
|
||
| describe("HiddenIfInvalidLink", () => { | ||
| it("should render children if previewType is BlockPreview", () => { | ||
| usePreview.mockReturnValue({ previewType: "BlockPreview" }); | ||
|
|
||
| const { getByText } = render( | ||
| <HiddenIfInvalidLink link={{ block: { type: "internal", props: {} }, attachedBlocks: [] }}> | ||
| <div>Valid Link</div> | ||
| </HiddenIfInvalidLink>, | ||
| ); | ||
|
|
||
| expect(getByText("Valid Link")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("should render children if link is valid", () => { | ||
| usePreview.mockReturnValue({ previewType: "OtherPreview" }); | ||
|
|
||
| const validLink = { | ||
| block: { type: "internal", props: { targetPage: { id: "", name: "", path: "", documentType: "" } } }, | ||
| attachedBlocks: [], | ||
| }; | ||
| const { getByText } = render( | ||
| <HiddenIfInvalidLink link={validLink}> | ||
| <div>Valid Link</div> | ||
| </HiddenIfInvalidLink>, | ||
| ); | ||
|
|
||
| expect(getByText("Valid Link")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("should not render children if link is invalid", () => { | ||
| usePreview.mockReturnValue({ previewType: "OtherPreview" }); | ||
| const invalidLink = { block: { type: "internal", props: {} }, attachedBlocks: [] }; | ||
| const { queryByText } = render( | ||
| <HiddenIfInvalidLink link={invalidLink}> | ||
| <div>Invalid Link</div> | ||
| </HiddenIfInvalidLink>, | ||
| ); | ||
|
|
||
| expect(queryByText("Invalid Link")).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: "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); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)), | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add an example usage for testing library as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Added tests for HiddenIfInvalidLink.tsx. ae2b484