-
Notifications
You must be signed in to change notification settings - Fork 4
/
Contributors-test.tsx
166 lines (149 loc) · 6.32 KB
/
Contributors-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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { expect } from "chai";
import * as React from "react";
import { mount } from "enzyme";
import { spy } from "sinon";
import Contributors from "../Contributors";
import { RolesData, ContributorData } from "../../interfaces";
describe("Contributors", () => {
const roles: RolesData = {
aut: "Author",
ill: "Illustrator",
nrt: "Narrator",
trl: "Translator",
};
const contributors: ContributorData[] = [
{ name: "A Translator", role: "trl" },
{ name: "A Narrator", role: "nrt" },
];
let wrapper;
const hasSelect = (element, value: string) => {
const select = element.find("select");
expect(select.length).to.equal(1);
expect(select.prop("name")).to.equal("contributor-role");
expect(select.prop("value")).to.equal(value);
expect(select.find("option").map((o) => o.prop("value"))).to.eql(
Object.values(roles)
);
};
const hasInput = (element, value = "") => {
const input = element.find("input");
expect(element.length).to.equal(1);
expect(input.prop("name")).to.equal("contributor-name");
expect(input.prop("value")).to.equal(value);
};
beforeEach(() => {
wrapper = mount(
<Contributors
roles={roles}
contributors={contributors}
disabled={false}
/>
);
});
it("displays a label", () => {
const label = wrapper.find("label").at(0);
expect(label.text()).to.equal("Authors and Contributors");
});
it("displays a list of existing contributors", () => {
const removables = wrapper.find(".with-remove-button");
expect(removables.length).to.equal(2);
removables.forEach((el, idx) => {
expect(hasSelect(el, roles[contributors[idx].role]));
expect(hasInput(el, contributors[idx].name));
});
});
it("displays a blank field for adding a new contributor", () => {
const newContributorForm = wrapper.find(".contributor-form").last();
expect(hasSelect(newContributorForm, "Author"));
expect(hasInput(newContributorForm));
const button = newContributorForm.find(".btn.add-contributor");
expect(button.length).to.equal(1);
expect(button.text()).to.equal("Add");
});
it("deletes a contributor", () => {
expect(wrapper.state()["contributors"]).to.eql(contributors);
let existingContributors = wrapper.find(".with-remove-button");
expect(existingContributors.length).to.equal(2);
expect(
hasSelect(existingContributors.first(), roles[contributors[0].role])
);
expect(hasInput(existingContributors.first(), contributors[0].name));
existingContributors.first().find("button").simulate("click");
expect(wrapper.state()["contributors"]).to.eql([contributors[1]]);
existingContributors = wrapper.find(".with-remove-button");
expect(existingContributors.length).to.equal(1);
expect(
hasSelect(existingContributors.first(), roles[contributors[1].role])
);
expect(hasInput(existingContributors.first(), contributors[1].name));
});
it("adds a contributor", () => {
expect(wrapper.state()["contributors"]).to.eql(contributors);
let existingContributors = wrapper.find(".with-remove-button");
expect(existingContributors.length).to.equal(2);
const newContributorForm = wrapper.find(".contributor-form").last();
newContributorForm.find("select").getDOMNode().value = "Illustrator";
newContributorForm.find("select").simulate("change");
newContributorForm.find("input").getDOMNode().value = "An Illustrator";
newContributorForm.find("input").simulate("change");
newContributorForm.find("button").simulate("click");
expect(wrapper.state()["contributors"]).to.eql(
contributors.concat({ role: "Illustrator", name: "An Illustrator" })
);
existingContributors = wrapper.find(".with-remove-button");
expect(existingContributors.length).to.equal(3);
expect(existingContributors.last().find("select").prop("value")).to.equal(
"Illustrator"
);
expect(existingContributors.last().find("input").prop("value")).to.equal(
"An Illustrator"
);
});
it("does not add an empty string as a contributor's name", () => {
const spyAddContributor = spy(wrapper.instance(), "addContributor");
const newContributorForm = wrapper.find(".contributor-form").last();
const button = newContributorForm.find("button");
expect(wrapper.state()["disabled"]).to.be.true;
button.simulate("click");
// Nothing happens.
expect(spyAddContributor.callCount).to.equal(0);
newContributorForm.find("input").getDOMNode().value = "An Author";
newContributorForm.find("input").simulate("change");
expect(wrapper.state()["disabled"]).to.be.false;
button.simulate("click");
// The button works now!
expect(spyAddContributor.callCount).to.equal(1);
// Deleting the text disables the button again.
newContributorForm.find("input").getDOMNode().value = "";
newContributorForm.find("input").simulate("change");
expect(wrapper.state()["disabled"]).to.be.true;
expect(wrapper.state()["disabled"]).to.be.true;
button.simulate("click");
expect(spyAddContributor.callCount).to.equal(1);
spyAddContributor.restore();
});
it("looks up the full name of a contributor's role", () => {
expect(wrapper.instance().getContributorRole(contributors[0])).to.equal(
"Translator"
);
// If the look-up doesn't yield anything, it just uses the abbreviated version.
const adapter = { name: "An Adapter", role: "adp" };
expect(wrapper.instance().getContributorRole(adapter)).to.equal("adp");
});
it("resets the form after adding a contributor", () => {
// Add a contributor:
let newContributorForm = wrapper.find(".contributor-form").last();
newContributorForm.find("select").getDOMNode().value = "Illustrator";
newContributorForm.find("select").simulate("change");
newContributorForm.find("input").getDOMNode().value = "An Illustrator";
newContributorForm.find("input").simulate("change");
newContributorForm.find("button").simulate("click");
newContributorForm = wrapper.find(".contributor-form").last();
// The input field is blank.
expect(newContributorForm.find("input").prop("value")).to.equal("");
// The drop-down menu has defaulted to "Author".
expect(newContributorForm.find("select").prop("value")).to.equal("Author");
// The button is disabled.
expect(wrapper.state()["disabled"]).to.be.true;
});
});