UNPKG

opds-web-client

Version:
246 lines (245 loc) 13.1 kB
"use strict"; var chai_1 = require("chai"); var sinon_1 = require("sinon"); var testData = { lanes: [], books: [{ id: "test id", url: "http://example.com/book", title: "test title" }] }; var DataFetcher_1 = require("../__mocks__/DataFetcher"); var fetcher = new DataFetcher_1.default(); fetcher.testData = testData; var actions_1 = require("../actions"); var actions = new actions_1.default(fetcher); describe("actions", function () { describe("fetchCollection", function () { var collectionUrl = "http://example.com/feed"; it("dispatches request, load, and success", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = true; actions.fetchCollection(collectionUrl)(dispatch).then(function (data) { chai_1.expect(dispatch.callCount).to.equal(3); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FETCH_COLLECTION_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FETCH_COLLECTION_SUCCESS); chai_1.expect(dispatch.args[2][0].type).to.equal(actions.LOAD_COLLECTION); chai_1.expect(data).to.equal(testData); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("dispatches failure", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = false; actions.fetchCollection(collectionUrl)(dispatch).catch(function (err) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FETCH_COLLECTION_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FETCH_COLLECTION_FAILURE); chai_1.expect(err).to.equal("test error"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); }); describe("fetchPage", function () { it("dispatches request, success, and load", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = true; actions.fetchPage("http://example.com/feed")(dispatch).then(function (data) { chai_1.expect(dispatch.callCount).to.equal(3); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FETCH_PAGE_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FETCH_PAGE_SUCCESS); chai_1.expect(dispatch.args[2][0].type).to.equal(actions.LOAD_PAGE); chai_1.expect(data).to.equal(testData); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("dispatches failure", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = false; actions.fetchPage("http://example.com/feed")(dispatch).catch(function (err) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FETCH_PAGE_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FETCH_PAGE_FAILURE); chai_1.expect(err).to.equal("test error"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); }); describe("fetchSearchDescription", function () { it("dispatches load", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = true; actions.fetchSearchDescription("http://example.com/search")(dispatch).then(function (data) { chai_1.expect(dispatch.callCount).to.equal(1); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.LOAD_SEARCH_DESCRIPTION); chai_1.expect(data).to.equal(testData); done(); }).catch(function (err) { console.log(err); throw (err); }); }); }); describe("fetchBook", function () { var bookUrl = "http://example.com/book"; it("dispatches request, load, and success", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = true; actions.fetchBook(bookUrl)(dispatch).then(function (data) { chai_1.expect(dispatch.callCount).to.equal(3); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FETCH_BOOK_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FETCH_BOOK_SUCCESS); chai_1.expect(dispatch.args[2][0].type).to.equal(actions.LOAD_BOOK); chai_1.expect(data).to.equal(testData); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("dispatches failure", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = false; actions.fetchBook(bookUrl)(dispatch).catch(function (err) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FETCH_BOOK_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FETCH_BOOK_FAILURE); chai_1.expect(err).to.equal("test error"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); }); describe("updateBook", function () { var borrowUrl = "http://example.com/book/borrow"; var fulfillmentUrl = "http://example.com/book/fulfill"; var mimeType = "mime/type"; it("dispatches request, load, and success", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = true; fetcher.testData = { fulfillmentUrl: fulfillmentUrl, mimeType: mimeType }; actions.updateBook(borrowUrl)(dispatch).then(function (data) { chai_1.expect(dispatch.callCount).to.equal(3); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.UPDATE_BOOK_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.UPDATE_BOOK_SUCCESS); chai_1.expect(dispatch.args[2][0].type).to.equal(actions.LOAD_UPDATE_BOOK_DATA); chai_1.expect(data).to.equal(fetcher.testData); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("dispatches failure", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = false; actions.updateBook(borrowUrl)(dispatch).catch(function (err) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.UPDATE_BOOK_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.UPDATE_BOOK_FAILURE); chai_1.expect(err).to.equal("test error"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); }); describe("fulfillBook", function () { var fulfillmentUrl = "http://example.com/book/fulfill"; it("dispatches request, load, and success", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = true; fetcher.testData = { blob: function () { return "blob"; }, ok: true }; actions.fulfillBook(fulfillmentUrl)(dispatch).then(function (data) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FULFILL_BOOK_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FULFILL_BOOK_SUCCESS); chai_1.expect(data).to.equal("blob"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("dispatches failure", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = false; actions.fulfillBook(fulfillmentUrl)(dispatch).catch(function (err) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FULFILL_BOOK_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FULFILL_BOOK_FAILURE); chai_1.expect(err).to.equal("test error"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); }); describe("indirectFulfillBook", function () { var fulfillmentUrl = "http://example.com/book/fulfill"; var fulfillmentType = "text/html;profile=http://librarysimplified.org/terms/profiles/streaming-media"; var indirectUrl = "http://example.com/reader"; it("dispatches request, load, and success", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = true; fetcher.testData = { fulfillmentLinks: [ { url: indirectUrl, type: fulfillmentType } ] }; actions.indirectFulfillBook(fulfillmentUrl, fulfillmentType)(dispatch).then(function (url) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FULFILL_BOOK_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FULFILL_BOOK_SUCCESS); chai_1.expect(url).to.equal(indirectUrl); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("dispatches failure", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = false; actions.indirectFulfillBook(fulfillmentUrl, fulfillmentType)(dispatch).catch(function (err) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FULFILL_BOOK_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FULFILL_BOOK_FAILURE); chai_1.expect(err).to.equal("test error"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); }); describe("fetchLoans", function () { var loansUrl = "http://example.com/loans"; it("dispatches request, load, and success", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = true; fetcher.testData = testData; actions.fetchLoans(loansUrl)(dispatch).then(function (data) { chai_1.expect(dispatch.callCount).to.equal(3); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FETCH_LOANS_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FETCH_LOANS_SUCCESS); chai_1.expect(dispatch.args[2][0].type).to.equal(actions.LOAD_LOANS); chai_1.expect(data).to.equal(testData); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("dispatches failure", function (done) { var dispatch = sinon_1.stub(); fetcher.resolve = false; actions.fetchLoans(loansUrl)(dispatch).catch(function (err) { chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.FETCH_LOANS_REQUEST); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.FETCH_LOANS_FAILURE); chai_1.expect(err).to.equal("test error"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); }); describe("closeErrorAndHideAuthForm", function () { it("closes error message", function () { var dispatch = sinon_1.stub(); actions.closeErrorAndHideAuthForm()(dispatch); chai_1.expect(dispatch.callCount).to.equal(2); chai_1.expect(dispatch.args[0][0].type).to.equal(actions.CLOSE_ERROR); chai_1.expect(dispatch.args[1][0].type).to.equal(actions.HIDE_AUTH_FORM); }); }); describe("saveAuthCredentials", function () { it("sets fetcher credentials", function () { var credentials = { provider: "test", credentials: "credentials" }; actions.saveAuthCredentials(credentials); chai_1.expect(fetcher.setAuthCredentials.callCount).to.equal(1); chai_1.expect(fetcher.setAuthCredentials.args[0][0]).to.deep.equal(credentials); }); }); describe("clearAuthCredentials", function () { it("clears fetcher credentials", function () { fetcher.clearAuthCredentials = sinon_1.stub(); actions.clearAuthCredentials(); chai_1.expect(fetcher.clearAuthCredentials.callCount).to.equal(1); }); }); });