From 527e108d2ef8dfb11738080e1fb49c8243611c0b Mon Sep 17 00:00:00 2001 From: alecmerdler Date: Sun, 12 Mar 2017 00:21:59 -0800 Subject: [PATCH] tests for DataFileServiceImpl --- .../datafile/datafile.service.impl.spec.ts | 88 ++++++++++++++++++- .../datafile/datafile.service.impl.ts | 10 ++- .../js/services/datafile/datafile.service.ts | 2 +- 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/static/js/services/datafile/datafile.service.impl.spec.ts b/static/js/services/datafile/datafile.service.impl.spec.ts index 7f828c750..33e3a1f29 100644 --- a/static/js/services/datafile/datafile.service.impl.spec.ts +++ b/static/js/services/datafile/datafile.service.impl.spec.ts @@ -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({target: {result: blob.toString()}}); + fileReaderMock.Object.onload({target: {result: data}}); }); }); + it("calls file reader to read given blob", (done) => { + dataFileServiceImpl.blobToString(blob, (result) => { + expect((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({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({target: {result: data}}); + }); + }); + + it("calls file reader to read blob created from given buffer", (done) => { + dataFileServiceImpl.arrayToString(data, (result) => { + expect((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(); }); }); }); diff --git a/static/js/services/datafile/datafile.service.impl.ts b/static/js/services/datafile/datafile.service.impl.ts index ed0e50fec..be5ce180f 100644 --- a/static/js/services/datafile/datafile.service.impl.ts +++ b/static/js/services/datafile/datafile.service.impl.ts @@ -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); diff --git a/static/js/services/datafile/datafile.service.ts b/static/js/services/datafile/datafile.service.ts index cd7bf93ca..47c990d83 100644 --- a/static/js/services/datafile/datafile.service.ts +++ b/static/js/services/datafile/datafile.service.ts @@ -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.