-
Notifications
You must be signed in to change notification settings - Fork 4
/
LibraryStats-test.tsx
281 lines (251 loc) · 9.71 KB
/
LibraryStats-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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { expect } from "chai";
import * as React from "react";
import { mount } from "enzyme";
import { componentWithProviders } from "../../../tests/jest/testUtils/withProviders";
import LibraryStats from "../LibraryStats";
import { BarChart } from "recharts";
import {
statisticsApiResponseData,
testLibraryKey,
noCollectionsLibraryKey,
noInventoryLibraryKey,
noPatronsLibraryKey,
} from "../../../tests/__data__/statisticsApiResponseData";
import { normalizeStatistics } from "../../features/stats/normalizeStatistics";
import { ContextProviderProps } from "../ContextProvider";
const getAllProviders = ({ isSysAdmin = false } = {}) => {
const contextProviderProps: Partial<ContextProviderProps> = isSysAdmin
? { roles: [{ role: "system" }] }
: {};
return componentWithProviders({ contextProviderProps });
};
global.ResizeObserver = require("resize-observer-polyfill");
describe("LibraryStats", () => {
// Convert from the API format to our in-app format.
const statisticsData = normalizeStatistics(statisticsApiResponseData);
const librariesStatsTestDataByKey = statisticsData.libraries.reduce(
(map, library) => ({ ...map, [library.key]: library }),
{}
);
const defaultLibraryStatsTestData =
librariesStatsTestDataByKey[testLibraryKey];
const allLibrariesHeadingText = "All Authorized Libraries";
const noCollectionsHeadingText = "No associated collections.";
const expectStats = (
{ label, value },
expected_label: string,
expected_value
) => {
expect(label).to.equal(expected_label);
expect(value).to.equal(expected_value);
};
const expectAllGroups = (groups) => {
expect(groups.length).to.equal(4);
expect(groups.at(0).text()).to.contain("Patrons");
expect(groups.at(1).text()).to.contain("Circulation");
expect(groups.at(2).text()).to.contain("Inventory");
};
describe("rendering", () => {
let wrapper;
beforeEach(() => {
wrapper = mount(<LibraryStats stats={defaultLibraryStatsTestData} />, {
wrappingComponent: getAllProviders(),
});
});
it("shows 'all libraries' header when there is no library", () => {
const header = wrapper.find("h2");
expect(header.text()).to.contain(allLibrariesHeadingText);
});
it("shows library header", () => {
wrapper.setProps({ library: defaultLibraryStatsTestData.key });
const header = wrapper.find("h2");
expect(header.text()).to.contain(defaultLibraryStatsTestData.name);
expect(header.text()).not.to.contain(allLibrariesHeadingText);
});
it("show patrons and circulation groups, even when no patrons", () => {
const noPatrons = librariesStatsTestDataByKey[noPatronsLibraryKey];
wrapper.setProps({ stats: noPatrons });
const groups = wrapper.find(".stat-group");
expectAllGroups(groups);
});
it("shows inventory group, even when there is no inventory", () => {
const noInventory = librariesStatsTestDataByKey[noInventoryLibraryKey];
wrapper.setProps({ stats: noInventory });
const groups = wrapper.find(".stat-group");
expectAllGroups(groups);
});
it("shows appropriate message when there are no collections", () => {
const noCollections =
librariesStatsTestDataByKey[noCollectionsLibraryKey];
wrapper.setProps({ stats: noCollections });
const groups = wrapper.find(".stat-group");
expectAllGroups(groups);
expect(groups.at(3).text()).to.contain(noCollectionsHeadingText);
});
it("shows stats data", () => {
const groups = wrapper.find(".stat-group");
let statItems;
expect(groups.length).to.equal(4);
/* Patrons */
expect(groups.at(0).text()).to.contain("Current Circulation Activity");
statItems = groups.at(0).find("SingleStatListItem");
expectStats(statItems.at(0).props(), "Patrons With Active Loans", 21);
expectStats(
statItems.at(1).props(),
"Patrons With Active Loans or Holds",
23
);
expect(groups.at(0).text()).to.contain("21Patrons With Active Loans");
expect(groups.at(0).text()).to.contain(
"23Patrons With Active Loans or Holds"
);
/* Circulation */
expect(groups.at(1).text()).to.contain("Circulation");
statItems = groups.at(1).find("SingleStatListItem");
expect(statItems.length).to.equal(2);
expectStats(statItems.at(0).props(), "Active Loans", 87);
expectStats(statItems.at(1).props(), "Active Holds", 5);
expect(groups.at(1).text()).to.contain("87Active Loans");
expect(groups.at(1).text()).to.contain("5Active Holds");
/* Inventory */
expect(groups.at(2).text()).to.contain("Inventory");
statItems = groups.at(2).find("SingleStatListItem");
expect(statItems.length).to.equal(5);
expectStats(statItems.at(0).props(), "Titles", 29119);
expectStats(statItems.at(1).props(), "Available Titles", 29092);
expectStats(statItems.at(2).props(), "Metered License Titles", 20658);
expectStats(statItems.at(3).props(), "Unlimited License Titles", 623);
expectStats(statItems.at(4).props(), "Open Access Titles", 7838);
expect(groups.at(2).text()).to.contain("29.1kTitles");
expect(groups.at(2).text()).to.contain("29.1kAvailable Titles");
expect(groups.at(2).text()).to.contain("20.7kMetered License Titles");
expect(groups.at(2).text()).to.contain("623Unlimited License Titles");
expect(groups.at(2).text()).to.contain("7.8kOpen Access Titles");
/* Collections */
expect(groups.at(3).text()).to.contain("Collections");
});
it("shows barchart if user is sysadmin", () => {
wrapper = mount(<LibraryStats stats={defaultLibraryStatsTestData} />, {
wrappingComponent: getAllProviders({ isSysAdmin: true }),
});
const groups = wrapper.find(".stat-group");
expect(groups.length).to.equal(4);
// Chart data will be present because we're a sysadmin.
const chart = groups.at(3).find(BarChart);
expect(chart.length).to.equal(1);
const chartData = chart.props().data;
expect(chartData.length).to.equal(
defaultLibraryStatsTestData.collections.length
);
expect(chartData[0]).to.deep.equal({
name: "New BiblioBoard Test",
titles: 13306,
availableTitles: 13306,
selfHostedTitles: 0,
openAccessTitles: 0,
licensedTitles: 13306,
unlimitedLicenseTitles: 0,
meteredLicenseTitles: 13306,
meteredLicensesOwned: 13306,
meteredLicensesAvailable: 13306,
_by_medium: {},
});
expect(chart.props().data).to.deep.equal([
{
name: "New BiblioBoard Test",
titles: 13306,
availableTitles: 13306,
selfHostedTitles: 0,
openAccessTitles: 0,
licensedTitles: 13306,
unlimitedLicenseTitles: 0,
meteredLicenseTitles: 13306,
meteredLicensesOwned: 13306,
meteredLicensesAvailable: 13306,
_by_medium: {},
},
{
name: "New Bibliotheca Test Collection",
titles: 76,
availableTitles: 64,
selfHostedTitles: 0,
openAccessTitles: 0,
licensedTitles: 76,
unlimitedLicenseTitles: 0,
meteredLicenseTitles: 76,
meteredLicensesOwned: 85,
meteredLicensesAvailable: 72,
_by_medium: {},
},
{
name: "Palace Bookshelf",
titles: 7838,
availableTitles: 7838,
selfHostedTitles: 0,
openAccessTitles: 7838,
licensedTitles: 0,
unlimitedLicenseTitles: 0,
meteredLicenseTitles: 0,
meteredLicensesOwned: 0,
meteredLicensesAvailable: 0,
_by_medium: {},
},
{
name: "TEST Baker & Taylor",
titles: 146,
availableTitles: 134,
selfHostedTitles: 0,
openAccessTitles: 0,
licensedTitles: 146,
unlimitedLicenseTitles: 0,
meteredLicenseTitles: 146,
meteredLicensesOwned: 147,
meteredLicensesAvailable: 135,
_by_medium: {},
},
{
name: "TEST Palace Marketplace",
titles: 7753,
availableTitles: 7750,
selfHostedTitles: 0,
openAccessTitles: 0,
licensedTitles: 7753,
unlimitedLicenseTitles: 0,
meteredLicenseTitles: 7753,
meteredLicensesOwned: 305725,
meteredLicensesAvailable: 75337,
_by_medium: {},
},
]);
});
it("shows a list of collections instead of barchart, if not sysadmin", () => {
wrapper = mount(<LibraryStats stats={defaultLibraryStatsTestData} />, {
wrappingComponent: getAllProviders({ isSysAdmin: false }),
});
const groups = wrapper.find(".stat-group");
expect(groups.length).to.equal(4);
const collectionGroup = groups.at(3);
// No chart because we're not a sysadmin.
const chart = collectionGroup.find(BarChart);
expect(chart.length).to.equal(0);
// But we should still see a list with our collections.
const collectionNames = [
"New BiblioBoard Test",
"New Bibliotheca Test Collection",
"Palace Bookshelf",
"TEST Baker & Taylor",
"TEST Palace Marketplace",
];
const collectionsList = collectionGroup.find("ul");
const listItems = collectionsList.find("li");
expect(collectionsList.length).to.equal(1);
expect(listItems.length).to.equal(collectionNames.length);
collectionNames.forEach((name: string) => {
expect(collectionsList.text()).to.contain(name);
});
listItems.forEach((item, index) => {
expect(item.text()).to.contain(collectionNames[index]);
});
});
});
});