@thepalaceproject/circulation-admin 1.20.0 → 1.21.0-post.2
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/dist/circulation-admin.js +1 -1
- package/package.json +4 -4
- package/tests/jest/components/CustomLists.test.tsx +4 -0
- package/tests/jest/components/IndividualAdminEditForm.test.tsx +1 -0
- package/tests/jest/components/QuicksightDashboard.test.tsx +1 -0
- package/tests/jest/components/Stats.test.tsx +62 -1
- package/tests/jest/features/book.test.ts +396 -0
- package/tests/jest/testUtils/withProviders.tsx +9 -2
- package/webpack.dev-server.config.js +1 -1
package/package.json
CHANGED
|
@@ -61,8 +61,8 @@
|
|
|
61
61
|
"react-redux": "^7.2.9",
|
|
62
62
|
"react-router": "^3.2.0",
|
|
63
63
|
"recharts": "^1.8.6",
|
|
64
|
-
"redux": "^4.
|
|
65
|
-
"redux-thunk": "^2.
|
|
64
|
+
"redux": "^4.2.1",
|
|
65
|
+
"redux-thunk": "^2.4.2",
|
|
66
66
|
"request": "^2.85.0",
|
|
67
67
|
"stream-browserify": "^3.0.0",
|
|
68
68
|
"timers-browserify": "^2.0.12",
|
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
"eslint-plugin-prettier": "^3.1.3",
|
|
101
101
|
"eslint-plugin-react": "^7.19.0",
|
|
102
102
|
"eslint-plugin-react-hooks": "^4.0.0",
|
|
103
|
-
"fetch-mock": "^
|
|
103
|
+
"fetch-mock": "^10.0.7",
|
|
104
104
|
"fetch-mock-jest": "^1.5.1",
|
|
105
105
|
"fetch-ponyfill": "^7.1.0",
|
|
106
106
|
"file-loader": "^6.2.0",
|
|
@@ -149,5 +149,5 @@
|
|
|
149
149
|
"*.{js,jsx,ts,tsx,css,md}": "prettier --write",
|
|
150
150
|
"*.{js,css,md}": "prettier --write"
|
|
151
151
|
},
|
|
152
|
-
"version": "1.
|
|
152
|
+
"version": "1.21.0-post.2"
|
|
153
153
|
}
|
|
@@ -31,6 +31,7 @@ describe("CustomLists", () => {
|
|
|
31
31
|
|
|
32
32
|
const contextProviderProps = {
|
|
33
33
|
csrfToken: "",
|
|
34
|
+
featureFlags: {},
|
|
34
35
|
roles: [{ role: "system" }],
|
|
35
36
|
};
|
|
36
37
|
|
|
@@ -70,6 +71,7 @@ describe("CustomLists", () => {
|
|
|
70
71
|
|
|
71
72
|
const contextProviderProps = {
|
|
72
73
|
csrfToken: "",
|
|
74
|
+
featureFlags: {},
|
|
73
75
|
roles: [{ role: "system" }],
|
|
74
76
|
};
|
|
75
77
|
|
|
@@ -121,6 +123,7 @@ describe("CustomLists", () => {
|
|
|
121
123
|
|
|
122
124
|
const contextProviderProps = {
|
|
123
125
|
csrfToken: "",
|
|
126
|
+
featureFlags: {},
|
|
124
127
|
roles: [{ role: "system" }],
|
|
125
128
|
};
|
|
126
129
|
|
|
@@ -164,6 +167,7 @@ describe("CustomLists", () => {
|
|
|
164
167
|
|
|
165
168
|
const contextProviderProps = {
|
|
166
169
|
csrfToken: "",
|
|
170
|
+
featureFlags: {},
|
|
167
171
|
roles: [{ role: "system" }],
|
|
168
172
|
};
|
|
169
173
|
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { render } from "@testing-library/react";
|
|
3
|
-
import {
|
|
3
|
+
import LibraryStats, {
|
|
4
|
+
CustomTooltip,
|
|
5
|
+
} from "../../../src/components/LibraryStats";
|
|
6
|
+
import { renderWithProviders } from "../testUtils/withProviders";
|
|
7
|
+
import { ContextProviderProps } from "../../../src/components/ContextProvider";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
statisticsApiResponseData,
|
|
11
|
+
testLibraryKey as sampleLibraryKey,
|
|
12
|
+
} from "../../__data__/statisticsApiResponseData";
|
|
13
|
+
import { normalizeStatistics } from "../../../src/components/Stats";
|
|
4
14
|
|
|
5
15
|
describe("Dashboard Statistics", () => {
|
|
6
16
|
// NB: This adds test to the already existing tests in:
|
|
@@ -11,6 +21,57 @@ describe("Dashboard Statistics", () => {
|
|
|
11
21
|
// Those tests should eventually be migrated here and
|
|
12
22
|
// adapted to the Jest/React Testing Library paradigm.
|
|
13
23
|
|
|
24
|
+
describe("requesting inventory reports", () => {
|
|
25
|
+
// Convert from the API format to our in-app format.
|
|
26
|
+
const statisticsData = normalizeStatistics(statisticsApiResponseData);
|
|
27
|
+
const librariesStatsTestDataByKey = statisticsData.libraries.reduce(
|
|
28
|
+
(map, library) => ({ ...map, [library.key]: library }),
|
|
29
|
+
{}
|
|
30
|
+
);
|
|
31
|
+
const sampleStatsData = librariesStatsTestDataByKey[sampleLibraryKey];
|
|
32
|
+
|
|
33
|
+
const systemAdmin = [{ role: "system" }];
|
|
34
|
+
const managerAll = [{ role: "manager-all" }];
|
|
35
|
+
const librarianAll = [{ role: "librarian-all" }];
|
|
36
|
+
|
|
37
|
+
const baseContextProviderProps = {
|
|
38
|
+
csrfToken: "",
|
|
39
|
+
featureFlags: { reportsOnlyForSysadmins: false },
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const renderFor = (
|
|
43
|
+
onlySysadmins: boolean,
|
|
44
|
+
roles: { role: string; library?: string }[]
|
|
45
|
+
) => {
|
|
46
|
+
const contextProviderProps: ContextProviderProps = {
|
|
47
|
+
...baseContextProviderProps,
|
|
48
|
+
featureFlags: { reportsOnlyForSysadmins: onlySysadmins },
|
|
49
|
+
roles,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const { container, queryByRole } = renderWithProviders(
|
|
53
|
+
<LibraryStats stats={sampleStatsData} library={sampleLibraryKey} />,
|
|
54
|
+
{ contextProviderProps }
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const result = queryByRole("button", { name: "⬇︎" });
|
|
58
|
+
// Clean up the container after each render.
|
|
59
|
+
document.body.removeChild(container);
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
it("shows inventory reports only for sysadmins, if feature flag set", async () => {
|
|
64
|
+
// If the feature flag is set, the button should be visible only to sysadmins.
|
|
65
|
+
expect(renderFor(true, systemAdmin)).not.toBeNull();
|
|
66
|
+
expect(renderFor(true, managerAll)).toBeNull();
|
|
67
|
+
expect(renderFor(true, librarianAll)).toBeNull();
|
|
68
|
+
// If the feature flag is false, the button should be visible to all users.
|
|
69
|
+
expect(renderFor(false, systemAdmin)).not.toBeNull();
|
|
70
|
+
expect(renderFor(false, managerAll)).not.toBeNull();
|
|
71
|
+
expect(renderFor(false, librarianAll)).not.toBeNull();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
14
75
|
describe("charting - custom tooltip", () => {
|
|
15
76
|
const defaultLabel = "Collection X";
|
|
16
77
|
const summaryInventory = {
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import reducer, {
|
|
2
|
+
initialState,
|
|
3
|
+
getBookData,
|
|
4
|
+
GetBookDataArgs,
|
|
5
|
+
submitBookData,
|
|
6
|
+
} from "../../../src/features/book/bookEditorSlice";
|
|
7
|
+
import { expect } from "chai";
|
|
8
|
+
import * as fetchMock from "fetch-mock-jest";
|
|
9
|
+
import { store } from "../../../src/store";
|
|
10
|
+
import { BookData } from "@thepalaceproject/web-opds-client/lib/interfaces";
|
|
11
|
+
import { RequestError } from "@thepalaceproject/web-opds-client/lib/DataFetcher";
|
|
12
|
+
import { AsyncThunkAction, Dispatch } from "@reduxjs/toolkit";
|
|
13
|
+
import ActionCreator from "@thepalaceproject/web-opds-client/lib/actions";
|
|
14
|
+
|
|
15
|
+
const SAMPLE_BOOK_ADMIN_DETAIL = `
|
|
16
|
+
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:opds="http://opds-spec.org/2010/catalog" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:drm="http://librarysimplified.org/terms/drm" xmlns:schema="http://schema.org/" xmlns:simplified="http://librarysimplified.org/terms/" xmlns:bibframe="http://bibframe.org/vocab/" xmlns:bib="http://bib.schema.org/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:lcp="http://readium.org/lcp-specs/ns" schema:additionalType="http://schema.org/EBook">
|
|
17
|
+
<title>Tea-Cup Reading and Fortune-Telling by Tea Leaves</title>
|
|
18
|
+
<schema:alternativeHeadline>by a Highland Seer</schema:alternativeHeadline>
|
|
19
|
+
<summary>First written in the year 1881 by A Highland Seer, it contains a list of omens, both good and bad, many of which are very familiar. Folklore, mystic paths, and enlightenment of the divine are laid out in simple terms to help the everyday person interpret patterns found in tea leaves. (Google Books)</summary>
|
|
20
|
+
<simplified:pwid>60c23c76-f789-a51d-cd0b-31d2decf1271</simplified:pwid>
|
|
21
|
+
<dcterms:language>en</dcterms:language>
|
|
22
|
+
<dcterms:publisher>GEORGE SULLY AND COMPANY</dcterms:publisher>
|
|
23
|
+
<dcterms:issued>1921-01-01</dcterms:issued>
|
|
24
|
+
<id>urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1</id>
|
|
25
|
+
<bibframe:distribution bibframe:ProviderName="Palace Bookshelf"/>
|
|
26
|
+
<published>2022-12-17T00:00:00Z</published>
|
|
27
|
+
<updated>2023-05-03T16:31:10+00:00</updated>
|
|
28
|
+
<category scheme="http://schema.org/audience" term="Adult" label="Adult"/>
|
|
29
|
+
<author>
|
|
30
|
+
<name>Homer</name>
|
|
31
|
+
<link href="http://localhost:8080/minotaur-test-library/works/contributor/Homer/eng/Adult,Adults+Only,All+Ages,Children,Young+Adult" rel="contributor" type="application/atom+xml;profile=opds-catalog;kind=acquisition" title="Homer"/>
|
|
32
|
+
</author>
|
|
33
|
+
<link href="https://palace-bookshelf-downloads.dp.la/assets/0d970df7-275e-45ee-a90e-884f1a93beef?fit=inside&width=10000&height=560" rel="http://opds-spec.org/image" type="image/png"/>
|
|
34
|
+
<link href="https://palace-bookshelf-downloads.dp.la/0d970df7-275e-45ee-a90e-884f1a93beef.jpeg" rel="http://opds-spec.org/image/thumbnail" type="image/jpeg"/>
|
|
35
|
+
<link href="http://localhost:8080/minotaur-test-library/works/URI/urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1/borrow" rel="http://opds-spec.org/acquisition/borrow" type="application/atom+xml;type=entry;profile=opds-catalog">
|
|
36
|
+
<opds:indirectAcquisition type="application/epub+zip"/>
|
|
37
|
+
<opds:availability status="available"/>
|
|
38
|
+
</link>
|
|
39
|
+
<link href="http://localhost:8080/minotaur-test-library/works/URI/urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1" rel="alternate" type="application/atom+xml;type=entry;profile=opds-catalog"/>
|
|
40
|
+
<link href="http://localhost:8080/minotaur-test-library/works/URI/urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1/related_books" rel="related" type="application/atom+xml;profile=opds-catalog;kind=acquisition" title="Recommended Works"/>
|
|
41
|
+
<link href="http://localhost:8080/minotaur-test-library/annotations/URI/urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1" rel="http://www.w3.org/ns/oa#annotationService" type="application/ld+json; profile="http://www.w3.org/ns/anno.jsonld""/>
|
|
42
|
+
<link href="http://localhost:8080/minotaur-test-library/analytics/URI/urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1/open_book" rel="http://librarysimplified.org/terms/rel/analytics/open-book"/>
|
|
43
|
+
<link href="http://localhost:8080/minotaur-test-library/admin/works/URI/urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1/suppression" rel="http://palaceproject.io/terms/rel/suppress-for-library"/>
|
|
44
|
+
<link href="http://localhost:8080/admin/works/URI/urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1/edit" rel="edit"/>
|
|
45
|
+
</entry>
|
|
46
|
+
`;
|
|
47
|
+
const SAMPLE_BOOK_DATA_BROKEN_XML = `BROKEN ${SAMPLE_BOOK_ADMIN_DETAIL}`;
|
|
48
|
+
const FETCH_OPDS_PARSE_ERROR_MESSAGE = "Failed to parse OPDS data";
|
|
49
|
+
|
|
50
|
+
describe("Redux bookEditorSlice...", () => {
|
|
51
|
+
const bookData = { id: "urn:something:something", title: "test title" };
|
|
52
|
+
|
|
53
|
+
const fetchedState = {
|
|
54
|
+
url: "test url",
|
|
55
|
+
data: { ...bookData },
|
|
56
|
+
isFetching: false,
|
|
57
|
+
fetchError: null,
|
|
58
|
+
editError: null,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
describe("reducers...", () => {
|
|
62
|
+
it("should return the initial state from undefined, if no action is passed", () => {
|
|
63
|
+
expect(reducer(undefined, { type: "unknown" })).to.deep.equal(
|
|
64
|
+
initialState
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
it("should return the initial state from initialState, if no action is passed", () => {
|
|
68
|
+
expect(reducer(initialState, { type: "unknown" })).to.deep.equal(
|
|
69
|
+
initialState
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
it("should handle BOOK_CLEAR", () => {
|
|
73
|
+
// This is dispatched by `web-opds`client`, but we need to handle it, too.
|
|
74
|
+
const action = { type: ActionCreator.BOOK_CLEAR };
|
|
75
|
+
|
|
76
|
+
expect(reducer(fetchedState, action)).to.deep.equal(initialState);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should handle getBookData.pending", () => {
|
|
80
|
+
const action = {
|
|
81
|
+
type: getBookData.pending.type,
|
|
82
|
+
meta: { arg: { url: "https://example.com/book" } },
|
|
83
|
+
};
|
|
84
|
+
const previousState = { ...initialState, url: null, isFetching: false };
|
|
85
|
+
const state = reducer(previousState, action);
|
|
86
|
+
|
|
87
|
+
expect(state.url).to.equal("https://example.com/book");
|
|
88
|
+
expect(state.data).to.be.null;
|
|
89
|
+
expect(state.isFetching).to.equal(true);
|
|
90
|
+
expect(state.fetchError).to.be.null;
|
|
91
|
+
expect(state.editError).to.be.null;
|
|
92
|
+
});
|
|
93
|
+
it("should handle getBookData.fulfilled", () => {
|
|
94
|
+
const action = {
|
|
95
|
+
type: getBookData.fulfilled.type,
|
|
96
|
+
meta: { arg: { url: "https://example.com/book" } },
|
|
97
|
+
payload: bookData,
|
|
98
|
+
};
|
|
99
|
+
const previousState = {
|
|
100
|
+
...initialState,
|
|
101
|
+
url: null,
|
|
102
|
+
data: null,
|
|
103
|
+
isFetching: true,
|
|
104
|
+
};
|
|
105
|
+
const state = reducer(previousState, action);
|
|
106
|
+
|
|
107
|
+
expect(state.url).to.equal("https://example.com/book");
|
|
108
|
+
expect(state.data).to.deep.equal(bookData);
|
|
109
|
+
expect(state.isFetching).to.equal(false);
|
|
110
|
+
expect(state.fetchError).to.be.null;
|
|
111
|
+
expect(state.editError).to.be.null;
|
|
112
|
+
});
|
|
113
|
+
it("should handle getBookData.rejected", () => {
|
|
114
|
+
const errorObject = { error: "some error object" };
|
|
115
|
+
const action = {
|
|
116
|
+
type: getBookData.rejected.type,
|
|
117
|
+
meta: { arg: { url: "https://example.com/book" } },
|
|
118
|
+
payload: errorObject,
|
|
119
|
+
};
|
|
120
|
+
const previousState = {
|
|
121
|
+
...initialState,
|
|
122
|
+
url: null,
|
|
123
|
+
data: null,
|
|
124
|
+
isFetching: true,
|
|
125
|
+
};
|
|
126
|
+
const state = reducer(previousState, action);
|
|
127
|
+
|
|
128
|
+
expect(state.url).to.equal("https://example.com/book");
|
|
129
|
+
expect(state.data).to.be.null;
|
|
130
|
+
expect(state.isFetching).to.equal(false);
|
|
131
|
+
expect(state.fetchError).to.deep.equal(errorObject);
|
|
132
|
+
expect(state.editError).to.be.null;
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("should handle submitBookData.pending", () => {
|
|
136
|
+
const action = { type: submitBookData.pending.type };
|
|
137
|
+
const previousState = { ...fetchedState, isFetching: false };
|
|
138
|
+
const state = reducer(previousState, action);
|
|
139
|
+
|
|
140
|
+
expect(state).to.deep.equal({ ...fetchedState, isFetching: true });
|
|
141
|
+
});
|
|
142
|
+
it("should handle submitBookData.fulfilled", () => {
|
|
143
|
+
const action = {
|
|
144
|
+
type: submitBookData.fulfilled.type,
|
|
145
|
+
payload: "some value",
|
|
146
|
+
};
|
|
147
|
+
const previousState = { ...fetchedState, isFetching: true };
|
|
148
|
+
const state = reducer(previousState, action);
|
|
149
|
+
|
|
150
|
+
expect(state).to.deep.equal({
|
|
151
|
+
...fetchedState,
|
|
152
|
+
isFetching: false,
|
|
153
|
+
editError: null,
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
it("should handle submitBookData.rejected", () => {
|
|
157
|
+
const action = {
|
|
158
|
+
type: submitBookData.rejected.type,
|
|
159
|
+
payload: "some value",
|
|
160
|
+
};
|
|
161
|
+
const previousState = { ...fetchedState, isFetching: true };
|
|
162
|
+
const state = reducer(previousState, action);
|
|
163
|
+
|
|
164
|
+
expect(state).to.deep.equal({
|
|
165
|
+
...fetchedState,
|
|
166
|
+
isFetching: false,
|
|
167
|
+
editError: "some value",
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("thunks...", () => {
|
|
173
|
+
describe("getBookData...", () => {
|
|
174
|
+
const goodBookUrl = "https://example.com/book";
|
|
175
|
+
const brokenBookUrl = "https://example.com/broken-book";
|
|
176
|
+
const errorBookUrl = "https://example.com/error-book";
|
|
177
|
+
|
|
178
|
+
const dispatch = jest.fn();
|
|
179
|
+
const getState = jest.fn().mockReturnValue({
|
|
180
|
+
bookEditor: initialState,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
beforeAll(() => {
|
|
184
|
+
fetchMock
|
|
185
|
+
.get(goodBookUrl, { body: SAMPLE_BOOK_ADMIN_DETAIL, status: 200 })
|
|
186
|
+
.get(brokenBookUrl, {
|
|
187
|
+
body: SAMPLE_BOOK_DATA_BROKEN_XML,
|
|
188
|
+
status: 200,
|
|
189
|
+
})
|
|
190
|
+
.get(errorBookUrl, { body: "Internal server error", status: 400 });
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
afterEach(() => {
|
|
194
|
+
fetchMock.resetHistory();
|
|
195
|
+
dispatch.mockClear();
|
|
196
|
+
});
|
|
197
|
+
afterAll(() => fetchMock.restore());
|
|
198
|
+
|
|
199
|
+
it("should return the book data on the happy path", async () => {
|
|
200
|
+
const action = getBookData({ url: goodBookUrl });
|
|
201
|
+
|
|
202
|
+
const result = await action(dispatch, getState, undefined);
|
|
203
|
+
const dispatchCalls = dispatch.mock.calls;
|
|
204
|
+
|
|
205
|
+
const payload = result.payload as BookData;
|
|
206
|
+
expect(payload.id).to.equal(
|
|
207
|
+
"urn:uuid:1cca9468-c447-4303-bc5a-c57470b85cb1"
|
|
208
|
+
);
|
|
209
|
+
expect(payload.title).to.equal(
|
|
210
|
+
"Tea-Cup Reading and Fortune-Telling by Tea Leaves"
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
expect(dispatchCalls.length).to.equal(2);
|
|
214
|
+
expect(dispatchCalls[0][0].type).to.equal(getBookData.pending.type);
|
|
215
|
+
expect(dispatchCalls[0][0].payload).to.equal(undefined);
|
|
216
|
+
expect(dispatchCalls[0][0].meta.arg).to.deep.equal({
|
|
217
|
+
url: goodBookUrl,
|
|
218
|
+
});
|
|
219
|
+
expect(dispatchCalls[1][0].type).to.equal(getBookData.fulfilled.type);
|
|
220
|
+
expect(dispatchCalls[1][0].payload).to.deep.equal(payload);
|
|
221
|
+
expect(dispatchCalls[1][0].meta.arg).to.deep.equal({
|
|
222
|
+
url: goodBookUrl,
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
it("should return an error, if the data is malformed", async () => {
|
|
226
|
+
const action = getBookData({ url: brokenBookUrl });
|
|
227
|
+
|
|
228
|
+
const result = await action(dispatch, getState, undefined);
|
|
229
|
+
const dispatchCalls = dispatch.mock.calls;
|
|
230
|
+
|
|
231
|
+
const payload = result.payload as RequestError;
|
|
232
|
+
expect(payload.response).to.equal(FETCH_OPDS_PARSE_ERROR_MESSAGE);
|
|
233
|
+
expect(payload.url).to.equal(brokenBookUrl);
|
|
234
|
+
|
|
235
|
+
expect(dispatchCalls.length).to.equal(2);
|
|
236
|
+
expect(dispatchCalls[0][0].type).to.equal(getBookData.pending.type);
|
|
237
|
+
expect(dispatchCalls[0][0].payload).to.equal(undefined);
|
|
238
|
+
expect(dispatchCalls[0][0].meta.arg).to.deep.equal({
|
|
239
|
+
url: brokenBookUrl,
|
|
240
|
+
});
|
|
241
|
+
expect(dispatchCalls[1][0].type).to.equal(getBookData.rejected.type);
|
|
242
|
+
expect(dispatchCalls[1][0].payload).to.deep.equal(payload);
|
|
243
|
+
expect(dispatchCalls[1][0].meta.arg).to.deep.equal({
|
|
244
|
+
url: brokenBookUrl,
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
it("should return an error, if the HTTP request fails", async () => {
|
|
248
|
+
const action = getBookData({ url: errorBookUrl });
|
|
249
|
+
|
|
250
|
+
const result = await action(dispatch, getState, undefined);
|
|
251
|
+
const dispatchCalls = dispatch.mock.calls;
|
|
252
|
+
|
|
253
|
+
const payload = result.payload as RequestError;
|
|
254
|
+
|
|
255
|
+
expect(result.type).to.equal(getBookData.rejected.type);
|
|
256
|
+
expect(result.meta.arg).to.deep.equal({ url: errorBookUrl });
|
|
257
|
+
expect(payload.response).to.equal("Internal server error");
|
|
258
|
+
expect(payload.url).to.equal(errorBookUrl);
|
|
259
|
+
|
|
260
|
+
expect(dispatchCalls.length).to.equal(2);
|
|
261
|
+
expect(dispatchCalls[0][0].type).to.equal(getBookData.pending.type);
|
|
262
|
+
expect(dispatchCalls[0][0].payload).to.equal(undefined);
|
|
263
|
+
expect(dispatchCalls[0][0].meta.arg).to.deep.equal({
|
|
264
|
+
url: errorBookUrl,
|
|
265
|
+
});
|
|
266
|
+
expect(dispatchCalls[1][0].type).to.equal(getBookData.rejected.type);
|
|
267
|
+
expect(dispatchCalls[1][0].payload).to.deep.equal(payload);
|
|
268
|
+
expect(dispatchCalls[1][0].meta.arg).to.deep.equal({
|
|
269
|
+
url: errorBookUrl,
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
describe("submitBookData...", () => {
|
|
275
|
+
const goodBookUrl = "https://example.com/book";
|
|
276
|
+
const editBookUrl = `${goodBookUrl}/edit`;
|
|
277
|
+
const brokenBookUrl = "https://example.com/broken-book";
|
|
278
|
+
const errorBookUrl = "https://example.com/error-book";
|
|
279
|
+
const csrfTokenHeader = "X-CSRF-Token";
|
|
280
|
+
const validCsrfToken = "valid-csrf-token";
|
|
281
|
+
|
|
282
|
+
const badCsrfTokenResponseBody = {
|
|
283
|
+
type: "http://librarysimplified.org/terms/problem/invalid-csrf-token",
|
|
284
|
+
title: "Invalid CSRF token",
|
|
285
|
+
status: 400,
|
|
286
|
+
detail: "There was an error saving your changes.",
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const dispatch = jest.fn();
|
|
290
|
+
const getState = jest.fn().mockReturnValue({
|
|
291
|
+
bookEditor: initialState,
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
beforeAll(() => {
|
|
295
|
+
fetchMock
|
|
296
|
+
.post(
|
|
297
|
+
{
|
|
298
|
+
name: "valid-csrf-token-post",
|
|
299
|
+
url: editBookUrl,
|
|
300
|
+
headers: { [csrfTokenHeader]: validCsrfToken },
|
|
301
|
+
},
|
|
302
|
+
{ body: "Success!", status: 201 }
|
|
303
|
+
)
|
|
304
|
+
.post(
|
|
305
|
+
{ name: "invalid-csrf-token-post", url: editBookUrl },
|
|
306
|
+
{ body: badCsrfTokenResponseBody, status: 400 }
|
|
307
|
+
)
|
|
308
|
+
.get(goodBookUrl, { body: SAMPLE_BOOK_ADMIN_DETAIL, status: 200 });
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
afterEach(() => {
|
|
312
|
+
fetchMock.resetHistory();
|
|
313
|
+
dispatch.mockClear();
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
afterEach(fetchMock.resetHistory);
|
|
317
|
+
afterAll(() => fetchMock.restore());
|
|
318
|
+
|
|
319
|
+
it("should post the book data on the happy path", async () => {
|
|
320
|
+
const csrfToken = validCsrfToken;
|
|
321
|
+
const formData = new FormData();
|
|
322
|
+
formData.append("id", "urn:something:something");
|
|
323
|
+
formData.append("title", "title");
|
|
324
|
+
|
|
325
|
+
const action = submitBookData({
|
|
326
|
+
url: editBookUrl,
|
|
327
|
+
data: formData,
|
|
328
|
+
csrfToken,
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
const result = await action(dispatch, getState, undefined);
|
|
332
|
+
const dispatchCalls = dispatch.mock.calls;
|
|
333
|
+
const fetchCalls = fetchMock.calls();
|
|
334
|
+
|
|
335
|
+
expect(fetchCalls.length).to.equal(1);
|
|
336
|
+
expect(fetchCalls[0].identifier).to.equal("valid-csrf-token-post");
|
|
337
|
+
expect(
|
|
338
|
+
(fetchCalls[0][1].headers as Headers).get(csrfTokenHeader)
|
|
339
|
+
).to.equal(validCsrfToken);
|
|
340
|
+
|
|
341
|
+
expect(fetchCalls[0][0]).to.equal(editBookUrl);
|
|
342
|
+
expect(fetchCalls[0][1].method).to.equal("POST");
|
|
343
|
+
expect(fetchCalls[0][1].body).to.equal(formData);
|
|
344
|
+
|
|
345
|
+
expect(dispatchCalls.length).to.equal(3);
|
|
346
|
+
expect(dispatchCalls[0][0].type).to.equal(submitBookData.pending.type);
|
|
347
|
+
expect(dispatchCalls[0][0].payload).to.equal(undefined);
|
|
348
|
+
// On a successful update, the second dispatch is to re-fetch the updated book data.
|
|
349
|
+
// The third dispatch is for the fulfilled action.
|
|
350
|
+
expect(dispatchCalls[2][0].type).to.equal(
|
|
351
|
+
submitBookData.fulfilled.type
|
|
352
|
+
);
|
|
353
|
+
expect(dispatchCalls[2][0].payload.body.toString()).to.equal(
|
|
354
|
+
"Success!"
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
it("should fail, if the user is unauthorized", async () => {
|
|
358
|
+
const csrfToken = "invalid-token";
|
|
359
|
+
const formData = new FormData();
|
|
360
|
+
formData.append("id", "urn:something:something");
|
|
361
|
+
formData.append("title", "title");
|
|
362
|
+
|
|
363
|
+
const action = submitBookData({
|
|
364
|
+
url: editBookUrl,
|
|
365
|
+
data: formData,
|
|
366
|
+
csrfToken,
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
const result = await action(dispatch, getState, undefined);
|
|
370
|
+
const dispatchCalls = dispatch.mock.calls;
|
|
371
|
+
const fetchCalls = fetchMock.calls();
|
|
372
|
+
|
|
373
|
+
expect(fetchCalls.length).to.equal(1);
|
|
374
|
+
expect(fetchCalls[0].identifier).to.equal("invalid-csrf-token-post");
|
|
375
|
+
expect(
|
|
376
|
+
(fetchCalls[0][1].headers as Headers).get(csrfTokenHeader)
|
|
377
|
+
).not.to.equal(validCsrfToken);
|
|
378
|
+
|
|
379
|
+
expect(fetchCalls[0][0]).to.equal(editBookUrl);
|
|
380
|
+
expect(fetchCalls[0][1].method).to.equal("POST");
|
|
381
|
+
expect(fetchCalls[0][1].body).to.equal(formData);
|
|
382
|
+
|
|
383
|
+
expect(dispatchCalls.length).to.equal(2);
|
|
384
|
+
expect(dispatchCalls[0][0].type).to.equal(submitBookData.pending.type);
|
|
385
|
+
expect(dispatchCalls[0][0].payload).to.equal(undefined);
|
|
386
|
+
// There is no re-fetch on a failed request, ...
|
|
387
|
+
// ...so the second dispatch is for the rejected action.
|
|
388
|
+
expect(dispatchCalls[1][0].type).to.equal(submitBookData.rejected.type);
|
|
389
|
+
expect(dispatchCalls[1][0].payload.status).to.equal(400);
|
|
390
|
+
expect(dispatchCalls[1][0].payload.response).to.equal(
|
|
391
|
+
"There was an error saving your changes."
|
|
392
|
+
);
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
});
|
|
@@ -4,6 +4,7 @@ import ContextProvider, {
|
|
|
4
4
|
} from "../../../src/components/ContextProvider";
|
|
5
5
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
6
6
|
import { render, RenderOptions, RenderResult } from "@testing-library/react";
|
|
7
|
+
import { defaultFeatureFlags } from "../../../src/utils/featureFlags";
|
|
7
8
|
|
|
8
9
|
export type TestProviderWrapperOptions = {
|
|
9
10
|
contextProviderProps?: Partial<ContextProviderProps>;
|
|
@@ -15,7 +16,10 @@ export type TestRenderWrapperOptions = TestProviderWrapperOptions & {
|
|
|
15
16
|
|
|
16
17
|
// The `csrfToken` context provider prop is required, so we provide
|
|
17
18
|
// a default value here, so it can be easily merged with other props.
|
|
18
|
-
const defaultContextProviderProps = {
|
|
19
|
+
const defaultContextProviderProps: ContextProviderProps = {
|
|
20
|
+
csrfToken: "",
|
|
21
|
+
featureFlags: defaultFeatureFlags,
|
|
22
|
+
};
|
|
19
23
|
|
|
20
24
|
/**
|
|
21
25
|
* Returns a component, composed with our providers, that can be used to wrap
|
|
@@ -27,7 +31,10 @@ const defaultContextProviderProps = { csrfToken: "" };
|
|
|
27
31
|
* @returns {React.FunctionComponent} A React component that wraps children with our providers
|
|
28
32
|
*/
|
|
29
33
|
export const componentWithProviders = ({
|
|
30
|
-
contextProviderProps = {
|
|
34
|
+
contextProviderProps = {
|
|
35
|
+
csrfToken: "",
|
|
36
|
+
featureFlags: defaultFeatureFlags,
|
|
37
|
+
},
|
|
31
38
|
queryClient = new QueryClient(),
|
|
32
39
|
}: TestProviderWrapperOptions): React.FunctionComponent => {
|
|
33
40
|
const effectiveContextProviderProps = {
|