From d35af445c1dbb0981b0d43c5bc7b7348ac7e4668 Mon Sep 17 00:00:00 2001 From: GitHub -- tdilauro Date: Tue, 25 Jun 2024 20:51:00 -0400 Subject: [PATCH] Clean up tests. --- .../__tests__/BookDetailsEditor-test.tsx | 20 +-- src/features/book/bookEditorSlice.ts | 10 +- src/reducers/__tests__/book-test.ts | 119 ------------------ tests/jest/features/book.test.ts | 36 ++++++ 4 files changed, 48 insertions(+), 137 deletions(-) delete mode 100644 src/reducers/__tests__/book-test.ts diff --git a/src/components/__tests__/BookDetailsEditor-test.tsx b/src/components/__tests__/BookDetailsEditor-test.tsx index be142495c..7a2e920e1 100644 --- a/src/components/__tests__/BookDetailsEditor-test.tsx +++ b/src/components/__tests__/BookDetailsEditor-test.tsx @@ -10,25 +10,25 @@ import BookEditForm from "../BookEditForm"; import ErrorMessage from "../ErrorMessage"; describe("BookDetailsEditor", () => { - let fetchBook; + let fetchBookData; let fetchRoles; let fetchMedia; let fetchLanguages; - let editBook; + let postBookData; let dispatchProps; beforeEach(() => { - fetchBook = stub(); + fetchBookData = stub(); fetchRoles = stub(); fetchMedia = stub(); fetchLanguages = stub(); - editBook = stub(); + postBookData = stub(); dispatchProps = { - fetchBook, + fetchBookData, fetchRoles, fetchMedia, fetchLanguages, - editBook, + postBookData, }; }); @@ -42,8 +42,8 @@ describe("BookDetailsEditor", () => { /> ); - expect(fetchBook.callCount).to.equal(1); - expect(fetchBook.args[0][0]).to.equal("admin/works/1234"); + expect(fetchBookData.callCount).to.equal(1); + expect(fetchBookData.args[0][0]).to.equal("admin/works/1234"); expect(fetchRoles.callCount).to.equal(1); expect(fetchMedia.callCount).to.equal(1); expect(fetchLanguages.callCount).to.equal(1); @@ -63,8 +63,8 @@ describe("BookDetailsEditor", () => { wrapper.setProps({ bookUrl: newPermalink }); wrapper.update(); - expect(fetchBook.callCount).to.equal(2); - expect(fetchBook.args[1][0]).to.equal("admin/works/5555"); + expect(fetchBookData.callCount).to.equal(2); + expect(fetchBookData.args[1][0]).to.equal("admin/works/5555"); }); it("shows title", () => { diff --git a/src/features/book/bookEditorSlice.ts b/src/features/book/bookEditorSlice.ts index 5f237ed5c..a4b10cf2d 100644 --- a/src/features/book/bookEditorSlice.ts +++ b/src/features/book/bookEditorSlice.ts @@ -76,15 +76,9 @@ const bookEditorSlice = createSlice({ state.editError = null; }) .addCase(submitBookData.rejected, (state, action) => { - state.isFetching = true; + state.isFetching = false; state.editError = action.payload as RequestError; - }) - .addMatcher( - (action) => true, - (state, action) => { - // console.log("Unhandled action", action.type, {action, state}); - } - ); + }); }, }); diff --git a/src/reducers/__tests__/book-test.ts b/src/reducers/__tests__/book-test.ts deleted file mode 100644 index bff2e3f67..000000000 --- a/src/reducers/__tests__/book-test.ts +++ /dev/null @@ -1,119 +0,0 @@ -import { expect } from "chai"; - -// import book from "../book"; -import book from "../../features/book/bookEditorSlice"; -import ActionCreator from "../../actions"; -import bookEditorSlice, { - getBookData, -} from "../../features/book/bookEditorSlice"; - -describe("book reducer", () => { - const initState = { - url: null, - data: null, - isFetching: false, - fetchError: null, - editError: null, - }; - - const fetchedState = { - url: "test url", - data: { - id: "id", - title: "test book", - }, - isFetching: false, - fetchError: null, - editError: null, - }; - - // it("returns initial state for unrecognized action", () => { - // expect(book(undefined, {})).to.deep.equal(initState); - // }); - - // TODO: test clearBook - it("handles CLEAR_BOOK", () => { - const action = { type: ActionCreator.BOOK_CLEAR }; - console.log("result", book(fetchedState, action)); - expect(book(fetchedState, action)).to.deep.equal(initState); - }); - - // it("handles BOOK_ADMIN_REQUEST", () => { - // const action = { type: ActionCreator.BOOK_ADMIN_REQUEST, url: "test url" }; - // const oldState = Object.assign({}, initState, { editError: "error" }); - // const newState = Object.assign({}, initState, { - // url: "test url", - // isFetching: true, - // editError: null, - // }); - // expect(book(initState, action)).to.deep.equal(newState); - // }); - - // TODO: test editBook - // it("handles EDIT_BOOK_REQUEST", () => { - // const action = { type: ActionCreator.EDIT_BOOK_REQUEST }; - // const newState = Object.assign({}, fetchedState, { - // isFetching: true, - // }); - // expect(book(fetchedState, action)).to.deep.equal(newState); - // }); - - // it("handles BOOK_ADMIN_FAILURE", () => { - // const action = { - // type: ActionCreator.BOOK_ADMIN_FAILURE, - // error: "test error", - // }; - // const oldState = { - // url: "test url", - // data: null, - // isFetching: true, - // fetchError: null, - // editError: null, - // }; - // const newState = Object.assign({}, oldState, { - // fetchError: "test error", - // isFetching: false, - // }); - // expect(book(oldState, action)).to.deep.equal(newState); - // }); - - // TODO: test editBook - // it("handles EDIT_BOOK_FAILURE", () => { - // const action = { - // type: ActionCreator.EDIT_BOOK_FAILURE, - // error: "test error", - // }; - // const oldState = { - // url: "test url", - // data: null, - // isFetching: true, - // fetchError: null, - // editError: null, - // }; - // const newState = Object.assign({}, oldState, { - // editError: "test error", - // isFetching: false, - // }); - // expect(book(oldState, action)).to.deep.equal(newState); - // }); - - // it("handles BOOK_ADMIN_LOAD", () => { - // const action = { - // type: ActionCreator.BOOK_ADMIN_LOAD, - // data: "test data", - // url: "test url", - // }; - // const oldState = { - // url: "test url", - // data: null, - // isFetching: true, - // fetchError: null, - // editError: null, - // }; - // const newState = Object.assign({}, oldState, { - // data: "test data", - // isFetching: false, - // }); - // expect(book(oldState, action)).to.deep.equal(newState); - // }); -}); diff --git a/tests/jest/features/book.test.ts b/tests/jest/features/book.test.ts index 1129b61d2..311b52bfc 100644 --- a/tests/jest/features/book.test.ts +++ b/tests/jest/features/book.test.ts @@ -130,6 +130,42 @@ describe("Redux bookEditorSlice...", () => { expect(state.fetchError).to.deep.equal(errorObject); expect(state.editError).to.be.null; }); + + it("should handle submitBookData.pending", () => { + const action = { type: submitBookData.pending.type }; + const previousState = { ...fetchedState, isFetching: false }; + const state = reducer(previousState, action); + + expect(state).to.deep.equal({ ...fetchedState, isFetching: true }); + }); + it("should handle submitBookData.fulfilled", () => { + const action = { + type: submitBookData.fulfilled.type, + payload: "some value", + }; + const previousState = { ...fetchedState, isFetching: true }; + const state = reducer(previousState, action); + + expect(state).to.deep.equal({ + ...fetchedState, + isFetching: false, + editError: null, + }); + }); + it("should handle submitBookData.rejected", () => { + const action = { + type: submitBookData.rejected.type, + payload: "some value", + }; + const previousState = { ...fetchedState, isFetching: true }; + const state = reducer(previousState, action); + + expect(state).to.deep.equal({ + ...fetchedState, + isFetching: false, + editError: "some value", + }); + }); }); describe("thunks...", () => {