Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SarahElson/18dca70acdff1c22a44bdb29662e6cfa to your computer and use it in GitHub Desktop.
Save SarahElson/18dca70acdff1c22a44bdb29662e6cfa to your computer and use it in GitHub Desktop.
Unit tests for the home page.
import React from "react"
import { render } from "@testing-library/react"
import { useStaticQuery } from "gatsby"
import BlogIndexDetails from "../index"
describe("BlogIndex component", () => {
beforeEach(() => {
useStaticQuery.mockReturnValue({
site: {
siteMetadata: {
title: `Gatsby Starter Blog for Freshers`,
description: A starter blog demonstrating what Gatsby can do and the features offered. `,
social: {
twitter: `Irshad`,
},
},
},
})
})
it("renders the tests correctly", async () => {
const mockData = {
site: {
siteMetadata: {
author: "xyz",
},
},
allMarkdownRemark: {
nodes: [
{
excerpt: "This is my first attempt at excerpt writing",
fields: {
slug: "first-slug",
},
frontmatter: {
date: "May 3, 2022",
title: "My first blog post for readers",
description: "My awesome blog description for readers",
},
},
{
excerpt: "This is my second attempt at excerpt writing",
fields: {
slug: "second-slug",
},
frontmatter: {
date: "Nov 12, 2022",
title: "My second blog post for reader friends",
description: "My awesome second blog description for reader friends",
},
},
],
},
}
const { getByTestId } = render(
<BlogIndex data={mockData} location={window.location} />
)
const { nodes } = mockData.allMarkdownRemark
const post1 = "first-slug-link"
const post2 = "second-slug-desc"
expect(getByTestId(post1)).toHaveTextContent(nodes[0].frontmatter.title)
expect(getByTestId(post2)).toHaveTextContent(
nodes[1].frontmatter.description
)
expect(nodes.length).toEqual(5)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment