Created
June 10, 2022 16:47
Unit tests for the home page.
This file contains 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
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