opds-web-client
Version:
245 lines (244 loc) • 12.2 kB
JavaScript
dontMock("../actions");
var testData = {
lanes: [],
books: [{
id: "test id",
url: "http://example.com/book",
title: "test title"
}]
};
var DataFetcher_1 = require("../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 = jest.genMockFunction();
fetcher.resolve = true;
actions.fetchCollection(collectionUrl)(dispatch).then(function (data) {
expect(dispatch.mock.calls.length).toBe(3);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FETCH_COLLECTION_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FETCH_COLLECTION_SUCCESS);
expect(dispatch.mock.calls[2][0].type).toBe(actions.LOAD_COLLECTION);
expect(data).toBe(testData);
done();
}).catch(done.fail);
});
it("dispatches failure", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = false;
actions.fetchCollection(collectionUrl)(dispatch).catch(function (err) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FETCH_COLLECTION_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FETCH_COLLECTION_FAILURE);
expect(err).toBe("test error");
done();
});
});
});
describe("fetchPage", function () {
it("dispatches request, success, and load", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = true;
actions.fetchPage("http://example.com/feed")(dispatch).then(function (data) {
expect(dispatch.mock.calls.length).toBe(3);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FETCH_PAGE_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FETCH_PAGE_SUCCESS);
expect(dispatch.mock.calls[2][0].type).toBe(actions.LOAD_PAGE);
expect(data).toBe(testData);
done();
}).catch(done.fail);
});
it("dispatches failure", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = false;
actions.fetchPage("http://example.com/feed")(dispatch).catch(function (err) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FETCH_PAGE_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FETCH_PAGE_FAILURE);
expect(err).toBe("test error");
done();
});
});
});
describe("fetchSearchDescription", function () {
it("dispatches load", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = true;
actions.fetchSearchDescription("http://example.com/search")(dispatch).then(function (data) {
expect(dispatch.mock.calls.length).toBe(1);
expect(dispatch.mock.calls[0][0].type).toBe(actions.LOAD_SEARCH_DESCRIPTION);
expect(data).toBe(testData);
done();
}).catch(done.fail);
});
});
describe("fetchBook", function () {
var bookUrl = "http://example.com/book";
it("dispatches request, load, and success", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = true;
actions.fetchBook(bookUrl)(dispatch).then(function (data) {
expect(dispatch.mock.calls.length).toBe(3);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FETCH_BOOK_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FETCH_BOOK_SUCCESS);
expect(dispatch.mock.calls[2][0].type).toBe(actions.LOAD_BOOK);
expect(data).toBe(testData);
done();
}).catch(done.fail);
});
it("dispatches failure", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = false;
actions.fetchBook(bookUrl)(dispatch).catch(function (err) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FETCH_BOOK_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FETCH_BOOK_FAILURE);
expect(err).toBe("test error");
done();
});
});
});
describe("borrowBook", 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 = jest.genMockFunction();
fetcher.resolve = true;
fetcher.testData = { fulfillmentUrl: fulfillmentUrl, mimeType: mimeType };
actions.borrowBook(borrowUrl)(dispatch).then(function (data) {
expect(dispatch.mock.calls.length).toBe(3);
expect(dispatch.mock.calls[0][0].type).toBe(actions.BORROW_BOOK_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.BORROW_BOOK_SUCCESS);
expect(dispatch.mock.calls[2][0].type).toBe(actions.LOAD_BORROW_DATA);
expect(data).toEqual(fetcher.testData);
done();
}).catch(done.fail);
});
it("dispatches failure", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = false;
actions.borrowBook(borrowUrl)(dispatch).catch(function (err) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.BORROW_BOOK_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.BORROW_BOOK_FAILURE);
expect(err).toBe("test error");
done();
});
});
});
describe("fulfillBook", function () {
var fulfillmentUrl = "http://example.com/book/fulfill";
it("dispatches request, load, and success", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = true;
fetcher.testData = { blob: function () { return "blob"; }, ok: true };
actions.fulfillBook(fulfillmentUrl)(dispatch).then(function (data) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FULFILL_BOOK_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FULFILL_BOOK_SUCCESS);
expect(data).toBe("blob");
done();
}).catch(done.fail);
});
it("dispatches failure", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = false;
actions.fulfillBook(fulfillmentUrl)(dispatch).catch(function (err) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FULFILL_BOOK_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FULFILL_BOOK_FAILURE);
expect(err).toBe("test error");
done();
}).catch(done.fail);
});
});
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 = jest.genMockFunction();
fetcher.resolve = true;
fetcher.testData = {
fulfillmentLinks: [
{ url: indirectUrl, type: fulfillmentType }
]
};
actions.indirectFulfillBook(fulfillmentUrl, fulfillmentType)(dispatch).then(function (url) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FULFILL_BOOK_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FULFILL_BOOK_SUCCESS);
expect(url).toBe(indirectUrl);
done();
}).catch(done.fail);
});
it("dispatches failure", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = false;
actions.indirectFulfillBook(fulfillmentUrl, fulfillmentType)(dispatch).catch(function (err) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FULFILL_BOOK_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FULFILL_BOOK_FAILURE);
expect(err).toBe("test error");
done();
}).catch(done.fail);
});
});
describe("fetchLoans", function () {
var loansUrl = "http://example.com/loans";
it("dispatches request, load, and success", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = true;
fetcher.testData = testData;
actions.fetchLoans(loansUrl)(dispatch).then(function (data) {
expect(dispatch.mock.calls.length).toBe(3);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FETCH_LOANS_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FETCH_LOANS_SUCCESS);
expect(dispatch.mock.calls[2][0].type).toBe(actions.LOAD_LOANS);
expect(data).toBe(testData);
done();
}).catch(done.fail);
});
it("dispatches failure", function (done) {
var dispatch = jest.genMockFunction();
fetcher.resolve = false;
actions.fetchLoans(loansUrl)(dispatch).catch(function (err) {
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.FETCH_LOANS_REQUEST);
expect(dispatch.mock.calls[1][0].type).toBe(actions.FETCH_LOANS_FAILURE);
expect(err).toBe("test error");
done();
}).catch(done.fail);
});
});
describe("closeErrorAndHideBasicAuthForm", function () {
it("closes error message", function () {
var dispatch = jest.genMockFunction();
actions.closeErrorAndHideBasicAuthForm()(dispatch);
expect(dispatch.mock.calls.length).toBe(2);
expect(dispatch.mock.calls[0][0].type).toBe(actions.CLOSE_ERROR);
expect(dispatch.mock.calls[1][0].type).toBe(actions.HIDE_BASIC_AUTH_FORM);
});
});
describe("saveBasicAuthCredentials", function () {
it("sets fetcher credentaials", function () {
fetcher.setBasicAuthCredentials = jest.genMockFunction();
actions.saveBasicAuthCredentials("credentials");
expect(fetcher.setBasicAuthCredentials.mock.calls.length).toBe(1);
expect(fetcher.setBasicAuthCredentials.mock.calls[0][0]).toBe("credentials");
});
});
describe("clearBasicAuthCredentials", function () {
it("clears fetcher credentaials", function () {
fetcher.clearBasicAuthCredentials = jest.genMockFunction();
actions.clearBasicAuthCredentials();
expect(fetcher.clearBasicAuthCredentials.mock.calls.length).toBe(1);
});
});
});
;
jest.