@thepalaceproject/circulation-admin 1.21.0-post.2 → 1.21.0-post.3
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
CHANGED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { renderWithProviders } from "../testUtils/withProviders";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import {
|
|
5
|
+
bookEditorApiEndpoints,
|
|
6
|
+
PER_LIBRARY_SUPPRESS_REL,
|
|
7
|
+
PER_LIBRARY_UNSUPPRESS_REL,
|
|
8
|
+
} from "../../../src/features/book/bookEditorSlice";
|
|
9
|
+
import { BookDetailsEditor } from "../../../src/components/BookDetailsEditor";
|
|
10
|
+
import { expect } from "chai";
|
|
11
|
+
import { store } from "../../../src/store";
|
|
12
|
+
import * as fetchMock from "fetch-mock-jest";
|
|
13
|
+
|
|
14
|
+
describe("BookDetails", () => {
|
|
15
|
+
const suppressPerLibraryLink = {
|
|
16
|
+
href: "/suppress/href",
|
|
17
|
+
rel: PER_LIBRARY_SUPPRESS_REL,
|
|
18
|
+
};
|
|
19
|
+
const unsuppressPerLibraryLink = {
|
|
20
|
+
href: "/unsuppress/href",
|
|
21
|
+
rel: PER_LIBRARY_UNSUPPRESS_REL,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
let fetchBookData;
|
|
25
|
+
let fetchRoles;
|
|
26
|
+
let fetchMedia;
|
|
27
|
+
let fetchLanguages;
|
|
28
|
+
let postBookData;
|
|
29
|
+
let dispatchProps;
|
|
30
|
+
const suppressBook = jest.fn().mockImplementation((url: string) =>
|
|
31
|
+
store.dispatch(
|
|
32
|
+
bookEditorApiEndpoints.endpoints.suppressBook.initiate({
|
|
33
|
+
url,
|
|
34
|
+
csrfToken: "token",
|
|
35
|
+
})
|
|
36
|
+
)
|
|
37
|
+
);
|
|
38
|
+
const unsuppressBook = jest.fn().mockImplementation((url: string) =>
|
|
39
|
+
store.dispatch(
|
|
40
|
+
bookEditorApiEndpoints.endpoints.unsuppressBook.initiate({
|
|
41
|
+
url,
|
|
42
|
+
csrfToken: "token",
|
|
43
|
+
})
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
beforeAll(() => {
|
|
48
|
+
fetchMock
|
|
49
|
+
.post("/suppress/href", {
|
|
50
|
+
status: 200,
|
|
51
|
+
body: { message: "Successfully suppressed book availability." },
|
|
52
|
+
})
|
|
53
|
+
.delete("/unsuppress/href", {
|
|
54
|
+
status: 200,
|
|
55
|
+
body: { message: "Successfully unsuppressed book availability." },
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
fetchBookData = jest.fn();
|
|
60
|
+
fetchRoles = jest.fn();
|
|
61
|
+
fetchMedia = jest.fn();
|
|
62
|
+
fetchLanguages = jest.fn();
|
|
63
|
+
postBookData = jest.fn();
|
|
64
|
+
dispatchProps = {
|
|
65
|
+
fetchBookData,
|
|
66
|
+
fetchRoles,
|
|
67
|
+
fetchMedia,
|
|
68
|
+
fetchLanguages,
|
|
69
|
+
postBookData,
|
|
70
|
+
suppressBook,
|
|
71
|
+
unsuppressBook,
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
afterEach(() => {
|
|
75
|
+
jest.clearAllMocks();
|
|
76
|
+
fetchMock.resetHistory();
|
|
77
|
+
});
|
|
78
|
+
afterAll(() => {
|
|
79
|
+
jest.restoreAllMocks();
|
|
80
|
+
fetchMock.restore();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("uses modal for suppress book confirmation", async () => {
|
|
84
|
+
// Configure standard constructors so that RTK Query works in tests with FetchMockJest
|
|
85
|
+
Object.assign(fetchMock.config, {
|
|
86
|
+
fetch,
|
|
87
|
+
Headers,
|
|
88
|
+
Request,
|
|
89
|
+
Response,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const user = userEvent.setup();
|
|
93
|
+
|
|
94
|
+
const { getByRole, getByText, queryByRole } = renderWithProviders(
|
|
95
|
+
<BookDetailsEditor
|
|
96
|
+
bookData={{ id: "id", title: "title", suppressPerLibraryLink }}
|
|
97
|
+
bookUrl="url"
|
|
98
|
+
csrfToken="token"
|
|
99
|
+
{...dispatchProps}
|
|
100
|
+
/>
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// The `Hide` button should be present.
|
|
104
|
+
const hideButton = getByRole("button", { name: "Hide" });
|
|
105
|
+
|
|
106
|
+
// Clicking `Hide` should show the book suppression modal.
|
|
107
|
+
await user.click(hideButton);
|
|
108
|
+
getByRole("heading", { level: 4, name: "Suppressing Availability" });
|
|
109
|
+
getByText(/to hide this title from your library's catalog/);
|
|
110
|
+
let confirmButton = getByRole("button", { name: "Suppress Availability" });
|
|
111
|
+
let cancelButton = getByRole("button", { name: "Cancel" });
|
|
112
|
+
|
|
113
|
+
// Clicking `Cancel` should close the modal.
|
|
114
|
+
await user.click(cancelButton);
|
|
115
|
+
confirmButton = queryByRole("button", { name: "Suppress Availability" });
|
|
116
|
+
cancelButton = queryByRole("button", { name: "Cancel" });
|
|
117
|
+
expect(confirmButton).to.be.null;
|
|
118
|
+
expect(cancelButton).to.be.null;
|
|
119
|
+
|
|
120
|
+
// Clicking `Hide` again should show the modal again.
|
|
121
|
+
await user.click(hideButton);
|
|
122
|
+
confirmButton = getByRole("button", { name: "Suppress Availability" });
|
|
123
|
+
|
|
124
|
+
// Clicking the confirmation button should invoke the API and show a confirmation.
|
|
125
|
+
await user.click(confirmButton);
|
|
126
|
+
getByRole("heading", { level: 4, name: "Result" });
|
|
127
|
+
getByText(/Successfully suppressed book availability/);
|
|
128
|
+
getByRole("button", { name: "Dismiss" });
|
|
129
|
+
|
|
130
|
+
// Check that the API was invoked.
|
|
131
|
+
expect(suppressBook.mock.calls.length).to.equal(1);
|
|
132
|
+
expect(suppressBook.mock.calls[0][0]).to.equal("/suppress/href");
|
|
133
|
+
const fetchCalls = fetchMock.calls();
|
|
134
|
+
expect(fetchCalls.length).to.equal(1);
|
|
135
|
+
const fetchCall = fetchCalls[0];
|
|
136
|
+
const fetchOptions = fetchCalls[0][1];
|
|
137
|
+
expect(fetchCall[0]).to.equal("/suppress/href");
|
|
138
|
+
expect(fetchOptions["headers"]["X-CSRF-Token"]).to.contain("token");
|
|
139
|
+
expect(fetchOptions["method"]).to.equal("POST");
|
|
140
|
+
});
|
|
141
|
+
it("uses modal for unsuppress book confirmation", async () => {
|
|
142
|
+
// Configure standard constructors so that RTK Query works in tests with FetchMockJest
|
|
143
|
+
Object.assign(fetchMock.config, {
|
|
144
|
+
fetch,
|
|
145
|
+
Headers,
|
|
146
|
+
Request,
|
|
147
|
+
Response,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const user = userEvent.setup();
|
|
151
|
+
|
|
152
|
+
const { getByRole, getByText, queryByRole } = renderWithProviders(
|
|
153
|
+
<BookDetailsEditor
|
|
154
|
+
bookData={{ id: "id", title: "title", unsuppressPerLibraryLink }}
|
|
155
|
+
bookUrl="url"
|
|
156
|
+
csrfToken="token"
|
|
157
|
+
{...dispatchProps}
|
|
158
|
+
/>
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// The `Restore` button should be present.
|
|
162
|
+
const restoreButton = getByRole("button", { name: "Restore" });
|
|
163
|
+
|
|
164
|
+
// Clicking `Restore` should show the book un/suppression modal.
|
|
165
|
+
await user.click(restoreButton);
|
|
166
|
+
getByRole("heading", { level: 4, name: "Restoring Availability" });
|
|
167
|
+
getByText(/to make this title visible in your library's catalog/);
|
|
168
|
+
let confirmButton = getByRole("button", { name: "Restore Availability" });
|
|
169
|
+
let cancelButton = getByRole("button", { name: "Cancel" });
|
|
170
|
+
|
|
171
|
+
// Clicking `Cancel` should close the modal.
|
|
172
|
+
await user.click(cancelButton);
|
|
173
|
+
confirmButton = queryByRole("button", { name: "Restore Availability" });
|
|
174
|
+
cancelButton = queryByRole("button", { name: "Cancel" });
|
|
175
|
+
expect(confirmButton).to.be.null;
|
|
176
|
+
expect(cancelButton).to.be.null;
|
|
177
|
+
|
|
178
|
+
// Clicking `Restore` again should show the modal again.
|
|
179
|
+
await user.click(restoreButton);
|
|
180
|
+
confirmButton = getByRole("button", { name: "Restore Availability" });
|
|
181
|
+
|
|
182
|
+
// Clicking the confirmation button should invoke the API and show a confirmation.
|
|
183
|
+
await user.click(confirmButton);
|
|
184
|
+
getByRole("heading", { level: 4, name: "Result" });
|
|
185
|
+
getByText(/Successfully unsuppressed book availability/);
|
|
186
|
+
getByRole("button", { name: "Dismiss" });
|
|
187
|
+
|
|
188
|
+
// Check that the API was invoked.
|
|
189
|
+
expect(unsuppressBook.mock.calls.length).to.equal(1);
|
|
190
|
+
expect(unsuppressBook.mock.calls[0][0]).to.equal("/unsuppress/href");
|
|
191
|
+
const fetchCalls = fetchMock.calls();
|
|
192
|
+
expect(fetchCalls.length).to.equal(1);
|
|
193
|
+
const fetchCall = fetchCalls[0];
|
|
194
|
+
const fetchOptions = fetchCalls[0][1];
|
|
195
|
+
expect(fetchCall[0]).to.equal("/unsuppress/href");
|
|
196
|
+
expect(fetchOptions["headers"]["X-CSRF-Token"]).to.contain("token");
|
|
197
|
+
expect(fetchOptions["method"]).to.equal("DELETE");
|
|
198
|
+
});
|
|
199
|
+
});
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
+
import { Provider, ProviderProps } from "react-redux";
|
|
2
3
|
import ContextProvider, {
|
|
3
4
|
ContextProviderProps,
|
|
4
5
|
} from "../../../src/components/ContextProvider";
|
|
5
6
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
6
7
|
import { render, RenderOptions, RenderResult } from "@testing-library/react";
|
|
7
8
|
import { defaultFeatureFlags } from "../../../src/utils/featureFlags";
|
|
9
|
+
import { store } from "../../../src/store";
|
|
8
10
|
|
|
9
11
|
export type TestProviderWrapperOptions = {
|
|
12
|
+
reduxProviderProps?: ProviderProps;
|
|
10
13
|
contextProviderProps?: Partial<ContextProviderProps>;
|
|
11
14
|
queryClient?: QueryClient;
|
|
12
15
|
};
|
|
@@ -14,6 +17,10 @@ export type TestRenderWrapperOptions = TestProviderWrapperOptions & {
|
|
|
14
17
|
renderOptions?: Omit<RenderOptions, "queries">;
|
|
15
18
|
};
|
|
16
19
|
|
|
20
|
+
// The `store` argument is required for the Redux Provider and should
|
|
21
|
+
// be the same for both the Redux Provider and the ContextProvider.
|
|
22
|
+
const defaultReduxStore = store;
|
|
23
|
+
|
|
17
24
|
// The `csrfToken` context provider prop is required, so we provide
|
|
18
25
|
// a default value here, so it can be easily merged with other props.
|
|
19
26
|
const defaultContextProviderProps: ContextProviderProps = {
|
|
@@ -26,11 +33,15 @@ const defaultContextProviderProps: ContextProviderProps = {
|
|
|
26
33
|
* a React element for testing.
|
|
27
34
|
*
|
|
28
35
|
* @param {TestProviderWrapperOptions} options
|
|
36
|
+
* @param options.reduxProviderProps Props to pass to the Redux `Provider` wrapper
|
|
29
37
|
* @param {ContextProviderProps} options.contextProviderProps Props to pass to the ContextProvider wrapper
|
|
30
38
|
* @param {QueryClient} options.queryClient A `tanstack/react-query` QueryClient
|
|
31
39
|
* @returns {React.FunctionComponent} A React component that wraps children with our providers
|
|
32
40
|
*/
|
|
33
41
|
export const componentWithProviders = ({
|
|
42
|
+
reduxProviderProps = {
|
|
43
|
+
store: defaultReduxStore,
|
|
44
|
+
},
|
|
34
45
|
contextProviderProps = {
|
|
35
46
|
csrfToken: "",
|
|
36
47
|
featureFlags: defaultFeatureFlags,
|
|
@@ -40,11 +51,16 @@ export const componentWithProviders = ({
|
|
|
40
51
|
const effectiveContextProviderProps = {
|
|
41
52
|
...defaultContextProviderProps,
|
|
42
53
|
...contextProviderProps,
|
|
54
|
+
...reduxProviderProps.store, // Context and Redux Provider stores must match.
|
|
43
55
|
};
|
|
44
56
|
const wrapper = ({ children }) => (
|
|
45
|
-
<
|
|
46
|
-
<
|
|
47
|
-
|
|
57
|
+
<Provider {...reduxProviderProps}>
|
|
58
|
+
<ContextProvider {...effectiveContextProviderProps}>
|
|
59
|
+
<QueryClientProvider client={queryClient}>
|
|
60
|
+
{children}
|
|
61
|
+
</QueryClientProvider>
|
|
62
|
+
</ContextProvider>
|
|
63
|
+
</Provider>
|
|
48
64
|
);
|
|
49
65
|
wrapper.displayName = "TestWrapperComponent";
|
|
50
66
|
return wrapper;
|