Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Commit

Permalink
Add unit and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown committed Jul 2, 2019
1 parent de4570f commit 1c42bc4
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 92 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug CRA Tests",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
"args": [
"test",
"--runInBand",
"--no-cache",
"--coverage",
"--watchAll=false"
],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
331 changes: 240 additions & 91 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
},
"devDependencies": {
"@testing-library/react": "^8.0.4",
"jest-dom": "^3.5.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test": "react-scripts test --coverage --watchAll=false",
"eject": "react-scripts eject"
},
"eslintConfig": {
Expand All @@ -28,5 +32,13 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"jest": {
"collectCoverageFrom" : [
"<rootDir>/src/**/*.js",
"!<rootDir>/src/index.js" ,
"!<rootDir>/src/serviceWorker.js",
"!<rootDir>/src/test-utils/**/*.js"
]
}
}
55 changes: 55 additions & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from "react";
import { cleanup } from "@testing-library/react";
import "jest-dom/extend-expect";
import renderWithRoute from "./test-utils/renderWithRoute";
import App from "./App";

afterEach(cleanup);

test("should render home page", async () => {
const { queryByTestId } = renderWithRoute(<App />, "/");

const navigationNode = queryByTestId("navigation");
expect(navigationNode).toBeInTheDocument();

const homeNode = queryByTestId("home");
expect(homeNode).toBeInTheDocument();

const todosNode = queryByTestId("todos");
expect(todosNode).not.toBeInTheDocument();

const notFoundNode = queryByTestId("not-found");
expect(notFoundNode).not.toBeInTheDocument();
});

test("should render todos page", () => {
const { queryByTestId } = renderWithRoute(<App />, "/todos");

const navigationNode = queryByTestId("navigation");
expect(navigationNode).toBeInTheDocument();

const homeNode = queryByTestId("home");
expect(homeNode).not.toBeInTheDocument();

const todosNode = queryByTestId("todos");
expect(todosNode).toBeInTheDocument();

const notFoundNode = queryByTestId("not-found");
expect(notFoundNode).not.toBeInTheDocument();
});

test("should render not found page", () => {
const { queryByTestId } = renderWithRoute(<App />, "/bad-route");

const navigationNode = queryByTestId("navigation");
expect(navigationNode).toBeInTheDocument();

const homeNode = queryByTestId("home");
expect(homeNode).not.toBeInTheDocument();

const todosNode = queryByTestId("todos");
expect(todosNode).not.toBeInTheDocument();

const notFoundNode = queryByTestId("not-found");
expect(notFoundNode).toBeInTheDocument();
});
10 changes: 10 additions & 0 deletions src/Home.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import { render, cleanup } from "@testing-library/react";
import Home from "./Home";

afterEach(cleanup);

test("should render home component", () => {
const { container } = render(<Home />);
expect(container).toMatchSnapshot();
});
51 changes: 51 additions & 0 deletions src/Navigation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from "react";
import { cleanup, fireEvent } from "@testing-library/react";
import "jest-dom/extend-expect";
import renderWithRoute from "./test-utils/renderWithRoute";
import Navigation from "./Navigation";

afterEach(cleanup);

test("should have home not as link when on home route", async () => {
const { getByText } = renderWithRoute(<Navigation />, "/");

const navigationNode = getByText("Home");
expect(navigationNode).toMatchSnapshot();
});

test("should have home as link when not on home route", async () => {
const { getByText } = renderWithRoute(<Navigation />, "/not-home");

const navigationNode = getByText("Home");
expect(navigationNode).toMatchSnapshot();
});

test("should change route to home route when home link clicked", async () => {
const { getByText, history } = renderWithRoute(<Navigation />, "/not-home");

const navigationNode = getByText("Home");
fireEvent.click(navigationNode);
expect(history.location.pathname).toBe("/");
});

test("should have todos example not as link when on todos route", async () => {
const { getByText } = renderWithRoute(<Navigation />, "/todos");

const navigationNode = getByText("Todos Example");
expect(navigationNode).toMatchSnapshot();
});

test("should have todos example as link when not on todos route", async () => {
const { getByText } = renderWithRoute(<Navigation />, "/not-todos");

const navigationNode = getByText("Todos Example");
expect(navigationNode).toMatchSnapshot();
});

test("should change route to todos route when todos link clicked", async () => {
const { getByText, history } = renderWithRoute(<Navigation />, "/not-todos");

const navigationNode = getByText("Todos Example");
fireEvent.click(navigationNode);
expect(history.location.pathname).toBe("/todos");
});
10 changes: 10 additions & 0 deletions src/NotFound.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import { render, cleanup } from "@testing-library/react";
import NotFound from "./NotFound";

afterEach(cleanup);

test("should render not found component", () => {
const { container } = render(<NotFound />);
expect(container).toMatchSnapshot();
});
82 changes: 82 additions & 0 deletions src/Todos.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from "react";
import { render, cleanup, fireEvent } from "@testing-library/react";
import "jest-dom/extend-expect";
import Todos from "./Todos";

afterEach(cleanup);

test("should start with empty list", () => {
const { getByTestId } = render(<Todos />);

const todosListNode = getByTestId("todos-list")
expect(todosListNode.hasChildNodes()).toBe(false)
});

test("should not add blank item to list", () => {
const { getByPlaceholderText, getByTestId } = render(<Todos />);

const todosEntryNode = getByPlaceholderText("Add todo...")

enterValueAndPressEnter(todosEntryNode, "")

const todosListNode = getByTestId("todos-list")
expect(todosListNode.hasChildNodes()).toBe(false)
});

test("should add items to list", () => {
const { getByPlaceholderText, getByLabelText } = render(<Todos />);

const todosEntryNode = getByPlaceholderText("Add todo...")

enterValueAndPressEnter(todosEntryNode, "Learn to test")
enterValueAndPressEnter(todosEntryNode, "Practice")
enterValueAndPressEnter(todosEntryNode, "Keep trying")

expect(getByLabelText("Learn to test").checked).toBe(false)
expect(getByLabelText("Practice").checked).toBe(false)
expect(getByLabelText("Keep trying").checked).toBe(false)
});

test("should check item to list", () => {
const { getByPlaceholderText, getByLabelText } = render(<Todos />);

const todosEntryNode = getByPlaceholderText("Add todo...")

enterValueAndPressEnter(todosEntryNode, "Learn to test")
enterValueAndPressEnter(todosEntryNode, "Practice")
enterValueAndPressEnter(todosEntryNode, "Keep trying")

const practiceNode = getByLabelText("Practice")

fireEvent.click(practiceNode)

expect(getByLabelText("Learn to test").checked).toBe(false)
expect(practiceNode.checked).toBe(true)
expect(getByLabelText("Keep trying").checked).toBe(false)
});

test("should delete item from list", () => {
const { getByPlaceholderText, getAllByText, queryByLabelText } = render(<Todos />);

const todosEntryNode = getByPlaceholderText("Add todo...")

enterValueAndPressEnter(todosEntryNode, "Learn to test")
enterValueAndPressEnter(todosEntryNode, "Practice")
enterValueAndPressEnter(todosEntryNode, "Keep trying")

const removeButtons = getAllByText("(remove)")

fireEvent.click(removeButtons[1])

expect(queryByLabelText("Learn to test")).toBeInTheDocument()
expect(queryByLabelText("Practice")).not.toBeInTheDocument()
expect(queryByLabelText("Keep trying")).toBeInTheDocument()
});

const enterValueAndPressEnter = (node, value) =>{
fireEvent.change(node, { target: { value } })

fireEvent.keyPress(node, {
charCode: 13
})
}
35 changes: 35 additions & 0 deletions src/__snapshots__/Home.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should render home component 1`] = `
<div>
<div
class="Home"
data-testid="home"
>
<header
class="Home-header"
>
<img
alt="logo"
class="Home-logo"
src="logo.svg"
/>
<p>
Edit
<code>
src/App.js
</code>
and save to reload.
</p>
<a
class="Home-link"
href="https://reactjs.org"
rel="noopener noreferrer"
target="_blank"
>
Learn React
</a>
</header>
</div>
</div>
`;
29 changes: 29 additions & 0 deletions src/__snapshots__/Navigation.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should have home as link when not on home route 1`] = `
<a
href="/"
>
Home
</a>
`;

exports[`should have home not as link when on home route 1`] = `
<span>
Home
</span>
`;

exports[`should have todos example as link when not on todos route 1`] = `
<a
href="/todos"
>
Todos Example
</a>
`;

exports[`should have todos example not as link when on todos route 1`] = `
<span>
Todos Example
</span>
`;
14 changes: 14 additions & 0 deletions src/__snapshots__/NotFound.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should render not found component 1`] = `
<div>
<div
class="NotFound"
data-testid="not-found"
>
<h1>
Whoops!
</h1>
</div>
</div>
`;
22 changes: 22 additions & 0 deletions src/test-utils/renderWithRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import { render } from "@testing-library/react";
import {
createHistory,
createMemorySource,
LocationProvider
} from "@reach/router";

// this is a handy function that I would utilize for any component
// that relies on the router being in context
const renderWithRoute = (ui, route) => {
const history = createHistory(createMemorySource(route))
return {
...render(<LocationProvider history={history}>{ui}</LocationProvider>),
// adding `history` to the returned utilities to allow us
// to reference it in our tests (just try to avoid using
// this to test implementation details).
history
}
};

export default renderWithRoute

0 comments on commit 1c42bc4

Please sign in to comment.