-
Notifications
You must be signed in to change notification settings - Fork 4
/
DiagnosticsTabContainer-test.tsx
160 lines (131 loc) · 4.9 KB
/
DiagnosticsTabContainer-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
import { expect } from "chai";
import * as React from "react";
import { shallow, mount } from "enzyme";
import { stub } from "sinon";
import buildStore from "../../store";
import { DiagnosticsTabContainer } from "../DiagnosticsTabContainer";
import DiagnosticsServiceType from "../DiagnosticsServiceType";
import LoadingIndicator from "@thepalaceproject/web-opds-client/lib/components/LoadingIndicator";
import ErrorMessage from "../ErrorMessage";
describe("DiagnosticsTabContainer", () => {
let wrapper;
let store;
let goToTab;
const ts1 = {
service: "test_service",
id: "1",
start: "start_time_string",
duration: "0",
collection_name: "collection1",
};
const diagnostics = {
monitor: [{ test_service_1: [{ collection1: [ts1] }] }],
};
const fetchDiagnostics = stub();
beforeEach(() => {
goToTab = stub();
wrapper = mount(
<DiagnosticsTabContainer
goToTab={goToTab}
tab="coverage_provider"
store={buildStore()}
fetchDiagnostics={fetchDiagnostics}
diagnostics={{ coverage_provider: [] }}
isLoaded={true}
/>
);
});
describe("rendering", () => {
it("renders a tab container", () => {
expect(wrapper.render().hasClass("tab-container")).to.be.true;
});
it("renders tabs and defaults to showing the Coverage Providers tab", () => {
const nav = wrapper.find(".nav-tabs").at(0);
expect(nav.length).to.equal(1);
const tabs = nav.find("li");
expect(tabs.length).to.equal(4);
const cpTab = tabs.at(0);
expect(cpTab.text()).to.equal("Coverage Providers");
expect(cpTab.hasClass("active")).to.be.true;
const monitorTab = tabs.at(1);
expect(monitorTab.text()).to.equal("Monitors");
expect(monitorTab.hasClass("active")).to.be.false;
const scriptTab = tabs.at(2);
expect(scriptTab.text()).to.equal("Scripts");
expect(scriptTab.hasClass("active")).to.be.false;
const otherTab = tabs.at(3);
expect(otherTab.text()).to.equal("Other");
expect(otherTab.hasClass("active")).to.be.false;
});
it("renders tab content", () => {
wrapper.setProps({ diagnostics });
const serviceTypes = wrapper.find(DiagnosticsServiceType);
expect(serviceTypes.length).to.equal(4);
const cpContent = serviceTypes.at(0);
expect(cpContent.prop("type")).to.equal("coverage_provider");
expect(cpContent.prop("services")).to.be.undefined;
const monitorContent = serviceTypes.at(1);
expect(monitorContent.prop("type")).to.equal("monitor");
expect(monitorContent.prop("services")).to.equal(
wrapper.prop("diagnostics")["monitor"]
);
const scriptContent = serviceTypes.at(2);
expect(scriptContent.prop("type")).to.equal("script");
expect(scriptContent.prop("services")).to.be.undefined;
const otherContent = serviceTypes.at(3);
expect(otherContent.prop("type")).to.equal("other");
expect(otherContent.prop("services")).to.be.undefined;
});
});
describe("behavior", () => {
it("calls fetchDiagnostics on mount", () => {
expect(fetchDiagnostics.called).to.be.true;
});
it("shows the loading indicator", () => {
expect(wrapper.find(LoadingIndicator).length).to.equal(0);
wrapper.setProps({ isLoaded: false });
expect(wrapper.find(LoadingIndicator).length).to.equal(4);
});
it("shows an error message", () => {
let error = wrapper.find(ErrorMessage);
expect(error.length).to.equal(0);
const fetchError = {
status: 401,
response: "error fetching diagnostics",
};
wrapper.setProps({ fetchError });
error = wrapper.find(ErrorMessage);
expect(error.length).to.equal(4);
});
it("calls goToTab", () => {
wrapper = shallow(
<DiagnosticsTabContainer
goToTab={goToTab}
tab="coverage_provider"
store={buildStore()}
fetchDiagnostics={fetchDiagnostics}
diagnostics={diagnostics}
isLoaded={true}
/>
);
const tabs = wrapper.find("ul.nav-tabs").find("a");
const monitorTab = tabs.at(1);
monitorTab.simulate("click", {
currentTarget: { dataset: { tabkey: "monitor" } },
});
expect(goToTab.callCount).to.equal(1);
expect(goToTab.args[0][0]).to.equal("monitor");
});
it("switches tabs when the tab prop changes", () => {
let cpTab = wrapper.find("ul.nav-tabs").find("li").at(0);
let monitorTab = wrapper.find("ul.nav-tabs").find("li").at(1);
expect(cpTab.hasClass("active")).to.be.true;
expect(monitorTab.hasClass("active")).to.be.false;
wrapper.setProps({ tab: "monitor" });
cpTab = wrapper.find("ul.nav-tabs").find("li").at(0);
monitorTab = wrapper.find("ul.nav-tabs").find("li").at(1);
expect(cpTab.hasClass("active")).to.be.false;
expect(monitorTab.hasClass("active")).to.be.true;
});
});
});