/
App.test.js
152 lines (143 loc) · 3.68 KB
/
App.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React from "react";
import { render, fireEvent, wait } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { MockedProvider } from "@apollo/react-testing";
import tk from "timekeeper";
import { GET_CONVERSATIONS } from "components/ChatBar";
import { GET_USER_INFO } from "components/Nav";
import { GET_POSTS } from "components/Posts";
import { LOGIN } from "pages/Login";
import App from "./App";
jest.mock("containers/Subscriber", () => ({ children }) => children);
describe("App", () => {
beforeEach(() => {
tk.freeze("2019-04-20");
});
afterEach(() => {
tk.reset();
});
it("successfully proceeds through user flow logging in and out", async () => {
const mocks = [
{
request: {
query: GET_POSTS,
},
result: {
data: {
posts: [
{
id: 1,
body: "Thoughts",
insertedAt: "2019-04-18T00:00:00",
user: {
id: 1,
name: "John Smith",
gravatarMd5: "abc",
},
},
],
},
},
},
{
request: {
query: LOGIN,
variables: { email: "john@lvh.me", password: "password" },
},
result: { data: { authenticate: { id: 123, token: "abc" } } },
},
{
request: {
query: GET_USER_INFO,
},
result: {
data: {
currentUser: {
name: "John Doe",
},
},
},
},
{
request: {
query: GET_POSTS,
},
result: {
data: {
posts: [
{
id: 1,
body: "Thoughts",
insertedAt: "2019-04-18T00:00:00",
user: {
id: 1,
name: "John Smith",
gravatarMd5: "abc",
},
},
],
},
},
},
{
request: {
query: GET_CONVERSATIONS,
},
result: {
data: {
conversations: [
{
id: 1,
title: "Jane Smith, John Doe",
updatedAt: "2019-04-18T00:00:00",
users: [
{
id: 1,
gravatarMd5: "abc",
},
{
id: 2,
gravatarMd5: "def",
},
],
},
],
},
},
},
];
const {
container,
getByLabelText,
getByTestId,
getAllByText,
getByText,
} = render(
<MemoryRouter>
<MockedProvider mocks={mocks} addTypename={false}>
<App />
</MockedProvider>
</MemoryRouter>,
);
// Log in the user.
fireEvent.click(getAllByText("Log in")[0]);
fireEvent.change(getByLabelText("Email address"), {
target: { value: "john@lvh.me" },
});
fireEvent.change(getByLabelText("Password"), {
target: { value: "password" },
});
fireEvent.submit(getByTestId("login-form"));
// Ensure all the queries have finished.
await wait(() => getByText("John Doe"));
await wait(() => getByText("Thoughts"));
// Check that the homepage renders correctly.
expect(container).toMatchSnapshot();
// Log back out.
fireEvent.click(getByText("John Doe"));
fireEvent.click(getByText("Log out"));
await wait(() => getAllByText("Log in")[0]);
// Check that the homepage renders correctly.
expect(container).toMatchSnapshot();
});
});