-
Notifications
You must be signed in to change notification settings - Fork 4
/
Classifications-test.tsx
135 lines (121 loc) · 4.3 KB
/
Classifications-test.tsx
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
import { expect } from "chai";
import { stub } from "sinon";
import * as React from "react";
import { shallow } from "enzyme";
import { Classifications } from "../Classifications";
import ErrorMessage from "../ErrorMessage";
import ClassificationsForm from "../ClassificationsForm";
import ClassificationsTable from "../ClassificationsTable";
import buildStore from "../../store";
import genreData from "./genreData";
import classificationsData from "./classificationsData";
import UpdatingLoader from "../UpdatingLoader";
describe("Classifications", () => {
let wrapper;
let instance;
let bookData;
let store;
let refreshCatalog,
fetchGenreTree,
fetchClassifications,
fetchBook,
editClassifications;
beforeEach(() => {
bookData = {
title: "title",
fiction: true,
categories: ["Space Opera"],
};
store = buildStore();
refreshCatalog = stub();
fetchGenreTree = stub();
fetchClassifications = stub();
fetchBook = stub();
editClassifications = stub();
wrapper = shallow(
<Classifications
store={store}
csrfToken="token"
bookUrl="book url"
book={bookData}
bookAdminUrl="book admin url"
genreTree={genreData}
classifications={classificationsData}
isFetching={false}
fetchError={null}
refreshCatalog={refreshCatalog}
fetchGenreTree={fetchGenreTree}
fetchClassifications={fetchClassifications}
fetchBook={fetchBook}
editClassifications={editClassifications}
/>
);
instance = wrapper.instance() as any;
});
describe("rendering", () => {
it("shows book title", () => {
const title = wrapper.find("h2");
expect(title.text()).to.equal(bookData.title);
});
it("hides/shows updating indicator", () => {
wrapper.setProps({ isFetching: false });
let updating = wrapper.find(UpdatingLoader);
expect(updating.length).to.equal(1);
expect(updating.prop("show")).to.equal(false);
wrapper.setProps({ isFetching: true });
updating = wrapper.find(UpdatingLoader);
expect(updating.prop("show")).to.equal(true);
});
it("shows fetchError", () => {
let error = wrapper.find(ErrorMessage);
expect(error.length).to.equal(0);
const errorData = { status: 500, url: "url", response: "error" };
wrapper.setProps({ fetchError: errorData });
error = wrapper.find(ErrorMessage);
expect(error.props().error).to.equal(errorData);
});
it("shows classifications form", () => {
const form = wrapper.find(ClassificationsForm);
expect(form.props().book).to.equal(bookData);
expect(form.props().genreTree).to.equal(genreData);
expect(form.props().editClassifications).to.equal(
instance.editClassifications
);
});
it("shows classifications table", () => {
const table = wrapper.find(ClassificationsTable);
expect(table.props().classifications).to.equal(classificationsData);
});
});
describe("behavior", () => {
it("fetches genre tree and classifications on mount", () => {
expect(fetchGenreTree.callCount).to.equal(1);
expect(fetchGenreTree.args[0][0]).to.equal("/admin/genres");
expect(fetchClassifications.callCount).to.equal(1);
expect(fetchClassifications.args[0][0]).to.equal(
instance.classificationsUrl()
);
});
it("refreshes book, classifications, and Catalog after editing classifications", (done) => {
const formData = new (window as any).FormData();
editClassifications.returns(
new Promise<void>((resolve, reject) => resolve())
);
instance.editClassifications(formData).then((response) => {
expect(editClassifications.callCount).to.equal(1);
expect(editClassifications.args[0][0]).to.equal(
instance.editClassificationsUrl()
);
expect(editClassifications.args[0][1]).to.equal(formData);
expect(fetchBook.callCount).to.equal(1);
expect(fetchBook.args[0][0]).to.equal("book admin url");
expect(fetchClassifications.callCount).to.equal(2); // already called on mount
expect(fetchClassifications.args[1][0]).to.equal(
instance.classificationsUrl()
);
expect(refreshCatalog.callCount).to.equal(1);
done();
});
});
});
});