Added post list tests for blog profile page

This commit is contained in:
Corbin Crutchley
2019-08-23 20:54:01 -07:00
parent ff92b7272a
commit b5141772f6
3 changed files with 48 additions and 3 deletions

View File

@@ -0,0 +1,15 @@
import React from 'react'
import {onLinkClick} from 'gatsby-plugin-google-analytics';
afterEach(() => {
onLinkClick.mockReset();
})
jest.mock('gatsby-plugin-google-analytics', () => {
const onLinkClick = jest.fn();
return {
OutboundLink: (props) => <div onClick={onLinkClick}>{props.children}</div>,
onLinkClick
}
});

View File

@@ -1,4 +1,4 @@
import './gatsby'; import './gatsby';
import './gatsby-image'; import './gatsby-image';
import './disqus-react'; import './disqus-react';
import './gatsby-plugin-google-analytics'

View File

@@ -1,10 +1,12 @@
import React from "react" import React from "react"
import { render } from "@testing-library/react" import { fireEvent, render } from "@testing-library/react"
import { siteMetadata } from "../../__mocks__/data/mock-site-metadata" import { siteMetadata } from "../../__mocks__/data/mock-site-metadata"
import { MockPost } from "../../__mocks__/data/mock-post" import { MockPost } from "../../__mocks__/data/mock-post"
import { useStaticQuery } from "gatsby" import { useStaticQuery } from "gatsby"
import { MockUnicorn } from "../../__mocks__/data/mock-unicorn" import { MockUnicorn } from "../../__mocks__/data/mock-unicorn"
import BlogProfile from "./blog-profile" import BlogProfile from "./blog-profile"
import {onLinkClick as onAnalyticsLinkClick} from 'gatsby-plugin-google-analytics';
import {onLinkClick as onGarsbyLinkClick} from 'gatsby';
beforeAll(() => { beforeAll(() => {
useStaticQuery.mockImplementation(() => ({ useStaticQuery.mockImplementation(() => ({
@@ -19,7 +21,7 @@ afterAll(() => {
}) })
test("Blog profile page renders", async () => { test("Blog profile page renders", async () => {
const { baseElement } = render( const { baseElement, findByText, findByTestId } = render(
<BlogProfile <BlogProfile
data={{ data={{
site: { site: {
@@ -39,5 +41,33 @@ test("Blog profile page renders", async () => {
/>) />)
expect(baseElement).toBeInTheDocument(); expect(baseElement).toBeInTheDocument();
expect(await findByText('Joe')).toBeInTheDocument();
expect(await findByText('Exists')).toBeInTheDocument();
const TwitterEl = await findByText('Twitter')
expect(TwitterEl).toBeInTheDocument();
fireEvent.click(TwitterEl);
expect(onAnalyticsLinkClick).toHaveBeenCalledTimes(1)
const GitHubEl = await findByText('GitHub')
expect(GitHubEl).toBeInTheDocument();
fireEvent.click(GitHubEl);
expect(onAnalyticsLinkClick).toHaveBeenCalledTimes(2)
const WebsiteEl = await findByText('Website')
expect(WebsiteEl).toBeInTheDocument();
fireEvent.click(WebsiteEl);
expect(onAnalyticsLinkClick).toHaveBeenCalledTimes(3)
expect(await findByText('1 Articles')).toBeInTheDocument();
expect(await findByText('10000 Words')).toBeInTheDocument();
// Post cards
expect(await findByText("by Joe")).toBeInTheDocument();
expect(await findByText('10-10-2010')).toBeInTheDocument();
expect(await findByText('This is a short description dunno why this would be this short')).toBeInTheDocument();
fireEvent.click(await findByText("Post title"));
expect(onGarsbyLinkClick).toHaveBeenCalledTimes(2);
fireEvent.click(await findByTestId("authorPic"));
expect(onGarsbyLinkClick).toHaveBeenCalledTimes(4);
}) })