Skip to content

Commit

Permalink
Wire up Jest tests with an example pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjarling committed Nov 21, 2023
1 parent 63f80b1 commit 6aa8b0d
Show file tree
Hide file tree
Showing 8 changed files with 2,768 additions and 1,055 deletions.
1 change: 0 additions & 1 deletion components/home/ApplicationTypes.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AtIcon } from "pages/types-of-applications";
import { CheckCircleIcon } from "@heroicons/react/20/solid";
import Link from "next/link";
import RichTextContent from "components/RichTextContent";

Expand Down
64 changes: 64 additions & 0 deletions components/news/BlogPostPreview.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import BlogPostPreview from "./BlogPostPreview";
import { mockBlogPostPreview } from "fixtures/cms-blog-post-preview";
import { render } from "@testing-library/react";

// Mock MarkdownContent component
jest.mock("components/MarkdownContent", () => {
return function MockMarkdownContent({ content }) {
return <div data-testid="mock-markdown-content">{content}</div>;
};
});

describe("BlogPostPreview", () => {
it("renders preview textual data", () => {
const { debug, getByText, getByAltText } = render(
<BlogPostPreview {...mockBlogPostPreview} />
);

// Test title appears
expect(
getByText(
"Samvera Connect 2023 Conference Celebrates In-Person Gathering of Global Community"
)
).toBeInTheDocument();

// Publish Date
expect(getByText("November 7, 2023")).toBeInTheDocument();

// Blog post content
expect(
getByText(
"Philadelphia, PA - Samvera Connect 2023, the much-anticipated conference of the Samvera Community, concluded on October 26th at the Philadelphia Marriott Old City, marking a triumphant return to in-person events after four years of virtual conferences. The conference brought together 78 attendees from five countries, fostering a sense of community solidarity and advancing the technical development and collaboration efforts of the Samvera community."
)
).toBeInTheDocument();

// Tag Cloud
expect(getByText(/news/i)).toBeInTheDocument();
});

it("renders Blog Post Preview image", () => {
const { debug, getByAltText } = render(
<BlogPostPreview {...mockBlogPostPreview} />
);
debug();

// Image renders
expect(
getByAltText("Philadelphia downtown - Photo by Micah Giszack on Unsplash")
).toBeInTheDocument();

// get an element by tagName "img"
const img = document.querySelector("img");

// check it's alt attribute
expect(img.alt).toBe(
"Philadelphia downtown - Photo by Micah Giszack on Unsplash"
);

// check it's src attribute
expect(img.src).toBe(
"http://images.ctfassets.net/gmxjheo1ix1o/16iCFPpKQ2PraT2Sjx344E/95a32867bc401ab67098af8a5240256c/micah-giszack-1AH0PfLQfDA-unsplash.jpg"
);
});
});
//
23 changes: 0 additions & 23 deletions components/news/BlogPosts.jsx

This file was deleted.

69 changes: 69 additions & 0 deletions fixtures/cms-blog-post-preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export const mockBlogPostPreview = {
cmsContent: {
data: {},
content: [
{
data: {},
content: [
{
data: {},
marks: [],
value:
"Philadelphia, PA - Samvera Connect 2023, the much-anticipated conference of the Samvera Community, concluded on October 26th at the Philadelphia Marriott Old City, marking a triumphant return to in-person events after four years of virtual conferences. The conference brought together 78 attendees from five countries, fostering a sense of community solidarity and advancing the technical development and collaboration efforts of the Samvera community.",
nodeType: "text",
},
],
nodeType: "paragraph",
},
],
nodeType: "document",
},
image: {
metadata: {
tags: [],
},
sys: {
space: {
sys: {
type: "Link",
linkType: "Space",
id: "gmxjheo1ix1o",
},
},
id: "16iCFPpKQ2PraT2Sjx344E",
type: "Asset",
createdAt: "2023-11-15T10:09:34.617Z",
updatedAt: "2023-11-15T10:09:34.617Z",
environment: {
sys: {
id: "master",
type: "Link",
linkType: "Environment",
},
},
revision: 1,
locale: "en-US",
},
fields: {
title: "Philadelphia downtown - Photo by Micah Giszack on Unsplash",
description: "Philadelphia downtown",
file: {
url: "//images.ctfassets.net/gmxjheo1ix1o/16iCFPpKQ2PraT2Sjx344E/95a32867bc401ab67098af8a5240256c/micah-giszack-1AH0PfLQfDA-unsplash.jpg",
details: {
size: 792856,
image: {
width: 1920,
height: 1440,
},
},
fileName: "micah-giszack-1AH0PfLQfDA-unsplash.jpg",
contentType: "image/jpeg",
},
},
},
publishDate: "November 7, 2023",
slug: "samvera-connect-2023-success",
tag: ["news"],
title:
"Samvera Connect 2023 Conference Celebrates In-Person Gathering of Global Community",
};
21 changes: 21 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
/** @type {import('jest').Config} */
const config = {
// Add more setup options before each test is run
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
moduleNameMapper: {
"^components/(.*)$": "<rootDir>/components/$1",
"^fixtures/(.*)$": "<rootDir>/fixtures/$1",
},
testEnvironment: "jest-environment-jsdom",
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config);
1 change: 1 addition & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom";
35 changes: 20 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,49 @@
"build": "next build && next export",
"build-site-nav": "node lib/build-nav.js",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"test": "jest --watch"
},
"dependencies": {
"-": "^0.0.1",
"@contentful/rich-text-from-markdown": "^15.16.8",
"@contentful/rich-text-react-renderer": "^15.17.1",
"@contentful/rich-text-react-renderer": "^15.19.0",
"@contentful/rich-text-types": "^16.3.0",
"@headlessui/react": "^1.7.16",
"@headlessui/react": "^1.7.17",
"@headlessui/tailwindcss": "^0.2.0",
"@heroicons/react": "^2.0.18",
"@tanstack/react-query": "^5.8.4",
"@testing-library/react": "^14.1.2",
"autoprefixer": "^10.4.14",
"contentful": "^10.4.2",
"autoprefixer": "^10.4.16",
"contentful": "^10.6.11",
"date-fns": "^2.30.0",
"deepmerge": "^4.3.1",
"gray-matter": "^4.0.3",
"markdown-it": "^13.0.1",
"next": "^13.4.12",
"jest": "^29.7.0",
"markdown-it": "^13.0.2",
"next": "^13.5.6",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rehype-raw": "^6.1.1",
"rehype-sanitize": "^5.0.1",
"rehype-stringify": "^9.0.3",
"rehype-stringify": "^9.0.4",
"remark": "^14.0.3",
"remark-html": "^15.0.2",
"remark-rehype": "^10.1.0",
"sass": "^1.64.1",
"swiper": "^10.0.4",
"sass": "^1.69.5",
"swiper": "^10.3.1",
"unified": "^10.1.2"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.1.4",
"@testing-library/react": "^14.1.2",
"depcheck": "^1.4.7",
"eslint": "^8.45.0",
"eslint-config-next": "^13.4.12",
"postcss": "^8.4.27",
"eslint": "^8.54.0",
"eslint-config-next": "^13.5.6",
"eslint-plugin-jest-dom": "^5.1.0",
"jest-environment-jsdom": "^29.7.0",
"postcss": "^8.4.31",
"remark-parse": "^10.0.2",
"tailwindcss": "^3.3.3"
"tailwindcss": "^3.3.5"
}
}
Loading

0 comments on commit 6aa8b0d

Please sign in to comment.