-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAuthors.test.js
80 lines (62 loc) · 2.01 KB
/
Authors.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
import React from "react";
import { shallow } from "enzyme";
import Authors from "./Authors";
describe("UserProfile", () => {
let props;
let wrapper;
let useEffect;
let mockUseEffect = () => {
useEffect.mockImplementationOnce(f => f());
};
const alice = { id: 1, name: "alice" };
const bob = { id: 2, name: "bob" };
const authors = [alice, bob];
const posts = [{ id: 1, title: "a post", body: "the body" }];
beforeEach(() => {
useEffect = jest.spyOn(React, "useEffect");
// useEffect = jest.spyOn(React, "useEffect").mockImplementation(f => f());
props = {
fetchAuthors: jest.fn().mockResolvedValue(authors),
fetchPosts: jest.fn().mockResolvedValue(posts)
};
mockUseEffect();
mockUseEffect();
wrapper = shallow(<Authors {...props} />);
});
describe("on start", () => {
it("loads the authors", () => {
expect(props.fetchAuthors).toHaveBeenCalled();
});
it("does not load posts", () => {
expect(props.fetchPosts).not.toHaveBeenCalled();
});
it("renders the authors", () => {
expect(wrapper.find("Author")).toHaveLength(2);
const firstAuthor = wrapper.find("Author").first();
expect(firstAuthor.prop("author")).toEqual(alice);
expect(firstAuthor.prop("activeAuthor")).toEqual(null);
});
});
describe("given selected author", () => {
beforeEach(() => {
mockUseEffect();
mockUseEffect();
wrapper
.find("Author")
.first()
.simulate("select", alice);
});
it("sets the active author", () => {
expect(wrapper.find("Author")).toHaveLength(2);
const firstAuthor = wrapper.find("Author").first();
expect(firstAuthor.prop("author")).toEqual(alice);
expect(firstAuthor.prop("activeAuthor")).toEqual(alice);
});
it("loads the right posts", () => {
expect(props.fetchPosts).toHaveBeenCalledWith(alice.id);
});
it("renders the posts", () => {
expect(wrapper.find("Post").prop("post")).toEqual(posts[0]);
});
});
});