opds-web-client
Version:
85 lines (84 loc) • 3.76 kB
JavaScript
dontMock("../DownloadButton");
jest.dontMock("./collectionData");
jest.dontMock("../../__mocks__/downloadjs");
var download = require("../../__mocks__/downloadjs");
jest.setMock("downloadjs", download);
var React = require("react");
var enzyme_1 = require("enzyme");
var DownloadButton_1 = require("../DownloadButton");
describe("DownloadButton", function () {
var wrapper;
var fulfill;
var indirectFulfill;
var style;
beforeEach(function () {
fulfill = jest.genMockFunction();
fulfill.mockReturnValue(new Promise(function (resolve, reject) { return resolve("blob"); }));
indirectFulfill = jest.genMockFunction();
indirectFulfill.mockReturnValue(new Promise(function (resolve, reject) { return resolve("web reader url"); }));
style = { border: "100px solid black" };
wrapper = enzyme_1.shallow(React.createElement(DownloadButton_1.default, {style: style, url: "download url", mimeType: "application/epub+zip", fulfill: fulfill, indirectFulfill: indirectFulfill, title: "title"}));
});
it("shows button", function () {
var button = wrapper.find("button");
expect(button.props().style).toBe(style);
expect(button.text()).toBe("Download EPUB");
});
it("shows plain link if specified", function () {
wrapper.setProps({ isPlainLink: true });
var link = wrapper.find("a");
expect(link.props().style).toBe(style);
expect(link.props().href).toBe("download url");
expect(link.text()).toBe("Download EPUB");
});
it("fulfills when clicked", function () {
var button = wrapper.find("button");
button.simulate("click");
expect(fulfill.mock.calls.length).toBe(1);
expect(fulfill.mock.calls[0][0]).toBe("download url");
});
it("downloads after fulfilling", function (done) {
var button = wrapper.find("button");
button.props().onClick().then(function () {
expect(download.getBlob()).toBe("blob");
expect(download.getFilename()).toBe(wrapper.instance().generateFilename("title"));
expect(download.getMimeType()).toBe(wrapper.instance().mimeType());
done();
}).catch(done.fail);
});
it("fulfills OPDS-based indirect links", function () {
var streamingType = "text/html;profile=http://librarysimplified.org/terms/profiles/streaming-media";
wrapper.setProps({
mimeType: "application/atom+xml;type=entry;profile=opds-catalog",
indirectType: streamingType
});
var button = wrapper.find("button");
button.simulate("click");
expect(indirectFulfill.mock.calls.length).toBe(1);
expect(indirectFulfill.mock.calls[0][0]).toBe("download url");
expect(indirectFulfill.mock.calls[0][1]).toBe(streamingType);
});
it("fulfills ACSM-based indirect links", function () {
wrapper.setProps({
mimeType: "vnd.adobe/adept+xml",
indirectType: "application/epub+zip"
});
var button = wrapper.find("button");
button.simulate("click");
expect(fulfill.mock.calls.length).toBe(1);
expect(fulfill.mock.calls[0][0]).toBe("download url");
});
it("opens indirect fulfillment link in new tab", function (done) {
wrapper.setProps({
mimeType: "application/atom+xml;type=entry;profile=opds-catalog",
indirectType: "some/type"
});
spyOn(window, "open");
var button = wrapper.find("button");
button.props().onClick().then(function () {
expect(window.open).toHaveBeenCalledWith("web reader url", "_blank");
done();
}).catch(done.fail);
});
});
;
jest.