UNPKG

opds-web-client

Version:
432 lines (431 loc) 20.8 kB
"use strict"; jest.dontMock("../mergeRootProps"); jest.dontMock("./collectionData"); // synchronous actions for simple testing // of createFetchCollectionAndBook var MockActionCreator = (function () { function MockActionCreator() { } MockActionCreator.prototype.fetchCollection = function (url) { return "fetchCollection:" + url; }; MockActionCreator.prototype.fetchBook = function (url) { return "fetchBook:" + url; }; return MockActionCreator; }()); jest.setMock("../../actions", { default: MockActionCreator }); var mergeRootProps_1 = require("../mergeRootProps"); var collectionData_1 = require("./collectionData"); describe("findBookInCollection", function () { it("returns nothing if no collection", function () { var result = mergeRootProps_1.findBookInCollection(null, "test"); expect(result).toBe(null); }); it("finds a book in the collection by url", function () { var collection = collectionData_1.groupedCollectionData; var book = collectionData_1.groupedCollectionData.lanes[0].books[0]; var result = mergeRootProps_1.findBookInCollection(collection, book.url); expect(result).toEqual(book); }); it("finds a book in the collection by id", function () { var collection = collectionData_1.groupedCollectionData; var book = collectionData_1.groupedCollectionData.lanes[0].books[0]; var result = mergeRootProps_1.findBookInCollection(collection, book.id); expect(result).toEqual(book); }); it("returns nothing if given a book url/id not in the collection", function () { var collection = collectionData_1.groupedCollectionData; var result = mergeRootProps_1.findBookInCollection(collection, "nonexistent"); expect(result).toBeFalsy(); }); }); describe("createFetchCollectionAndBook", function () { var collectionUrl = "collection url"; var bookUrl = "book url"; var dispatch = jest.genMockFunction(); dispatch.mockImplementation(function (url) { return new Promise(function (resolve, reject) { return resolve(url); }); }); it("returns fetch function that uses the provided dispatch", function (done) { var actions = new MockActionCreator(); var fetchCollectionAndBook = mergeRootProps_1.createFetchCollectionAndBook(dispatch); fetchCollectionAndBook(collectionUrl, bookUrl).then(function (_a) { var collectionData = _a.collectionData, bookData = _a.bookData; // we are only testing that the provided dispatch is called twice, // once for fetchCollection and once for fetchBook expect(dispatch.mock.calls.length).toBe(2); expect(dispatch.mock.calls[0][0]).toBe(actions.fetchCollection(collectionUrl)); expect(dispatch.mock.calls[1][0]).toBe(actions.fetchBook(bookUrl)); done(); }).catch(function (err) { return done(err); }); }); }); describe("mergeRootProps", function () { var stateProps, dispatchProps, componentProps; var fetchCollection, clearCollection, fetchBook, loadBook, clearBook, navigate, borrowBook, fetchLoans; var fakeCollection = collectionData_1.ungroupedCollectionData; var fakeBook = { id: "fake book id", title: "fake book title", url: "fake book url" }; beforeEach(function () { fetchCollection = jest.genMockFunction().mockImplementation(function (url) { return new Promise(function (resolve, reject) { resolve(fakeCollection); }); }); fetchBook = jest.genMockFunction().mockImplementation(function (url) { return new Promise(function (resolve, reject) { resolve(fakeBook); }); }); loadBook = jest.genMockFunction(); clearCollection = jest.genMockFunction(); clearBook = jest.genMockFunction(); borrowBook = jest.genMockFunction().mockImplementation(function (url) { return new Promise(function (resolve, reject) { return resolve(fakeBook); }); }); fetchLoans = jest.genMockFunction(); dispatchProps = { createDispatchProps: function (fetcher) { return { fetchCollection: fetchCollection, clearCollection: clearCollection, loadBook: loadBook, fetchBook: fetchBook, clearBook: clearBook, borrowBook: borrowBook, fetchLoans: fetchLoans }; } }; componentProps = {}; }); describe("setCollection", function () { var props; beforeEach(function () { stateProps = { loadedCollectionUrl: "test url", collectionData: collectionData_1.groupedCollectionData, bookData: null }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); }); it("fetches collection data if given a collection url", function (done) { props.setCollection("new collection url").then(function (data) { expect(data).toBe(fakeCollection); expect(fetchCollection.mock.calls.length).toBe(1); expect(fetchCollection.mock.calls[0][0]).toBe("new collection url"); expect(clearCollection.mock.calls.length).toBe(0); done(); }).catch(function (err) { return done.fail(err); }); }); it("does nothing and returns existing data if given the existing collection url", function (done) { props.setCollection("test url").then(function (data) { expect(data).toBe(collectionData_1.groupedCollectionData); expect(fetchCollection.mock.calls.length).toBe(0); expect(clearCollection.mock.calls.length).toBe(0); done(); }).catch(function (err) { return done.fail(err); }); }); it("clears collection data if given a falsy collection url", function (done) { props.setCollection(null).then(function (data) { expect(data).toBeFalsy(); expect(fetchCollection.mock.calls.length).toBe(0); expect(clearCollection.mock.calls.length).toBe(1); done(); }).catch(function (err) { return done.fail(err); }); }); }); describe("setBook", function () { var props; beforeEach(function () { stateProps = { currentCollectionUrl: "test collection url", collectionData: collectionData_1.groupedCollectionData, currentBookUrl: "test book url", bookData: { id: "test book id", title: "test book title", url: "test book url" } }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); }); it("fetches book data if given a book url not in the current collection", function (done) { props.setBook("fake book url", collectionData_1.groupedCollectionData).then(function (data) { expect(data).toBe(fakeBook); expect(fetchBook.mock.calls.length).toBe(1); expect(fetchBook.mock.calls[0][0]).toBe("fake book url"); expect(clearBook.mock.calls.length).toBe(0); done(); }).catch(function (err) { return done.fail(err); }); }); it("doesn't fetch book data if given a book url in the current collection", function (done) { props.setBook(collectionData_1.groupedCollectionData.lanes[0].books[0].url, collectionData_1.groupedCollectionData).then(function (data) { expect(data).toBe(collectionData_1.groupedCollectionData.lanes[0].books[0]); expect(fetchBook.mock.calls.length).toBe(0); expect(clearBook.mock.calls.length).toBe(0); done(); }).catch(function (err) { return done.fail(err); }); }); it("does nothing and returns book data if given book data", function (done) { var bookDataWithoutUrl = { id: "test id", "title": "test title" }; props.setBook(bookDataWithoutUrl).then(function (data) { expect(data).toBe(bookDataWithoutUrl); expect(fetchBook.mock.calls.length).toBe(0); expect(clearBook.mock.calls.length).toBe(0); done(); }).catch(function (err) { return done.fail(err); }); }); it("tries to refresh book data if given the existing book url and no collection", function (done) { props.setBook("test book url").then(function (data) { expect(data).toBe(fakeBook); expect(fetchBook.mock.calls.length).toBe(1); expect(clearBook.mock.calls.length).toBe(0); done(); }).catch(function (err) { return done.fail(err); }); }); it("clears book data if given a falsy book url", function (done) { props.setBook(null).then(function (data) { expect(data).toBeFalsy(); expect(fetchBook.mock.calls.length).toBe(0); expect(clearBook.mock.calls.length).toBe(1); done(); }).catch(function (err) { return done.fail(err); }); }); }); describe("setCollectionAndBook", function () { var props; beforeEach(function () { stateProps = { loadedCollectionUrl: collectionData_1.groupedCollectionData.url, collectionData: collectionData_1.groupedCollectionData, bookData: collectionData_1.groupedCollectionData.lanes[0].books[0] }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); }); it("does not fetch book if given book url belonging to given collection", function (done) { props.setCollectionAndBook(fakeCollection.url, fakeCollection.books[0].url).then(function (data) { expect(data).toEqual({ collectionData: fakeCollection, bookData: fakeCollection.books[0] }); expect(fetchCollection.mock.calls.length).toBe(1); expect(fetchCollection.mock.calls[0][0]).toBe(fakeCollection.url); expect(fetchBook.mock.calls.length).toBe(0); done(); }).catch(function (err) { return done.fail(err); }); }); it("fetches book if given book url not belonging to given collection", function (done) { props.setCollectionAndBook(fakeCollection.url, "fake book url").then(function (data) { expect(data).toEqual({ collectionData: fakeCollection, bookData: fakeBook }); expect(fetchCollection.mock.calls.length).toBe(1); expect(fetchCollection.mock.calls[0][0]).toBe(fakeCollection.url); expect(fetchBook.mock.calls.length).toBe(1); expect(fetchBook.mock.calls[0][0]).toBe("fake book url"); done(); }).catch(function (err) { return done.fail(err); }); }); it("fetches book if not given a collection url", function (done) { props.setCollectionAndBook(null, "fake book url").then(function (data) { expect(data).toEqual({ collectionData: null, bookData: fakeBook }); expect(fetchCollection.mock.calls.length).toBe(0); expect(fetchBook.mock.calls.length).toBe(1); expect(fetchBook.mock.calls[0][0]).toBe("fake book url"); done(); }).catch(function (err) { return done.fail(err); }); }); it("does nothing and returns existing data if given the existing collection and book urls", function (done) { props.setCollectionAndBook(stateProps.loadedCollectionUrl, stateProps.bookData.url).then(function (data) { expect(data).toEqual({ collectionData: stateProps.collectionData, bookData: stateProps.bookData }); expect(fetchCollection.mock.calls.length).toBe(0); expect(fetchBook.mock.calls.length).toBe(0); expect(clearCollection.mock.calls.length).toBe(0); expect(clearBook.mock.calls.length).toBe(0); done(); }).catch(function (err) { return done.fail(err); }); }); it("clears collection and book data if given falsy urls", function (done) { props.setCollectionAndBook(null, null).then(function (data) { expect(data).toEqual({ collectionData: null, bookData: null }); expect(fetchCollection.mock.calls.length).toBe(0); expect(fetchBook.mock.calls.length).toBe(0); expect(clearCollection.mock.calls.length).toBe(1); expect(clearBook.mock.calls.length).toBe(1); done(); }).catch(function (err) { return done.fail(err); }); }); }); describe("refreshCollectionAndBook", function () { var props; it("calls fetchCollection", function () { stateProps = { loadedCollectionUrl: "test collection", loadedBookUrl: "test book" }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.refreshCollectionAndBook(); expect(fetchCollection.mock.calls.length).toBe(1); expect(fetchCollection.mock.calls[0][0]).toBe("test collection"); }); it("calls fetchBook", function (done) { stateProps = { loadedCollectionUrl: "test collection", loadedBookUrl: "test book" }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.refreshCollectionAndBook().then(function (data) { expect(fetchBook.mock.calls.length).toBe(1); expect(fetchBook.mock.calls[0][0]).toBe("test book"); done(); }); }); it("only fetches collection if only collection is loaded", function () { stateProps = { loadedCollectionUrl: "test collection", loadedBookUrl: null }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.refreshCollectionAndBook(); expect(fetchCollection.mock.calls.length).toBe(1); expect(fetchCollection.mock.calls[0][0]).toBe("test collection"); expect(fetchBook.mock.calls.length).toBe(0); }); it("only fetches book if only book is loaded", function (done) { stateProps = { loadedCollectionUrl: null, loadedBookUrl: "test book" }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.refreshCollectionAndBook().then(function (data) { expect(fetchBook.mock.calls.length).toBe(1); expect(fetchBook.mock.calls[0][0]).toBe("test book"); expect(fetchCollection.mock.calls.length).toBe(0); done(); }); }); it("does not fetch if neither collection nor book are loaded", function (done) { stateProps = { loadedCollectionUrl: null, loadedBookUrl: null }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.refreshCollectionAndBook().then(function (data) { expect(fetchCollection.mock.calls.length).toBe(0); expect(fetchBook.mock.calls.length).toBe(0); done(); }); }); }); describe("retryCollectionAndBook", function () { var props; it("calls fetchCollection", function () { stateProps = { collectionUrl: "test collection", bookUrl: "test book", loadedCollectionUrl: null, loadedBookUrl: null }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.retryCollectionAndBook(); expect(fetchCollection.mock.calls.length).toBe(1); expect(fetchCollection.mock.calls[0][0]).toBe("test collection"); }); it("calls fetchBook", function (done) { stateProps = { collectionUrl: "test collection", bookUrl: "test book", loadedCollectionUrl: null, loadedBookUrl: null }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.retryCollectionAndBook().then(function (data) { expect(fetchBook.mock.calls.length).toBe(1); expect(fetchBook.mock.calls[0][0]).toBe("test book"); done(); }); }); it("only fetches collection if only collectionUrl is present", function () { stateProps = { collectionUrl: "test collection", bookUrl: null, loadedCollectionUrl: null, loadedBookUrl: null }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.retryCollectionAndBook(); expect(fetchCollection.mock.calls.length).toBe(1); expect(fetchCollection.mock.calls[0][0]).toBe("test collection"); expect(fetchBook.mock.calls.length).toBe(0); }); it("only fetches book if only book is loaded", function (done) { stateProps = { collectionUrl: null, bookUrl: "test book", loadedCollectionUrl: null, loadedBookUrl: null }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.retryCollectionAndBook().then(function (data) { expect(fetchBook.mock.calls.length).toBe(1); expect(fetchBook.mock.calls[0][0]).toBe("test book"); expect(fetchCollection.mock.calls.length).toBe(0); done(); }); }); it("does not fetch if neither collection nor book are loaded", function (done) { stateProps = { collectionUrl: null, bookUrl: null, loadedCollectionUrl: null, loadedBookUrl: null }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.retryCollectionAndBook().then(function (data) { expect(fetchCollection.mock.calls.length).toBe(0); expect(fetchBook.mock.calls.length).toBe(0); done(); }); }); }); describe("borrowBook", function () { var props; it("calls borrowBook", function () { stateProps = { loansUrl: "loans" }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.borrowBook("borrow url"); expect(borrowBook.mock.calls.length).toBe(1); expect(borrowBook.mock.calls[0][0]).toBe("borrow url"); }); it("calls fetchLoans if loansUrl is present", function (done) { stateProps = { loansUrl: "loans" }; props = mergeRootProps_1.mergeRootProps(stateProps, dispatchProps, componentProps); props.borrowBook().then(function (data) { expect(fetchLoans.mock.calls.length).toBe(1); expect(fetchLoans.mock.calls[0][0]).toBe("loans"); expect(data).toEqual(fakeBook); done(); }).catch(done.fail); }); it("doesn't call fetchLoans if loansUrl is blank", function (done) { props = mergeRootProps_1.mergeRootProps({}, dispatchProps, componentProps); props.borrowBook().then(function (data) { expect(fetchLoans.mock.calls.length).toBe(0); done(); }).catch(done.fail); }); }); });