UNPKG

opds-web-client

Version:
222 lines (221 loc) 7.23 kB
"use strict"; jest.dontMock("../collection"); jest.dontMock("../../actions"); jest.setMock("../history", { default: function (state, action) { return state.history; } }); var collection_1 = require("../collection"); var DataFetcher_1 = require("../../DataFetcher"); var actions_1 = require("../../actions"); var OPDSDataAdapter_1 = require("../../OPDSDataAdapter"); var fetcher = new DataFetcher_1.default({ adapter: OPDSDataAdapter_1.adapter }); var actions = new actions_1.default(fetcher); describe("collection reducer", function () { var initState = { url: null, data: null, isFetching: false, isFetchingPage: false, error: null, history: [] }; var currentState = { url: "some url", data: { id: "id", title: "title", url: "url", lanes: [], books: [], links: [], catalogRootLink: { url: "root url", text: "root title" }, parentLink: { url: "parent url", text: "parent title" } }, isFetching: false, isFetchingPage: false, error: null, history: [] }; var fetchingState = { url: "some url", data: null, isFetching: true, isFetchingPage: false, error: null, history: [] }; var fetchingPageState = { url: "some url", data: { id: "id", url: "some url", title: "some title", books: [], lanes: [], links: [] }, isFetching: false, isFetchingPage: true, error: null, history: [] }; var errorState = { url: null, data: null, isFetching: false, isFetchingPage: false, error: { status: 500, response: "test error", url: "some url" }, history: [] }; it("should return the initial state", function () { expect(collection_1.default(undefined, {})).toEqual(initState); }); it("should handle FETCH_COLLECTION_REQUEST", function () { var action = actions.fetchCollectionRequest("some other url"); var newState = Object.assign({}, errorState, { isFetching: true, error: null }); expect(collection_1.default(errorState, action)).toEqual(newState); }); it("should handle FETCH_COLLECTION_FAILURE", function () { var action = actions.fetchCollectionFailure({ status: 500, response: "test error", url: "error url" }); var newState = Object.assign({}, fetchingState, { isFetching: false, error: { status: 500, response: "test error", url: "error url" } }); expect(collection_1.default(fetchingState, action)).toEqual(newState); }); it("should handle LOAD_COLLECTION", function () { var data = { id: "some id", url: "some url", title: "some title", lanes: [], books: [], links: [] }; var action = actions.loadCollection(data, "some other url"); var newState = Object.assign({}, currentState, { url: "some other url", data: data, isFetching: false }); expect(collection_1.default(currentState, action)).toEqual(newState); }); it("should handle LOAD_COLLECTION after an error", function () { var data = { id: "some id", url: "some url", title: "some title", lanes: [], books: [], links: [] }; var action = actions.loadCollection(data, "some url"); var newState = Object.assign({}, errorState, { url: "some url", data: data, isFetching: false, error: null }); expect(collection_1.default(errorState, action)).toEqual(newState); }); it("should handle CLEAR_COLLECTION", function () { var action = actions.clearCollection(); var newState = Object.assign({}, currentState, { url: null, data: null }); expect(collection_1.default(currentState, action)).toEqual(newState); }); it("should handle FETCH_PAGE_REQUEST", function () { var action = actions.fetchPageRequest("some other url"); var newState = Object.assign({}, currentState, { pageUrl: "some other url", isFetchingPage: true }); expect(collection_1.default(currentState, action)).toEqual(newState); }); it("should handle FETCH_PAGE_FAILURE", function () { var action = actions.fetchPageFailure({ status: 500, response: "test error", url: "error url" }); var newState = Object.assign({}, fetchingPageState, { isFetchingPage: false, error: { status: 500, response: "test error", url: "error url" } }); expect(collection_1.default(fetchingPageState, action)).toEqual(newState); }); it("should handle LOAD_PAGE", function () { var data = { id: "some id", url: "test url", title: "some title", lanes: [], books: [{ id: "new book", title: "new title", authors: [], summary: "new summary", imageUrl: "", publisher: "" }], links: [], nextPageUrl: "next" }; var action = actions.loadPage(data); var newState = Object.assign({}, fetchingPageState, { data: Object.assign({}, fetchingPageState.data, { books: data.books, nextPageUrl: "next" }), isFetchingPage: false }); expect(collection_1.default(fetchingPageState, action)).toEqual(newState); }); it("should handle LOAD_SEARCH_DESCRIPTION", function () { var searchData = { description: "d", shortName: "s", template: function (s) { return s + " template"; } }; var action = actions.loadSearchDescription({ searchData: searchData }); var newState = collection_1.default(currentState, action); expect(newState.data.search).toBeTruthy(); expect(newState.data.search.searchData.description).toEqual("d"); expect(newState.data.search.searchData.shortName).toEqual("s"); expect(newState.data.search.searchData.template("test")).toEqual("test template"); }); it("should handle CLOSE_ERROR", function () { var action = actions.closeError(); var newState = Object.assign({}, errorState, { error: null }); expect(collection_1.default(errorState, action)).toEqual(newState); }); });