Skip to content

Commit

Permalink
Move book editor state to redux toolkit slices model. (PP-1350) (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdilauro authored Jun 28, 2024
1 parent e05491f commit 6accec3
Show file tree
Hide file tree
Showing 24 changed files with 826 additions and 530 deletions.
57 changes: 12 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
"react-redux": "^7.2.9",
"react-router": "^3.2.0",
"recharts": "^1.8.6",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0",
"redux": "^4.2.1",
"redux-thunk": "^2.4.2",
"request": "^2.85.0",
"stream-browserify": "^3.0.0",
"timers-browserify": "^2.0.12",
Expand Down Expand Up @@ -100,7 +100,7 @@
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^4.0.0",
"fetch-mock": "^7.3.1",
"fetch-mock": "^10.0.7",
"fetch-mock-jest": "^1.5.1",
"fetch-ponyfill": "^7.1.0",
"file-loader": "^6.2.0",
Expand Down
94 changes: 30 additions & 64 deletions src/__tests__/actions-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { stub } from "sinon";
const fetchMock = require("fetch-mock");

import ActionCreator from "../actions";
import { getBookData } from "../features/book/bookEditorSlice";

class MockDataFetcher {
resolve: boolean = true;
Expand Down Expand Up @@ -53,10 +54,11 @@ describe("actions", () => {
it("dispatches request, success, and load", async () => {
const dispatch = stub();
const responseText = "response";
fetchMock.mock(url, responseText);
const fetchArgs = fetchMock.calls();
fetchMock.post(url, responseText);

await actions.postForm(type, url, formData)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${type}_${ActionCreator.REQUEST}`
Expand All @@ -82,9 +84,10 @@ describe("actions", () => {
// prettier-ignore
const responseText = "{\"id\": \"test\", \"name\": \"test\"}";
fetchMock.mock(url, responseText);
const fetchArgs = fetchMock.calls();

await actions.postForm(type, url, formData, "POST", "", "JSON")(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${type}_${ActionCreator.REQUEST}`
Expand All @@ -109,9 +112,10 @@ describe("actions", () => {
const dispatch = stub();
const responseText = "response";
fetchMock.mock(url, responseText);
const fetchArgs = fetchMock.calls();

await actions.postForm(type, url, formData, "DELETE")(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(fetchMock.called()).to.equal(true);
expect(fetchArgs[0][0]).to.equal(url);
Expand Down Expand Up @@ -214,9 +218,10 @@ describe("actions", () => {
it("dispatches request and success", async () => {
const dispatch = stub();
fetchMock.mock(url, 200);
const fetchArgs = fetchMock.calls();

await actions.postJSON<{ test: number }>(type, url, jsonData)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(2);
expect(dispatch.args[0][0].type).to.equal(
`${type}_${ActionCreator.REQUEST}`
Expand Down Expand Up @@ -288,55 +293,6 @@ describe("actions", () => {
});
});

describe("fetchBookAdmin", () => {
it("dispatches request, load, and success", async () => {
const dispatch = stub();
const bookData = {
title: "test title",
};
fetcher.testData = bookData;
fetcher.resolve = true;

const data = await actions.fetchBookAdmin("http://example.com/book")(
dispatch
);
expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
ActionCreator.BOOK_ADMIN_REQUEST
);
expect(dispatch.args[1][0].type).to.equal(
ActionCreator.BOOK_ADMIN_SUCCESS
);
expect(dispatch.args[2][0].type).to.equal(ActionCreator.BOOK_ADMIN_LOAD);
expect(data).to.deep.equal(bookData);
});
});

describe("editBook", () => {
it("dispatches request and success", async () => {
const editBookUrl = "http://example.com/editBook";
const dispatch = stub();
const formData = new (window as any).FormData();
formData.append("title", "title");

fetchMock.mock(editBookUrl, "done");
const fetchArgs = fetchMock.calls();

await actions.editBook(editBookUrl, formData)(dispatch);
expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
ActionCreator.EDIT_BOOK_REQUEST
);
expect(dispatch.args[1][0].type).to.equal(
ActionCreator.EDIT_BOOK_SUCCESS
);
expect(fetchMock.called()).to.equal(true);
expect(fetchArgs[0][0]).to.equal(editBookUrl);
expect(fetchArgs[0][1].method).to.equal("POST");
expect(fetchArgs[0][1].body).to.equal(formData);
});
});

describe("fetchComplaints", () => {
it("dispatches request, load, and success", async () => {
const dispatch = stub();
Expand Down Expand Up @@ -378,9 +334,10 @@ describe("actions", () => {
};

fetchMock.mock(postComplaintUrl, 201);
const fetchArgs = fetchMock.calls();

await actions.postComplaint(postComplaintUrl, data)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(2);
expect(dispatch.args[0][0].type).to.equal(
ActionCreator.POST_COMPLAINT_REQUEST
Expand All @@ -403,9 +360,10 @@ describe("actions", () => {
formData.append("type", "test type");

fetchMock.mock(resolveComplaintsUrl, "server response");
const fetchArgs = fetchMock.calls();

await actions.resolveComplaints(resolveComplaintsUrl, formData)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
ActionCreator.RESOLVE_COMPLAINTS_REQUEST
Expand Down Expand Up @@ -458,12 +416,13 @@ describe("actions", () => {
newGenreTree.forEach((genre) => formData.append("genres", genre));

fetchMock.mock(editClassificationsUrl, "server response");
const fetchArgs = fetchMock.calls();

await actions.editClassifications(
editClassificationsUrl,
formData
)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
ActionCreator.EDIT_CLASSIFICATIONS_REQUEST
Expand Down Expand Up @@ -631,9 +590,10 @@ describe("actions", () => {
formData.append("name", "new name");

fetchMock.mock(editLibraryUrl, "server response");
const fetchArgs = fetchMock.calls();

await actions.editLibrary(formData)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${ActionCreator.EDIT_LIBRARY}_${ActionCreator.REQUEST}`
Expand Down Expand Up @@ -685,9 +645,10 @@ describe("actions", () => {
formData.append("name", "new name");

fetchMock.mock(editCollectionUrl, "server response");
const fetchArgs = fetchMock.calls();

await actions.editCollection(formData)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${ActionCreator.EDIT_COLLECTION}_${ActionCreator.REQUEST}`
Expand Down Expand Up @@ -739,9 +700,10 @@ describe("actions", () => {
formData.append("email", "email");

fetchMock.mock(editIndividualAdminUrl, "server response");
const fetchArgs = fetchMock.calls();

await actions.editIndividualAdmin(formData)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${ActionCreator.EDIT_INDIVIDUAL_ADMIN}_${ActionCreator.REQUEST}`
Expand Down Expand Up @@ -795,9 +757,10 @@ describe("actions", () => {
const dispatch = stub();

fetchMock.mock(collectionSelfTestURL, "server response");
const fetchArgs = fetchMock.calls();

await actions.runSelfTests(collectionSelfTestURL)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${ActionCreator.RUN_SELF_TESTS}_${ActionCreator.REQUEST}`
Expand All @@ -820,9 +783,10 @@ describe("actions", () => {
const response = "{\"id\": \"test\", \"name\": \"test\"}";

fetchMock.mock("/nypl/admin/manage_patrons", response);
const fetchArgs = fetchMock.calls();

const data = await actions.patronLookup(formData, "nypl")(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${ActionCreator.PATRON_LOOKUP}_${ActionCreator.REQUEST}`
Expand Down Expand Up @@ -850,9 +814,10 @@ describe("actions", () => {
"/nypl/admin/manage_patrons/reset_adobe_id",
"server response"
);
const fetchArgs = fetchMock.calls();

const data = await actions.resetAdobeId(formData, "nypl")(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${ActionCreator.RESET_ADOBE_ID}_${ActionCreator.REQUEST}`
Expand Down Expand Up @@ -967,9 +932,10 @@ describe("actions", () => {
formData.append("announcements", "[]");

fetchMock.mock(editSitewideAnnouncementsUrl, "server response");
const fetchArgs = fetchMock.calls();

await actions.editSitewideAnnouncements(formData)(dispatch);
const fetchArgs = fetchMock.calls();

expect(dispatch.callCount).to.equal(3);
expect(dispatch.args[0][0].type).to.equal(
`${ActionCreator.EDIT_SITEWIDE_ANNOUNCEMENTS}_${ActionCreator.REQUEST}`
Expand Down
17 changes: 0 additions & 17 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
AdvancedSearchQuery,
BookData,
ComplaintsData,
GenreTree,
ClassificationData,
Expand Down Expand Up @@ -131,14 +130,6 @@ export default class ActionCreator extends BaseActionCreator {
static readonly RESET_LANES = "RESET_LANES";
static readonly CHANGE_LANE_ORDER = "CHANGE_LANE_ORDER";

static readonly EDIT_BOOK_REQUEST = "EDIT_BOOK_REQUEST";
static readonly EDIT_BOOK_SUCCESS = "EDIT_BOOK_SUCCESS";
static readonly EDIT_BOOK_FAILURE = "EDIT_BOOK_FAILURE";
static readonly BOOK_ADMIN_REQUEST = "BOOK_ADMIN_REQUEST";
static readonly BOOK_ADMIN_SUCCESS = "BOOK_ADMIN_SUCCESS";
static readonly BOOK_ADMIN_FAILURE = "BOOK_ADMIN_FAILURE";
static readonly BOOK_ADMIN_LOAD = "BOOK_ADMIN_LOAD";

static readonly COMPLAINTS_REQUEST = "COMPLAINTS_REQUEST";
static readonly COMPLAINTS_SUCCESS = "COMPLAINTS_SUCCESS";
static readonly COMPLAINTS_FAILURE = "COMPLAINTS_FAILURE";
Expand Down Expand Up @@ -329,14 +320,6 @@ export default class ActionCreator extends BaseActionCreator {
};
}

fetchBookAdmin(url: string) {
return this.fetchOPDS<BookData>(ActionCreator.BOOK_ADMIN, url).bind(this);
}

editBook(url: string, data: FormData | null) {
return this.postForm(ActionCreator.EDIT_BOOK, url, data).bind(this);
}

fetchRoles() {
const url = "/admin/roles";
return this.fetchJSON<RolesData>(ActionCreator.ROLES, url).bind(this);
Expand Down
Loading

0 comments on commit 6accec3

Please sign in to comment.