-
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
Changes from 2 commits
d942b77
78b1d4b
5c5ab5a
4a76c3e
ae2b484
6f05a3d
711b998
199878e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| # testing | ||
| /coverage | ||
| junit.xml | ||
|
|
||
| # next.js | ||
| /.next/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { Config } from "jest"; | ||
| import nextJest from "next/jest.js"; | ||
|
|
||
| const createJestConfig = nextJest({ | ||
| // Provide the path to your Next.js app to load next.config.js and .env files in your test environment | ||
| dir: "./", | ||
| }); | ||
|
|
||
| // Add any custom config to be passed to Jest | ||
| const config: Config = { | ||
| coverageProvider: "v8", | ||
| testEnvironment: "jsdom", | ||
| setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"], | ||
| // Add more setup options before each test is run | ||
| // setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'], | ||
| }; | ||
|
|
||
| // createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
| export default createJestConfig(config); | ||
| 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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,10 @@ | |
| "generate-block-types": "comet generate-block-types", | ||
| "generate-block-types:watch": "chokidar -s \"**/block-meta.json\" -c \"npm run generate-block-types\"", | ||
| "intl:extract": "formatjs extract \"src/**/*.ts*\" --ignore **/*.d.ts --out-file lang-extracted/en.json --format simple --throws", | ||
| "intl:compile": "formatjs compile-folder --format simple --ast lang/starter-lang/site lang-compiled/" | ||
| "intl:compile": "formatjs compile-folder --format simple --ast lang/starter-lang/site lang-compiled/", | ||
| "test": "jest --passWithNoTests", | ||
| "test:watch": "npm run test -- --watch", | ||
| "test:ci": "npm run test -- --ci --reporters=default --reporters=jest-junit" | ||
| }, | ||
| "dependencies": { | ||
| "@comet/cms-site": "^7.5.0", | ||
|
|
@@ -47,12 +50,18 @@ | |
| "@graphql-codegen/near-operation-file-preset": "^2.5.0", | ||
| "@graphql-codegen/typescript": "^2.0.0", | ||
| "@graphql-codegen/typescript-operations": "^2.0.0", | ||
| "@testing-library/jest-dom": "^6.5.0", | ||
| "@testing-library/react": "^16.0.1", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add an example usage for testing library as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Added tests for HiddenIfInvalidLink.tsx. ae2b484 |
||
| "@types/jest": "^29.5.13", | ||
| "@types/node": "^20.0.0", | ||
| "@types/react": "^18.2.0", | ||
| "@types/react-dom": "^18.2.0", | ||
| "chokidar-cli": "^3.0.0", | ||
| "dotenv-cli": "^7.0.0", | ||
| "eslint": "^8.0.0", | ||
| "jest": "^29.7.0", | ||
| "jest-environment-jsdom": "^29.7.0", | ||
| "jest-junit": "^16.0.0", | ||
| "npm-run-all": "^4.1.5", | ||
| "postcss-styled-syntax": "^0.6.4", | ||
| "prettier": "^2.0.0", | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we should add a tests folder for this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer splitting tests and application code, but I can remove the |
| 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); | ||
| }); | ||
| }); |
| 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)), | ||
| ); | ||
| }; |
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.
This option is defined two lines above 😁 IMO we can remove all comments here
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.
done