Skip to content

Commit

Permalink
Added post list tests for blog profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Aug 24, 2019
1 parent ff92b72 commit b514177
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
15 changes: 15 additions & 0 deletions __mocks__/modules/gatsby-plugin-google-analytics.js
@@ -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
}
});
2 changes: 1 addition & 1 deletion __mocks__/modules/index.js
@@ -1,4 +1,4 @@
import './gatsby';
import './gatsby-image';
import './disqus-react';

import './gatsby-plugin-google-analytics'
34 changes: 32 additions & 2 deletions src/templates/blog-profile.test.js
@@ -1,10 +1,12 @@
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 { MockPost } from "../../__mocks__/data/mock-post"
import { useStaticQuery } from "gatsby"
import { MockUnicorn } from "../../__mocks__/data/mock-unicorn"
import BlogProfile from "./blog-profile"
import {onLinkClick as onAnalyticsLinkClick} from 'gatsby-plugin-google-analytics';
import {onLinkClick as onGarsbyLinkClick} from 'gatsby';

beforeAll(() => {
useStaticQuery.mockImplementation(() => ({
Expand All @@ -19,7 +21,7 @@ afterAll(() => {
})

test("Blog profile page renders", async () => {
const { baseElement } = render(
const { baseElement, findByText, findByTestId } = render(
<BlogProfile
data={{
site: {
Expand All @@ -39,5 +41,33 @@ test("Blog profile page renders", async () => {
/>)

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);

})

0 comments on commit b514177

Please sign in to comment.