tests for DataFileServiceImpl

This commit is contained in:
alecmerdler 2017-03-12 00:21:59 -08:00
parent 7416534ab8
commit 527e108d2e
3 changed files with 92 additions and 8 deletions

View file

@ -1,5 +1,6 @@
import { DataFileServiceImpl } from './datafile.service.impl';
import { Mock } from 'ts-mocks';
import Spy = jasmine.Spy;
describe("DataFileServiceImpl", () => {
@ -14,20 +15,101 @@ describe("DataFileServiceImpl", () => {
});
describe("blobToString", () => {
var data: any;
var blob: Blob;
beforeEach(() => {
data = {hello: "world"};
blob = new Blob([JSON.stringify(data)]);
fileReaderMock.setup(mock => mock.readAsText).is((blob: Blob) => {
fileReaderMock.Object.onload(<any>{target: {result: blob.toString()}});
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) => {
fileReaderMock.Object.onerror(new ErrorEvent("onerror"));
});
dataFileServiceImpl.blobToString(blob, (result) => {
expect(result).toBe(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.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();
});
});
});
describe("arrayToString", () => {
var blob: Blob;
var data: any;
beforeEach(() => {
fileReaderMock.setup(mock => mock.readAsArrayBuffer).is((blob: Blob) => {
fileReaderMock.Object.onload(<any>{target: {result: blob.toString()}});
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();
});
});
});

View file

@ -12,15 +12,17 @@ export class DataFileServiceImpl implements DataFileService {
}
public blobToString(blob: any, callback: (result: string) => void): void {
var reader: FileReader = new FileReader();
public blobToString(blob: Blob, callback: (result: string) => void): void {
var reader: FileReader = this.fileReaderFactory();
reader.onload = (event: Event) => callback(event.target['result']);
reader.onerror = (event: Event) => callback(null);
reader.onabort = (event: Event) => callback(null);
reader.readAsText(blob);
}
public arrayToString(buf: any, callback: (result: string) => void): void {
var blob: Blob = new Blob([buf], {type: 'application/octet-binary'});
var reader = new FileReader();
const blob: Blob = new Blob([buf], {type: 'application/octet-binary'});
var reader: FileReader = this.fileReaderFactory();
reader.onload = (event: Event) => callback(event.target['result']);
reader.onerror = (event: Event) => callback(null);
reader.onabort = (event: Event) => callback(null);

View file

@ -13,7 +13,7 @@ export abstract class DataFileService {
* @param blob The blob to convert.
* @param callback The success callback given converted blob.
*/
public abstract blobToString(blob: any, callback: (result: string) => void): void;
public abstract blobToString(blob: Blob, callback: (result: string) => void): void;
/**
* Convert array to string.