@thepalaceproject/circulation-admin 1.43.0-post.10 → 1.43.0-post.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/tests/jest/components/Announcement.test.tsx +76 -0
- package/tests/jest/components/Autocomplete.test.tsx +67 -0
- package/tests/jest/components/BookDetailsEditor.test.tsx +275 -0
- package/tests/jest/components/BookEditForm.test.tsx +352 -0
- package/tests/jest/components/Classifications.test.tsx +166 -0
- package/tests/jest/components/ClassificationsTable.test.tsx +57 -0
- package/tests/jest/components/ColorPicker.test.tsx +58 -0
- package/tests/jest/components/ComplaintForm.test.tsx +171 -0
- package/tests/jest/components/Complaints.test.tsx +259 -0
- package/tests/jest/components/ContextProvider.test.tsx +138 -0
- package/tests/jest/components/CustomListEditor.test.tsx +367 -0
- package/tests/jest/components/CustomListsForBook.test.tsx +315 -0
- package/tests/jest/components/DiagnosticsServiceTabs.test.tsx +141 -0
- package/tests/jest/components/DiagnosticsServiceType.test.tsx +75 -0
- package/tests/jest/components/LibraryEditForm.test.tsx +383 -0
- package/tests/jest/components/LibraryRegistrationForm.test.tsx +203 -0
- package/tests/jest/components/PatronAuthServices.test.tsx +114 -0
- package/tests/jest/components/ResetAdobeId.test.tsx +302 -0
- package/tests/jest/components/SitewideAnnouncements.test.tsx +134 -0
- package/tests/jest/components/TroubleshootingCategoryPage.test.tsx +69 -0
- package/tests/jest/components/TroubleshootingPage.test.tsx +94 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { render, screen, within } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
|
|
5
|
+
import CustomListEditor, {
|
|
6
|
+
CustomListEditorProps,
|
|
7
|
+
} from "../../../src/components/CustomListEditor";
|
|
8
|
+
|
|
9
|
+
// Each list entry renders a web-opds-client CatalogLink whose `collectionUrl` is
|
|
10
|
+
// the list's opdsFeedUrl. Mock it to a marker that echoes that URL, so the
|
|
11
|
+
// crawlable-feed derivation can be asserted without router/pathFor context.
|
|
12
|
+
jest.mock(
|
|
13
|
+
"@thepalaceproject/web-opds-client/lib/components/CatalogLink",
|
|
14
|
+
() => ({
|
|
15
|
+
__esModule: true,
|
|
16
|
+
default: (props: any) => (
|
|
17
|
+
<a
|
|
18
|
+
href={props.bookUrl}
|
|
19
|
+
data-testid="catalog-link"
|
|
20
|
+
data-collection-url={props.collectionUrl}
|
|
21
|
+
>
|
|
22
|
+
{props.children}
|
|
23
|
+
</a>
|
|
24
|
+
),
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
describe("CustomListEditor", () => {
|
|
29
|
+
// CustomListEntriesEditor calls Element.scrollTo on mount, which JSDOM does
|
|
30
|
+
// not implement, so stub it.
|
|
31
|
+
Element.prototype.scrollTo = () => {};
|
|
32
|
+
|
|
33
|
+
const searchResults = {
|
|
34
|
+
id: "id",
|
|
35
|
+
url: "url",
|
|
36
|
+
title: "title",
|
|
37
|
+
lanes: [],
|
|
38
|
+
books: [],
|
|
39
|
+
navigationLinks: [],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const entries = {
|
|
43
|
+
baseline: [],
|
|
44
|
+
baselineTotalCount: 0,
|
|
45
|
+
added: {},
|
|
46
|
+
removed: {},
|
|
47
|
+
current: [],
|
|
48
|
+
currentTotalCount: 0,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const entryPoints = ["Book", "Audio"];
|
|
52
|
+
|
|
53
|
+
const library = {
|
|
54
|
+
uuid: "uuid",
|
|
55
|
+
name: "name",
|
|
56
|
+
short_name: "library",
|
|
57
|
+
settings: {
|
|
58
|
+
large_collections: ["eng", "fre", "spa"],
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const languages = {
|
|
63
|
+
eng: ["English"],
|
|
64
|
+
spa: ["Spanish", "Castilian"],
|
|
65
|
+
fre: ["French"],
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const properties = {
|
|
69
|
+
name: "Listy McList",
|
|
70
|
+
collections: [2],
|
|
71
|
+
autoUpdate: false,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const searchParams = {
|
|
75
|
+
entryPoint: "All",
|
|
76
|
+
terms: "",
|
|
77
|
+
sort: null,
|
|
78
|
+
language: "all",
|
|
79
|
+
advanced: {
|
|
80
|
+
include: {
|
|
81
|
+
query: null,
|
|
82
|
+
selectedQueryId: null,
|
|
83
|
+
clearFilters: null,
|
|
84
|
+
},
|
|
85
|
+
exclude: {
|
|
86
|
+
query: null,
|
|
87
|
+
selectedQueryId: null,
|
|
88
|
+
clearFilters: null,
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const makeProps = (
|
|
94
|
+
overrides: Partial<CustomListEditorProps> = {}
|
|
95
|
+
): CustomListEditorProps => ({
|
|
96
|
+
entries,
|
|
97
|
+
entryPoints,
|
|
98
|
+
isFetchingMoreCustomListEntries: false,
|
|
99
|
+
isFetchingSearchResults: false,
|
|
100
|
+
isFetchingMoreSearchResults: false,
|
|
101
|
+
isLoaded: false,
|
|
102
|
+
isModified: true,
|
|
103
|
+
isOwner: true,
|
|
104
|
+
isValid: true,
|
|
105
|
+
languages,
|
|
106
|
+
library: library as CustomListEditorProps["library"],
|
|
107
|
+
listId: "1",
|
|
108
|
+
properties: properties as CustomListEditorProps["properties"],
|
|
109
|
+
searchParams: searchParams as CustomListEditorProps["searchParams"],
|
|
110
|
+
searchResults: searchResults as CustomListEditorProps["searchResults"],
|
|
111
|
+
loadMoreEntries: jest.fn(),
|
|
112
|
+
loadMoreSearchResults: jest.fn(),
|
|
113
|
+
reset: jest.fn(),
|
|
114
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
115
|
+
search: jest.fn(),
|
|
116
|
+
share: jest.fn(),
|
|
117
|
+
updateProperty: jest.fn(),
|
|
118
|
+
updateSearchParam: jest.fn(),
|
|
119
|
+
...overrides,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const renderEditor = (overrides: Partial<CustomListEditorProps> = {}) => {
|
|
123
|
+
const props = makeProps(overrides);
|
|
124
|
+
const result = render(<CustomListEditor {...props} />);
|
|
125
|
+
const rerender = (nextOverrides: Partial<CustomListEditorProps>) =>
|
|
126
|
+
result.rerender(<CustomListEditor {...makeProps(nextOverrides)} />);
|
|
127
|
+
return { ...result, rerender, props };
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
it("shows the list title with an edit button", () => {
|
|
131
|
+
renderEditor();
|
|
132
|
+
|
|
133
|
+
// The editable title (TextWithEditMode) shows the name and an edit button.
|
|
134
|
+
expect(screen.getByText("Listy McList")).toBeInTheDocument();
|
|
135
|
+
expect(
|
|
136
|
+
screen.getByRole("button", { name: /Edit list title/ })
|
|
137
|
+
).toBeInTheDocument();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("shows the list title without an edit button when isOwner is false", () => {
|
|
141
|
+
renderEditor({ isOwner: false });
|
|
142
|
+
|
|
143
|
+
const title = screen.getByRole("heading", { level: 3 });
|
|
144
|
+
expect(title).toHaveTextContent("Listy McList");
|
|
145
|
+
expect(
|
|
146
|
+
screen.queryByRole("button", { name: /Edit list title/ })
|
|
147
|
+
).not.toBeInTheDocument();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("shows the list id", () => {
|
|
151
|
+
renderEditor();
|
|
152
|
+
|
|
153
|
+
const listId = screen.getByRole("heading", { level: 4, name: /ID-1/ });
|
|
154
|
+
expect(listId).toHaveTextContent("1");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("shows an entries editor with list entries and search results", () => {
|
|
158
|
+
renderEditor();
|
|
159
|
+
|
|
160
|
+
// The entries editor renders both the search results and list-entries columns.
|
|
161
|
+
expect(
|
|
162
|
+
screen.getByRole("heading", { name: "Search Results" })
|
|
163
|
+
).toBeInTheDocument();
|
|
164
|
+
expect(
|
|
165
|
+
screen.getByRole("heading", {
|
|
166
|
+
name: /List Entries:.*No books in this list/,
|
|
167
|
+
})
|
|
168
|
+
).toBeInTheDocument();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("gives each entry of a saved list a crawlable OPDS feed URL", () => {
|
|
172
|
+
// opdsFeedUrl is `${short_name}/lists/${savedName}/crawlable`, but only when
|
|
173
|
+
// the list has both a listId and a savedName (CustomListEditor.tsx:124). It
|
|
174
|
+
// reaches the DOM as the collectionUrl of each list entry's catalog link.
|
|
175
|
+
renderEditor({
|
|
176
|
+
savedName: "saved-list",
|
|
177
|
+
entries: {
|
|
178
|
+
...entries,
|
|
179
|
+
current: [{ id: "book1", title: "A Book", url: "/works/book1" }],
|
|
180
|
+
currentTotalCount: 1,
|
|
181
|
+
} as CustomListEditorProps["entries"],
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
expect(screen.getByTestId("catalog-link")).toHaveAttribute(
|
|
185
|
+
"data-collection-url",
|
|
186
|
+
"library/lists/saved-list/crawlable"
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("disables entry point options when isOwner is false", () => {
|
|
191
|
+
const { container } = renderEditor({ isOwner: false });
|
|
192
|
+
|
|
193
|
+
const entryPointsSection =
|
|
194
|
+
container.querySelector<HTMLElement>(".entry-points");
|
|
195
|
+
within(entryPointsSection)
|
|
196
|
+
.getAllByRole("radio")
|
|
197
|
+
.forEach((radio) => expect(radio).toBeDisabled());
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("shows entry point options", () => {
|
|
201
|
+
const { container } = renderEditor();
|
|
202
|
+
|
|
203
|
+
const entryPointsSection =
|
|
204
|
+
container.querySelector<HTMLElement>(".entry-points");
|
|
205
|
+
|
|
206
|
+
const all = within(entryPointsSection).getByRole("radio", { name: "All" });
|
|
207
|
+
expect(all).toHaveAttribute("value", "All");
|
|
208
|
+
expect(all).toBeChecked();
|
|
209
|
+
|
|
210
|
+
const book = within(entryPointsSection).getByRole("radio", {
|
|
211
|
+
name: "Book",
|
|
212
|
+
});
|
|
213
|
+
expect(book).toHaveAttribute("value", "Book");
|
|
214
|
+
expect(book).not.toBeChecked();
|
|
215
|
+
|
|
216
|
+
const audio = within(entryPointsSection).getByRole("radio", {
|
|
217
|
+
name: "Audio",
|
|
218
|
+
});
|
|
219
|
+
expect(audio).toHaveAttribute("value", "Audio");
|
|
220
|
+
expect(audio).not.toBeChecked();
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("enables the save button when the list has changes and is valid", () => {
|
|
224
|
+
renderEditor();
|
|
225
|
+
|
|
226
|
+
expect(
|
|
227
|
+
screen.getByRole("button", { name: "Save this list" })
|
|
228
|
+
).toBeEnabled();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("disables the save button when the list does not have changes", () => {
|
|
232
|
+
renderEditor({ isModified: false });
|
|
233
|
+
|
|
234
|
+
expect(
|
|
235
|
+
screen.getByRole("button", { name: "Save this list" })
|
|
236
|
+
).toBeDisabled();
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("disables the save button when the list is invalid", () => {
|
|
240
|
+
renderEditor({ isValid: false });
|
|
241
|
+
|
|
242
|
+
expect(
|
|
243
|
+
screen.getByRole("button", { name: "Save this list" })
|
|
244
|
+
).toBeDisabled();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("calls save when the save button is clicked", async () => {
|
|
248
|
+
const user = userEvent.setup();
|
|
249
|
+
const save = jest.fn().mockResolvedValue(undefined);
|
|
250
|
+
renderEditor({ save });
|
|
251
|
+
|
|
252
|
+
await user.click(screen.getByRole("button", { name: "Save this list" }));
|
|
253
|
+
|
|
254
|
+
expect(save).toHaveBeenCalledTimes(1);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("disables the cancel button when the list does not have changes", () => {
|
|
258
|
+
renderEditor({ isModified: false });
|
|
259
|
+
|
|
260
|
+
expect(
|
|
261
|
+
screen.getByRole("button", { name: "Cancel changes" })
|
|
262
|
+
).toBeDisabled();
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("calls reset when the cancel button is clicked", async () => {
|
|
266
|
+
const user = userEvent.setup();
|
|
267
|
+
const reset = jest.fn();
|
|
268
|
+
renderEditor({ reset });
|
|
269
|
+
|
|
270
|
+
await user.click(screen.getByRole("button", { name: "Cancel changes" }));
|
|
271
|
+
|
|
272
|
+
expect(reset).toHaveBeenCalledTimes(1);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("does not render save/cancel buttons when isOwner is false", () => {
|
|
276
|
+
renderEditor({ isOwner: false });
|
|
277
|
+
|
|
278
|
+
expect(
|
|
279
|
+
screen.queryByRole("button", { name: "Save this list" })
|
|
280
|
+
).not.toBeInTheDocument();
|
|
281
|
+
expect(
|
|
282
|
+
screen.queryByRole("button", { name: "Cancel changes" })
|
|
283
|
+
).not.toBeInTheDocument();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("renders a CustomListSearch component", () => {
|
|
287
|
+
const { container } = renderEditor();
|
|
288
|
+
|
|
289
|
+
// CustomListSearch renders its search panel (a collapsible Panel whose
|
|
290
|
+
// header is a button) with the entry point options.
|
|
291
|
+
expect(
|
|
292
|
+
screen.getByRole("button", { name: "Search for titles" })
|
|
293
|
+
).toBeInTheDocument();
|
|
294
|
+
expect(container.querySelector(".entry-points")).toBeInTheDocument();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("updates the autoUpdate property from the search component", async () => {
|
|
298
|
+
const user = userEvent.setup();
|
|
299
|
+
// The auto-update controls only render when auto-update is enabled and the
|
|
300
|
+
// list is new (listId null), which is when the radios are interactive.
|
|
301
|
+
const updateProperty = jest.fn();
|
|
302
|
+
renderEditor({
|
|
303
|
+
updateProperty,
|
|
304
|
+
isAutoUpdateEnabled: true,
|
|
305
|
+
listId: null,
|
|
306
|
+
properties: { ...properties, autoUpdate: false },
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
await user.click(
|
|
310
|
+
screen.getByRole("radio", { name: "Automatically update this list" })
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
expect(updateProperty).toHaveBeenCalledTimes(1);
|
|
314
|
+
expect(updateProperty).toHaveBeenCalledWith("autoUpdate", true);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("calls search when listId changes", () => {
|
|
318
|
+
const search = jest.fn();
|
|
319
|
+
const { rerender } = renderEditor({ search });
|
|
320
|
+
|
|
321
|
+
expect(search).toHaveBeenCalledTimes(1);
|
|
322
|
+
|
|
323
|
+
rerender({ search, listId: "2" });
|
|
324
|
+
|
|
325
|
+
expect(search).toHaveBeenCalledTimes(2);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("calls search when isLoaded changes", () => {
|
|
329
|
+
const search = jest.fn();
|
|
330
|
+
const { rerender } = renderEditor({ search });
|
|
331
|
+
|
|
332
|
+
expect(search).toHaveBeenCalledTimes(1);
|
|
333
|
+
|
|
334
|
+
rerender({ search, isLoaded: true });
|
|
335
|
+
|
|
336
|
+
expect(search).toHaveBeenCalledTimes(2);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it("calls share when the share button is clicked", async () => {
|
|
340
|
+
const user = userEvent.setup();
|
|
341
|
+
const share = jest.fn();
|
|
342
|
+
const { container } = renderEditor({ share });
|
|
343
|
+
|
|
344
|
+
const shareButton = container.querySelector<HTMLButtonElement>(
|
|
345
|
+
".sharing-info button"
|
|
346
|
+
);
|
|
347
|
+
expect(shareButton).toBeInTheDocument();
|
|
348
|
+
|
|
349
|
+
await user.click(shareButton);
|
|
350
|
+
|
|
351
|
+
expect(share).toHaveBeenCalledTimes(1);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("does not render a share button when listId is undefined", () => {
|
|
355
|
+
const { container } = renderEditor({ listId: undefined });
|
|
356
|
+
|
|
357
|
+
expect(container.querySelector(".sharing-info")).not.toBeInTheDocument();
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("does not render a share button when isOwner is false", () => {
|
|
361
|
+
const { container } = renderEditor({ isOwner: false });
|
|
362
|
+
|
|
363
|
+
expect(
|
|
364
|
+
container.querySelector(".sharing-info button")
|
|
365
|
+
).not.toBeInTheDocument();
|
|
366
|
+
});
|
|
367
|
+
});
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { render, screen, waitFor } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
|
|
5
|
+
import { renderWithProviders } from "../testUtils/withProviders";
|
|
6
|
+
import buildStore from "../../../src/store";
|
|
7
|
+
|
|
8
|
+
// CustomListsForBook renders a react-router v3 <Link> for which
|
|
9
|
+
// renderWithProviders supplies no router context. Mock it to a marker that
|
|
10
|
+
// exposes the `to` pathname and state so the destination can be asserted.
|
|
11
|
+
jest.mock("react-router", () => ({
|
|
12
|
+
...jest.requireActual("react-router"),
|
|
13
|
+
Link: (props: any) => (
|
|
14
|
+
<div
|
|
15
|
+
data-testid="Link"
|
|
16
|
+
data-pathname={
|
|
17
|
+
typeof props.to === "string" ? props.to : props.to?.pathname
|
|
18
|
+
}
|
|
19
|
+
data-book-title={
|
|
20
|
+
typeof props.to === "object" ? props.to?.state?.bookTitle : undefined
|
|
21
|
+
}
|
|
22
|
+
>
|
|
23
|
+
{props.children}
|
|
24
|
+
</div>
|
|
25
|
+
),
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
// Render the CONNECTED default export so that mapStateToProps / mapDispatchToProps
|
|
29
|
+
// are exercised. The book's lists and all available lists are fetched on mount
|
|
30
|
+
// and fed back in through the Redux store; book/bookUrl/library/refreshCatalog
|
|
31
|
+
// are own props.
|
|
32
|
+
import CustomListsForBook, {
|
|
33
|
+
CustomListsForBook as UnconnectedCustomListsForBook,
|
|
34
|
+
} from "../../../src/components/CustomListsForBook";
|
|
35
|
+
|
|
36
|
+
const allCustomLists = [
|
|
37
|
+
{ id: "1", name: "list 1", is_owner: true, is_shared: false },
|
|
38
|
+
{ id: "2", name: "list 2", is_owner: true, is_shared: false },
|
|
39
|
+
{ id: "3", name: "list 3", is_owner: true, is_shared: false },
|
|
40
|
+
];
|
|
41
|
+
const listsForBook = [
|
|
42
|
+
{ id: "2", name: "list 2", is_owner: true, is_shared: false },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const jsonResponse = (body: unknown) =>
|
|
46
|
+
new Response(JSON.stringify(body), {
|
|
47
|
+
status: 200,
|
|
48
|
+
headers: { "Content-Type": "application/json" },
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Serves the "all custom lists" endpoint and the per-book "/lists" endpoint from
|
|
52
|
+
// the supplied fixtures; answers POSTs (edit) with success.
|
|
53
|
+
const stubFetch = (allLists: unknown[], forBook: unknown[]) =>
|
|
54
|
+
jest
|
|
55
|
+
.spyOn(globalThis, "fetch")
|
|
56
|
+
.mockImplementation(async (url, opts?: RequestInit) => {
|
|
57
|
+
if (opts?.method === "POST") {
|
|
58
|
+
return new Response("success", { status: 200 });
|
|
59
|
+
}
|
|
60
|
+
const u = String(url);
|
|
61
|
+
if (u.includes("custom_lists")) {
|
|
62
|
+
return jsonResponse({ custom_lists: allLists });
|
|
63
|
+
}
|
|
64
|
+
return jsonResponse({ custom_lists: forBook });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const renderConnected = () => {
|
|
68
|
+
const store = buildStore();
|
|
69
|
+
const refreshCatalog = jest.fn().mockResolvedValue(undefined);
|
|
70
|
+
const result = renderWithProviders(
|
|
71
|
+
<CustomListsForBook
|
|
72
|
+
csrfToken="token"
|
|
73
|
+
book={{ id: "id", title: "test title" } as any}
|
|
74
|
+
bookUrl="works/book url"
|
|
75
|
+
library="library"
|
|
76
|
+
refreshCatalog={refreshCatalog}
|
|
77
|
+
/>,
|
|
78
|
+
{ reduxProviderProps: { store } }
|
|
79
|
+
);
|
|
80
|
+
return { ...result, refreshCatalog };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
describe("CustomListsForBook", () => {
|
|
84
|
+
afterEach(() => {
|
|
85
|
+
jest.restoreAllMocks();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("shows the book title and the book's current lists", async () => {
|
|
89
|
+
stubFetch(allCustomLists, listsForBook);
|
|
90
|
+
const { container } = renderConnected();
|
|
91
|
+
|
|
92
|
+
expect(
|
|
93
|
+
await screen.findByRole("heading", { level: 2, name: "test title" })
|
|
94
|
+
).toBeInTheDocument();
|
|
95
|
+
expect(
|
|
96
|
+
screen.getByRole("heading", { name: "Current Lists" })
|
|
97
|
+
).toBeInTheDocument();
|
|
98
|
+
|
|
99
|
+
const link = await screen.findByRole("link", { name: "list 2" });
|
|
100
|
+
expect(link).toHaveAttribute("href", "/admin/web/lists/library/edit/2");
|
|
101
|
+
// Exactly one current list is shown.
|
|
102
|
+
expect(container.querySelectorAll(".with-remove-button")).toHaveLength(1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("builds the current-list URL from the list's id, not its name", async () => {
|
|
106
|
+
// A list named "new" whose id is "42" proves the URL uses the id.
|
|
107
|
+
const newList = { id: "42", name: "new", is_owner: true, is_shared: false };
|
|
108
|
+
stubFetch([...allCustomLists, newList], [newList]);
|
|
109
|
+
renderConnected();
|
|
110
|
+
|
|
111
|
+
const link = await screen.findByRole("link", { name: "new" });
|
|
112
|
+
expect(link).toHaveAttribute("href", "/admin/web/lists/library/edit/42");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("shows a menu of the lists the book is not yet on", async () => {
|
|
116
|
+
stubFetch(allCustomLists, listsForBook);
|
|
117
|
+
const { container } = renderConnected();
|
|
118
|
+
|
|
119
|
+
await screen.findByRole("link", { name: "list 2" });
|
|
120
|
+
|
|
121
|
+
const select = container.querySelector("select");
|
|
122
|
+
expect(select).not.toBeNull();
|
|
123
|
+
const optionValues = Array.from(select.querySelectorAll("option")).map(
|
|
124
|
+
(o) => o.getAttribute("value")
|
|
125
|
+
);
|
|
126
|
+
// "list 2" is excluded because the book is already on it.
|
|
127
|
+
expect(optionValues).toStrictEqual(["list 1", "list 3"]);
|
|
128
|
+
expect(screen.getByText(/Select an existing list/)).toBeInTheDocument();
|
|
129
|
+
expect(screen.getByRole("button", { name: "Add" })).toBeInTheDocument();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("shows placeholder text when the book is not on any list", async () => {
|
|
133
|
+
stubFetch(allCustomLists, []);
|
|
134
|
+
const { container } = renderConnected();
|
|
135
|
+
|
|
136
|
+
expect(
|
|
137
|
+
await screen.findByText("This book is not currently on any lists.")
|
|
138
|
+
).toBeInTheDocument();
|
|
139
|
+
// With no current lists there is no "Current Lists" heading.
|
|
140
|
+
expect(
|
|
141
|
+
screen.queryByRole("heading", { name: "Current Lists" })
|
|
142
|
+
).not.toBeInTheDocument();
|
|
143
|
+
expect(container.querySelector(".with-remove-button")).toBeNull();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("hides the menu once the book is on every available list", async () => {
|
|
147
|
+
stubFetch(allCustomLists, allCustomLists);
|
|
148
|
+
const { container } = renderConnected();
|
|
149
|
+
|
|
150
|
+
expect(
|
|
151
|
+
await screen.findByText(
|
|
152
|
+
"This book has been added to all the available lists."
|
|
153
|
+
)
|
|
154
|
+
).toBeInTheDocument();
|
|
155
|
+
expect(container.querySelector("select")).toBeNull();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("shows a message instead of the input list when no lists exist", async () => {
|
|
159
|
+
stubFetch([], []);
|
|
160
|
+
renderConnected();
|
|
161
|
+
|
|
162
|
+
expect(
|
|
163
|
+
await screen.findByText("There are no available lists.")
|
|
164
|
+
).toBeInTheDocument();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("shows a link to the list creator that carries the book title", async () => {
|
|
168
|
+
stubFetch(allCustomLists, listsForBook);
|
|
169
|
+
renderConnected();
|
|
170
|
+
|
|
171
|
+
await screen.findByRole("link", { name: "list 2" });
|
|
172
|
+
|
|
173
|
+
const creatorLink = screen.getByTestId("Link");
|
|
174
|
+
expect(creatorLink).toHaveTextContent("Create a new list");
|
|
175
|
+
expect(creatorLink).toHaveAttribute(
|
|
176
|
+
"data-pathname",
|
|
177
|
+
"/admin/web/lists/library/create"
|
|
178
|
+
);
|
|
179
|
+
expect(creatorLink).toHaveAttribute("data-book-title", "test title");
|
|
180
|
+
expect(
|
|
181
|
+
screen.getByText(
|
|
182
|
+
"(The book title will be automatically copied and searched on the list creator page.)"
|
|
183
|
+
)
|
|
184
|
+
).toBeInTheDocument();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("fetches the book's lists and all custom lists on mount, without refreshing the catalog", async () => {
|
|
188
|
+
const fetchSpy = stubFetch(allCustomLists, listsForBook);
|
|
189
|
+
const { refreshCatalog } = renderConnected();
|
|
190
|
+
|
|
191
|
+
await screen.findByRole("link", { name: "list 2" });
|
|
192
|
+
|
|
193
|
+
const urls = fetchSpy.mock.calls.map(([u]) => String(u));
|
|
194
|
+
expect(urls).toContain("admin/works/book url/lists");
|
|
195
|
+
expect(urls.some((u) => u.includes("custom_lists"))).toBe(true);
|
|
196
|
+
expect(refreshCatalog).not.toHaveBeenCalled();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("does not re-fetch all custom lists on mount when they are already loaded", () => {
|
|
200
|
+
// The mount guard (CustomListsForBook.tsx:158) fetches the full list of
|
|
201
|
+
// custom lists only when they are not already present; the book's own lists
|
|
202
|
+
// are still fetched. Drive the already-loaded case through the unconnected
|
|
203
|
+
// class, since the connected mount always starts from an empty store.
|
|
204
|
+
const fetchAllCustomLists = jest.fn();
|
|
205
|
+
const fetchCustomListsForBook = jest.fn();
|
|
206
|
+
render(
|
|
207
|
+
<UnconnectedCustomListsForBook
|
|
208
|
+
csrfToken="token"
|
|
209
|
+
book={{ id: "id", title: "test title" } as any}
|
|
210
|
+
bookUrl="works/book url"
|
|
211
|
+
library="library"
|
|
212
|
+
refreshCatalog={jest.fn().mockResolvedValue(undefined)}
|
|
213
|
+
allCustomLists={allCustomLists as any}
|
|
214
|
+
customListsForBook={listsForBook as any}
|
|
215
|
+
fetchAllCustomLists={fetchAllCustomLists}
|
|
216
|
+
fetchCustomListsForBook={fetchCustomListsForBook}
|
|
217
|
+
editCustomListsForBook={jest.fn().mockResolvedValue(undefined)}
|
|
218
|
+
/>
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
expect(fetchCustomListsForBook).toHaveBeenCalledTimes(1);
|
|
222
|
+
expect(fetchAllCustomLists).not.toHaveBeenCalled();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("adds a list and posts the updated set of lists", async () => {
|
|
226
|
+
const user = userEvent.setup();
|
|
227
|
+
const fetchSpy = stubFetch(allCustomLists, listsForBook);
|
|
228
|
+
const { container, refreshCatalog } = renderConnected();
|
|
229
|
+
|
|
230
|
+
await screen.findByRole("link", { name: "list 2" });
|
|
231
|
+
|
|
232
|
+
await user.selectOptions(
|
|
233
|
+
container.querySelector("select") as HTMLSelectElement,
|
|
234
|
+
"list 1"
|
|
235
|
+
);
|
|
236
|
+
await user.click(screen.getByRole("button", { name: "Add" }));
|
|
237
|
+
|
|
238
|
+
await waitFor(() => expect(refreshCatalog).toHaveBeenCalled());
|
|
239
|
+
|
|
240
|
+
const postCall = fetchSpy.mock.calls.find(
|
|
241
|
+
([, opts]) => (opts as RequestInit)?.method === "POST"
|
|
242
|
+
);
|
|
243
|
+
expect(String(postCall[0])).toBe("admin/works/book url/lists");
|
|
244
|
+
const lists = JSON.parse(
|
|
245
|
+
((postCall[1] as RequestInit).body as FormData).get("lists") as string
|
|
246
|
+
);
|
|
247
|
+
expect(lists).toStrictEqual([
|
|
248
|
+
{ id: "2", name: "list 2", is_owner: true, is_shared: false },
|
|
249
|
+
{ id: "1", name: "list 1", is_owner: true, is_shared: false },
|
|
250
|
+
]);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("removes a list and posts the emptied set of lists", async () => {
|
|
254
|
+
const user = userEvent.setup();
|
|
255
|
+
const fetchSpy = stubFetch(allCustomLists, listsForBook);
|
|
256
|
+
const { refreshCatalog } = renderConnected();
|
|
257
|
+
|
|
258
|
+
await screen.findByRole("link", { name: "list 2" });
|
|
259
|
+
|
|
260
|
+
await user.click(screen.getByRole("button", { name: "Delete" }));
|
|
261
|
+
|
|
262
|
+
await waitFor(() => expect(refreshCatalog).toHaveBeenCalled());
|
|
263
|
+
|
|
264
|
+
const postCall = fetchSpy.mock.calls.find(
|
|
265
|
+
([, opts]) => (opts as RequestInit)?.method === "POST"
|
|
266
|
+
);
|
|
267
|
+
expect(String(postCall[0])).toBe("admin/works/book url/lists");
|
|
268
|
+
const lists = JSON.parse(
|
|
269
|
+
((postCall[1] as RequestInit).body as FormData).get("lists") as string
|
|
270
|
+
);
|
|
271
|
+
expect(lists).toStrictEqual([]);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("shows an error message", () => {
|
|
275
|
+
// A mount-time fetch failure would reject the (uncaught) fetch promise and
|
|
276
|
+
// crash the worker, so drive the error through the prop connect derives.
|
|
277
|
+
render(
|
|
278
|
+
<UnconnectedCustomListsForBook
|
|
279
|
+
csrfToken="token"
|
|
280
|
+
book={{ id: "id", title: "test title" } as any}
|
|
281
|
+
bookUrl="works/book url"
|
|
282
|
+
library="library"
|
|
283
|
+
refreshCatalog={jest.fn().mockResolvedValue(undefined)}
|
|
284
|
+
allCustomLists={allCustomLists as any}
|
|
285
|
+
customListsForBook={listsForBook as any}
|
|
286
|
+
fetchAllCustomLists={jest.fn()}
|
|
287
|
+
fetchCustomListsForBook={jest.fn()}
|
|
288
|
+
editCustomListsForBook={jest.fn().mockResolvedValue(undefined)}
|
|
289
|
+
fetchError={{ status: 400, response: "boom", url: "" } as any}
|
|
290
|
+
/>
|
|
291
|
+
);
|
|
292
|
+
expect(screen.getByText(/Error: boom/)).toBeInTheDocument();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("disables the remove and add controls while fetching", () => {
|
|
296
|
+
// isFetching is a connect-derived prop; inject it via the unconnected class.
|
|
297
|
+
render(
|
|
298
|
+
<UnconnectedCustomListsForBook
|
|
299
|
+
csrfToken="token"
|
|
300
|
+
book={{ id: "id", title: "test title" } as any}
|
|
301
|
+
bookUrl="works/book url"
|
|
302
|
+
library="library"
|
|
303
|
+
refreshCatalog={jest.fn().mockResolvedValue(undefined)}
|
|
304
|
+
allCustomLists={allCustomLists as any}
|
|
305
|
+
customListsForBook={listsForBook as any}
|
|
306
|
+
fetchAllCustomLists={jest.fn()}
|
|
307
|
+
fetchCustomListsForBook={jest.fn()}
|
|
308
|
+
editCustomListsForBook={jest.fn().mockResolvedValue(undefined)}
|
|
309
|
+
isFetching={true}
|
|
310
|
+
/>
|
|
311
|
+
);
|
|
312
|
+
expect(screen.getByRole("button", { name: "Delete" })).toBeDisabled();
|
|
313
|
+
expect(screen.getByRole("button", { name: "Add" })).toBeDisabled();
|
|
314
|
+
});
|
|
315
|
+
});
|