easier mocking/spying using ts-mocks
This commit is contained in:
parent
4303f658e8
commit
40e6540074
8 changed files with 73 additions and 22 deletions
|
@ -62,6 +62,7 @@
|
||||||
"source-map-loader": "0.1.5",
|
"source-map-loader": "0.1.5",
|
||||||
"style-loader": "0.13.1",
|
"style-loader": "0.13.1",
|
||||||
"ts-loader": "^0.9.5",
|
"ts-loader": "^0.9.5",
|
||||||
|
"ts-mocks": "^0.2.2",
|
||||||
"typescript": "^2.2.1",
|
"typescript": "^2.2.1",
|
||||||
"typings": "1.4.0",
|
"typings": "1.4.0",
|
||||||
"webpack": "^2.2"
|
"webpack": "^2.2"
|
||||||
|
|
|
@ -10,10 +10,8 @@ angular.module('quay').directive('dockerfileBuildForm', function () {
|
||||||
restrict: 'C',
|
restrict: 'C',
|
||||||
scope: {
|
scope: {
|
||||||
'repository': '=repository',
|
'repository': '=repository',
|
||||||
|
|
||||||
'isReady': '=?isReady',
|
'isReady': '=?isReady',
|
||||||
'reset': '=?reset',
|
'reset': '=?reset',
|
||||||
|
|
||||||
'readyForBuild': '&readyForBuild'
|
'readyForBuild': '&readyForBuild'
|
||||||
},
|
},
|
||||||
controller: function($scope, $element, ApiService, DockerfileService, Config) {
|
controller: function($scope, $element, ApiService, DockerfileService, Config) {
|
||||||
|
|
0
static/js/services/datafile/datafile.service.impl.ts
Normal file
0
static/js/services/datafile/datafile.service.impl.ts
Normal file
48
static/js/services/datafile/datafile.service.ts
Normal file
48
static/js/services/datafile/datafile.service.ts
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
/**
|
||||||
|
* Service which provides helper methods for downloading a data file from a URL, and extracting
|
||||||
|
* its contents as .tar, .tar.gz, or .zip file. Note that this service depends on external
|
||||||
|
* library code in the lib/ directory:
|
||||||
|
* - jszip.min.js
|
||||||
|
* - Blob.js
|
||||||
|
* - zlib.js
|
||||||
|
*/
|
||||||
|
export abstract class DatafileService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a blob to a string.
|
||||||
|
* @param blob The blob to convert.
|
||||||
|
* @param callback The success callback given converted blob.
|
||||||
|
*/
|
||||||
|
public abstract blobToString(blob: any, callback: (result: string) => void): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert array to string.
|
||||||
|
* @param buf The array buffer to convert.
|
||||||
|
* @param callback The success callback given converted array buffer.
|
||||||
|
*/
|
||||||
|
public abstract arrayToString(buf: any, callback: (result: string) => void): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a given data array is an archive file.
|
||||||
|
* @param buf The data array to check.
|
||||||
|
* @param success The success callback if the given array is an archive file, given the file contents.
|
||||||
|
* @param failure The failure callback if the given array is not an archive file, given the error message.
|
||||||
|
*/
|
||||||
|
public abstract readDataArrayAsPossibleArchive(buf: any,
|
||||||
|
success: (result: any) => void,
|
||||||
|
failure: (error: any) => void): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download a file into an array buffer while tracking progress.
|
||||||
|
* @param $scope An AngularJS $scope instance.
|
||||||
|
* @param url The URL of the file to be downloaded.
|
||||||
|
* @param progress The callback for download progress.
|
||||||
|
* @param error The error callback.
|
||||||
|
* @param loaded The success callback given the downloaded array buffer.
|
||||||
|
*/
|
||||||
|
public abstract downloadDataFileAsArrayBuffer($scope: ng.IScope,
|
||||||
|
url: string,
|
||||||
|
progress: (percent: number) => void,
|
||||||
|
error: () => void,
|
||||||
|
loaded: (uint8array: Uint8Array) => void): void;
|
||||||
|
}
|
|
@ -1,22 +1,22 @@
|
||||||
import { DockerfileServiceImpl, DockerfileInfoImpl } from './dockerfile.service.impl';
|
import { DockerfileServiceImpl, DockerfileInfoImpl } from './dockerfile.service.impl';
|
||||||
|
import { DatafileService } from '../datafile/datafile.service';
|
||||||
import Spy = jasmine.Spy;
|
import Spy = jasmine.Spy;
|
||||||
|
import { Mock } from 'ts-mocks';
|
||||||
|
|
||||||
|
|
||||||
describe("DockerfileServiceImpl", () => {
|
describe("DockerfileServiceImpl", () => {
|
||||||
var dockerfileServiceImpl: DockerfileServiceImpl;
|
var dockerfileServiceImpl: DockerfileServiceImpl;
|
||||||
var dataFileServiceMock: any;
|
var datafileServiceMock: Mock<DatafileService>;
|
||||||
|
var datafileService: DatafileService;
|
||||||
var configMock: any;
|
var configMock: any;
|
||||||
var fileReaderMock: FileReader;
|
var fileReaderMock: FileReader;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
dataFileServiceMock = jasmine.createSpyObj('dataFileServiceMock', [
|
datafileServiceMock = new Mock<DatafileService>();
|
||||||
'readDataArrayAsPossibleArchive',
|
datafileService = datafileServiceMock.Object;
|
||||||
'arrayToString',
|
|
||||||
'blobToString',
|
|
||||||
]);
|
|
||||||
configMock = jasmine.createSpyObj('configMock', ['getDomain']);
|
configMock = jasmine.createSpyObj('configMock', ['getDomain']);
|
||||||
fileReaderMock = new FileReader();
|
fileReaderMock = new FileReader();
|
||||||
dockerfileServiceImpl = new DockerfileServiceImpl(dataFileServiceMock, configMock, () => fileReaderMock);
|
dockerfileServiceImpl = new DockerfileServiceImpl(datafileService, configMock, () => fileReaderMock);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getDockerfile", () => {
|
describe("getDockerfile", () => {
|
||||||
|
@ -27,16 +27,15 @@ describe("DockerfileServiceImpl", () => {
|
||||||
var forDataSpy: Spy;
|
var forDataSpy: Spy;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
datafileServiceMock.setup(mock => mock.readDataArrayAsPossibleArchive).is((buf, success, failure) => {
|
||||||
failure([]);
|
failure([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
dataFileServiceMock.arrayToString.and.callFake((buf, callback) => {
|
datafileServiceMock.setup(mock => mock.arrayToString).is((buf, callback) => {
|
||||||
var contents: string = "";
|
callback("");
|
||||||
callback(contents);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
dataFileServiceMock.blobToString.and.callFake((blob, callback) => {
|
datafileServiceMock.setup(mock => mock.blobToString).is((blob, callback) => {
|
||||||
callback(blob.toString());
|
callback(blob.toString());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -55,7 +54,7 @@ describe("DockerfileServiceImpl", () => {
|
||||||
dockerfileServiceImpl.getDockerfile(file)
|
dockerfileServiceImpl.getDockerfile(file)
|
||||||
.then((dockerfile: DockerfileInfoImpl) => {
|
.then((dockerfile: DockerfileInfoImpl) => {
|
||||||
expect(readAsFileBufferSpy.calls.argsFor(0)[0]).toEqual(file);
|
expect(readAsFileBufferSpy.calls.argsFor(0)[0]).toEqual(file);
|
||||||
expect(dataFileServiceMock.readDataArrayAsPossibleArchive).toHaveBeenCalled();
|
expect(datafileService.readDataArrayAsPossibleArchive).toHaveBeenCalled();
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch((error: string) => {
|
.catch((error: string) => {
|
||||||
|
@ -67,7 +66,7 @@ describe("DockerfileServiceImpl", () => {
|
||||||
it("calls datafile service to convert file to string if given file is not an archive", (done) => {
|
it("calls datafile service to convert file to string if given file is not an archive", (done) => {
|
||||||
dockerfileServiceImpl.getDockerfile(file)
|
dockerfileServiceImpl.getDockerfile(file)
|
||||||
.then((dockerfile: DockerfileInfoImpl) => {
|
.then((dockerfile: DockerfileInfoImpl) => {
|
||||||
expect(dataFileServiceMock.arrayToString.calls.argsFor(0)[0]).toEqual(file);
|
expect((<Spy>datafileService.arrayToString).calls.argsFor(0)[0]).toEqual(file);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch((error: string) => {
|
.catch((error: string) => {
|
||||||
|
@ -102,7 +101,7 @@ describe("DockerfileServiceImpl", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns rejected promise if given archive file with no Dockerfile present in root directory", (done) => {
|
it("returns rejected promise if given archive file with no Dockerfile present in root directory", (done) => {
|
||||||
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
datafileServiceMock.setup(mock => mock.readDataArrayAsPossibleArchive).is((buf, success, failure) => {
|
||||||
success(invalidArchiveFile);
|
success(invalidArchiveFile);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -118,14 +117,14 @@ describe("DockerfileServiceImpl", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("calls datafile service to convert blob to string if given file is an archive", (done) => {
|
it("calls datafile service to convert blob to string if given file is an archive", (done) => {
|
||||||
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
datafileServiceMock.setup(mock => mock.readDataArrayAsPossibleArchive).is((buf, success, failure) => {
|
||||||
success(validArchiveFile);
|
success(validArchiveFile);
|
||||||
});
|
});
|
||||||
|
|
||||||
dockerfileServiceImpl.getDockerfile(file)
|
dockerfileServiceImpl.getDockerfile(file)
|
||||||
.then((dockerfile: DockerfileInfoImpl) => {
|
.then((dockerfile: DockerfileInfoImpl) => {
|
||||||
expect(validArchiveFile[0].toBlob).toHaveBeenCalled();
|
expect(validArchiveFile[0].toBlob).toHaveBeenCalled();
|
||||||
expect(dataFileServiceMock.blobToString.calls.argsFor(0)[0]).toEqual(validArchiveFile[0].toBlob());
|
expect((<Spy>datafileService.blobToString).calls.argsFor(0)[0]).toEqual(validArchiveFile[0].toBlob());
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch((error: string) => {
|
.catch((error: string) => {
|
||||||
|
@ -137,7 +136,7 @@ describe("DockerfileServiceImpl", () => {
|
||||||
it("returns rejected promise if given archive file with invalid Dockerfile", (done) => {
|
it("returns rejected promise if given archive file with invalid Dockerfile", (done) => {
|
||||||
forDataSpy.and.returnValue(null);
|
forDataSpy.and.returnValue(null);
|
||||||
invalidArchiveFile[0].name = 'Dockerfile';
|
invalidArchiveFile[0].name = 'Dockerfile';
|
||||||
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
datafileServiceMock.setup(mock => mock.readDataArrayAsPossibleArchive).is((buf, success, failure) => {
|
||||||
success(invalidArchiveFile);
|
success(invalidArchiveFile);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -153,7 +152,7 @@ describe("DockerfileServiceImpl", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns resolved promise of new DockerfileInfoImpl instance if given archive with valid Dockerfile", (done) => {
|
it("returns resolved promise of new DockerfileInfoImpl instance if given archive with valid Dockerfile", (done) => {
|
||||||
dataFileServiceMock.readDataArrayAsPossibleArchive.and.callFake((buf, success, failure) => {
|
datafileServiceMock.setup(mock => mock.readDataArrayAsPossibleArchive).is((buf, success, failure) => {
|
||||||
success(validArchiveFile);
|
success(validArchiveFile);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import { DockerfileService, DockerfileInfo } from './dockerfile.service';
|
import { DockerfileService, DockerfileInfo } from './dockerfile.service';
|
||||||
import { Injectable } from 'angular-ts-decorators';
|
import { Injectable } from 'angular-ts-decorators';
|
||||||
|
import { DatafileService } from '../datafile/datafile.service';
|
||||||
|
|
||||||
|
|
||||||
@Injectable(DockerfileService.name)
|
@Injectable(DockerfileService.name)
|
||||||
export class DockerfileServiceImpl implements DockerfileService {
|
export class DockerfileServiceImpl implements DockerfileService {
|
||||||
|
|
||||||
constructor(private DataFileService: any,
|
constructor(private DataFileService: DatafileService,
|
||||||
private Config: any,
|
private Config: any,
|
||||||
private fileReaderFactory: () => FileReader) {
|
private fileReaderFactory: () => FileReader) {
|
||||||
|
|
||||||
|
|
|
@ -3880,6 +3880,10 @@ ts-loader@^0.9.5:
|
||||||
object-assign "^4.1.0"
|
object-assign "^4.1.0"
|
||||||
semver "^5.0.1"
|
semver "^5.0.1"
|
||||||
|
|
||||||
|
ts-mocks@^0.2.2:
|
||||||
|
version "0.2.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/ts-mocks/-/ts-mocks-0.2.2.tgz#051e5b3a30068f6b9f1b1faa552a6f172793c6d6"
|
||||||
|
|
||||||
ts-node@^1.2.1:
|
ts-node@^1.2.1:
|
||||||
version "1.7.3"
|
version "1.7.3"
|
||||||
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-1.7.3.tgz#dee7f8a84751732d3c2e497cac5a02fb117dfee7"
|
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-1.7.3.tgz#dee7f8a84751732d3c2e497cac5a02fb117dfee7"
|
||||||
|
|
Reference in a new issue