This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/static/js/services/datafile/datafile.service.impl.spec.ts

125 lines
3.6 KiB
TypeScript
Raw Normal View History

import { DataFileServiceImpl } from './datafile.service.impl';
import { Mock } from 'ts-mocks';
2017-03-12 08:21:59 +00:00
import Spy = jasmine.Spy;
2017-03-11 23:47:44 +00:00
describe("DataFileServiceImpl", () => {
var dataFileServiceImpl: DataFileServiceImpl;
var fileReaderMock: Mock<FileReader>;
var fileReader: FileReader;
2017-03-11 23:47:44 +00:00
beforeEach(() => {
fileReaderMock = new Mock<FileReader>();
fileReader = fileReaderMock.Object;
dataFileServiceImpl = new DataFileServiceImpl(() => fileReader);
2017-03-11 23:47:44 +00:00
});
describe("blobToString", () => {
2017-03-12 08:21:59 +00:00
var data: any;
var blob: Blob;
2017-03-11 23:47:44 +00:00
beforeEach(() => {
2017-03-12 08:21:59 +00:00
data = {hello: "world"};
blob = new Blob([JSON.stringify(data)]);
fileReaderMock.setup(mock => mock.readAsText).is((blob: Blob) => {
fileReaderMock.Object.onload(<any>{target: {result: data}});
});
});
it("calls file reader to read given blob", (done) => {
dataFileServiceImpl.blobToString(blob, (result) => {
expect((<Spy>fileReader.readAsText).calls.argsFor(0)[0]).toEqual(blob);
done();
});
});
it("calls given callback with null if file reader errors", (done) => {
fileReaderMock.setup(mock => mock.readAsText).is((blob: Blob) => {
2017-03-12 08:21:59 +00:00
fileReaderMock.Object.onerror(new ErrorEvent("onerror"));
});
dataFileServiceImpl.blobToString(blob, (result) => {
expect(result).toBe(null);
done();
});
});
2017-03-12 08:21:59 +00:00
it("calls given callback with null if file reader aborts", (done) => {
fileReaderMock.setup(mock => mock.readAsText).is((blob: Blob) => {
fileReaderMock.Object.onabort(new Event("onabort"));
});
dataFileServiceImpl.blobToString(blob, (result) => {
expect(result).toBe(null);
done();
});
});
it("calls given callback with result when file reader successfully loads", (done) => {
dataFileServiceImpl.blobToString(blob, (result) => {
expect(result).toBe(data);
done();
});
});
2017-03-11 23:47:44 +00:00
});
describe("arrayToString", () => {
2017-03-12 08:21:59 +00:00
var blob: Blob;
var data: any;
2017-03-11 23:47:44 +00:00
beforeEach(() => {
2017-03-12 08:21:59 +00:00
data = JSON.stringify({hello: "world"});
blob = new Blob([data], {type: 'application/octet-binary'});
fileReaderMock.setup(mock => mock.readAsText).is((blob: Blob) => {
fileReaderMock.Object.onload(<any>{target: {result: data}});
});
});
it("calls file reader to read blob created from given buffer", (done) => {
dataFileServiceImpl.arrayToString(data, (result) => {
expect((<Spy>fileReader.readAsText).calls.argsFor(0)[0]).toEqual(blob);
done();
});
});
it("calls given callback with null if file reader errors", (done) => {
fileReaderMock.setup(mock => mock.readAsText).is((blob: Blob) => {
fileReaderMock.Object.onerror(new ErrorEvent("onerror"));
});
dataFileServiceImpl.arrayToString(data, (result) => {
expect(result).toEqual(null);
done();
});
});
it("calls given callback with null if file reader aborts", (done) => {
fileReaderMock.setup(mock => mock.readAsText).is((blob: Blob) => {
fileReaderMock.Object.onabort(new Event("onabort"));
});
dataFileServiceImpl.arrayToString(data, (result) => {
expect(result).toEqual(null);
done();
});
});
it("calls given callback with result when file reader successfully loads", (done) => {
dataFileServiceImpl.arrayToString(data, (result) => {
expect(result).toEqual(data);
done();
});
});
2017-03-11 23:47:44 +00:00
});
describe("readDataArrayAsPossibleArchive", () => {
2017-03-12 08:29:39 +00:00
// TODO
2017-03-11 23:47:44 +00:00
});
describe("downloadDataFileAsArrayBuffer", () => {
2017-03-12 08:29:39 +00:00
// TODO
2017-03-11 23:47:44 +00:00
});
2017-03-12 08:29:39 +00:00
});