From 38e6648e150f5a3f87c9b5427fe8df1762a77e1a Mon Sep 17 00:00:00 2001 From: Paolo Bueno Date: Thu, 9 Nov 2017 12:12:12 -0200 Subject: [PATCH 1/5] Add tests for buildCameraOptions --- client/camera/package.json | 3 +- client/camera/test/Camera-spec.ts | 3 ++ client/camera/test/buildCameraOptions-spec.ts | 30 +++++++++++++++++++ client/camera/test/index.ts | 1 + 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 client/camera/test/Camera-spec.ts create mode 100644 client/camera/test/buildCameraOptions-spec.ts diff --git a/client/camera/package.json b/client/camera/package.json index ca10b70..58e52b3 100644 --- a/client/camera/package.json +++ b/client/camera/package.json @@ -9,7 +9,7 @@ "scripts": { "clean": "del coverage_report src/**/*.js src/**/*.map test/**/*.js test/**/*.map", "build": "tsc", - "test": "npm run clean && nyc mocha" + "test": "nyc mocha" }, "publishConfig": { "access": "public" @@ -29,7 +29,6 @@ "text" ], "report-dir": "coverage_report", - "check-coverage": true, "lines": 75, "functions": 100, "branches": 80 diff --git a/client/camera/test/Camera-spec.ts b/client/camera/test/Camera-spec.ts new file mode 100644 index 0000000..4eabde1 --- /dev/null +++ b/client/camera/test/Camera-spec.ts @@ -0,0 +1,3 @@ +import { expect } from 'chai'; +import { buildCameraOptions } from '../src/buildCameraOptions'; +import { Camera } from '../src/Camera'; diff --git a/client/camera/test/buildCameraOptions-spec.ts b/client/camera/test/buildCameraOptions-spec.ts new file mode 100644 index 0000000..7cef99e --- /dev/null +++ b/client/camera/test/buildCameraOptions-spec.ts @@ -0,0 +1,30 @@ +import { expect } from 'chai'; +import { buildCameraOptions } from '../src/buildCameraOptions'; + +describe('buildCameraOptions', function() { + const mockCordovaCamera = { + DestinationType: { + FILE_URI: 1 + }, + EncodingType: { + JPEG: 1 + }, + PictureSourceType: { + CAMERA: 1 + }, + MediaType: { + PICTURE: 1 + } + }; + it('should accept a given camera object', function() { + return expect(buildCameraOptions(mockCordovaCamera)).to.be.ok; + }); + it('should return a configuration object', function() { + const opts = buildCameraOptions(mockCordovaCamera); + expect(opts).to.have.property('quality'); + expect(opts).to.have.property('sourceType'); + expect(opts).to.have.property('destinationType'); + expect(opts).to.have.property('encodingType'); + expect(opts).to.have.property('mediaType'); + }); +}); diff --git a/client/camera/test/index.ts b/client/camera/test/index.ts index e69de29..58522cc 100644 --- a/client/camera/test/index.ts +++ b/client/camera/test/index.ts @@ -0,0 +1 @@ +/// \ No newline at end of file From 16a3b6eaf6df7893a8838d067393960335df6b52 Mon Sep 17 00:00:00 2001 From: Paolo Bueno Date: Fri, 10 Nov 2017 11:06:47 -0200 Subject: [PATCH 2/5] Add some mocks --- client/camera/package.json | 4 ++- client/camera/src/Camera.ts | 4 +-- client/camera/test/Camera-spec.ts | 21 ++++++++++++++ client/camera/test/buildCameraOptions-spec.ts | 15 +--------- client/camera/test/mocks/camera.ts | 28 +++++++++++++++++++ 5 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 client/camera/test/mocks/camera.ts diff --git a/client/camera/package.json b/client/camera/package.json index 58e52b3..b345b5a 100644 --- a/client/camera/package.json +++ b/client/camera/package.json @@ -44,7 +44,9 @@ "proxyquire": "^1.8.0", "source-map-support": "^0.5.0", "ts-node": "^3.3.0", - "typescript": "^2.5.0" + "typescript": "^2.5.0", + "sinon": "^4.0.1", + "@types/sinon": "^2.3.3" }, "dependencies": { "b64-to-blob": "^1.2.19" diff --git a/client/camera/src/Camera.ts b/client/camera/src/Camera.ts index 2fa971d..b43500e 100644 --- a/client/camera/src/Camera.ts +++ b/client/camera/src/Camera.ts @@ -18,8 +18,8 @@ export interface CaptureResponse { type optionsBuilderFn = (camera: any) => CameraOptions; export class Camera { - protected initPromise: Promise; - protected options: CameraOptions; + public options: CameraOptions; + public initPromise: Promise; constructor(optionsBuilderFunction?: optionsBuilderFn) { this.init(optionsBuilderFunction); diff --git a/client/camera/test/Camera-spec.ts b/client/camera/test/Camera-spec.ts index 4eabde1..d1ad1ca 100644 --- a/client/camera/test/Camera-spec.ts +++ b/client/camera/test/Camera-spec.ts @@ -1,3 +1,24 @@ import { expect } from 'chai'; import { buildCameraOptions } from '../src/buildCameraOptions'; import { Camera } from '../src/Camera'; +import { mockCordovaCamera } from './mocks/camera'; + +describe('Camera', function() { + const subject = new Camera(function() { + return { quality: 80 }; + }); + + describe('init', function() { + it('should take a function to partially supply options', function() { + return expect(subject.initPromise).to.eventually.have.property('quality', 80); + }); + }); + describe('cleanup', function() { + xit('should call the cordova cleanup function', function() { + subject.cleanup(); + }); + }); + describe('capture', function() { + it('should resolve to a local file uri'); + }); +}); diff --git a/client/camera/test/buildCameraOptions-spec.ts b/client/camera/test/buildCameraOptions-spec.ts index 7cef99e..7c50a9f 100644 --- a/client/camera/test/buildCameraOptions-spec.ts +++ b/client/camera/test/buildCameraOptions-spec.ts @@ -1,21 +1,8 @@ import { expect } from 'chai'; import { buildCameraOptions } from '../src/buildCameraOptions'; +import { mockCordovaCamera } from './mocks/camera'; describe('buildCameraOptions', function() { - const mockCordovaCamera = { - DestinationType: { - FILE_URI: 1 - }, - EncodingType: { - JPEG: 1 - }, - PictureSourceType: { - CAMERA: 1 - }, - MediaType: { - PICTURE: 1 - } - }; it('should accept a given camera object', function() { return expect(buildCameraOptions(mockCordovaCamera)).to.be.ok; }); diff --git a/client/camera/test/mocks/camera.ts b/client/camera/test/mocks/camera.ts new file mode 100644 index 0000000..28e2c82 --- /dev/null +++ b/client/camera/test/mocks/camera.ts @@ -0,0 +1,28 @@ +import {} from 'sinon'; +export const mockCordovaCamera = { + // properties + DestinationType: { + FILE_URI: 1 + }, + EncodingType: { + JPEG: 1 + }, + PictureSourceType: { + CAMERA: 1 + }, + MediaType: { + PICTURE: 1 + }, + // methods + cleanup(onSuccess) { + onSuccess(); + } +}; + +declare var global: any; +global.window = global.window || {}; +global.navigator = global.navigator || {}; +global.window.navigator = global.navigator; + +global.window.camera = mockCordovaCamera; +global.navigator.camera = mockCordovaCamera; From 468ab3c3448e1550b46ac7f8e1828a0e8c81fa13 Mon Sep 17 00:00:00 2001 From: Paolo Bueno Date: Fri, 10 Nov 2017 11:16:41 -0200 Subject: [PATCH 3/5] Update versions to match hoisted --- client/camera/package.json | 6 +++++- client/camera/test/Camera-spec.ts | 6 +++++- client/camera/test/mocks/camera.ts | 6 ++---- client/filestore-client/package.json | 14 +++++++------- cloud/filestore/package.json | 8 ++++++-- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/client/camera/package.json b/client/camera/package.json index b345b5a..57d6940 100644 --- a/client/camera/package.json +++ b/client/camera/package.json @@ -46,7 +46,11 @@ "ts-node": "^3.3.0", "typescript": "^2.5.0", "sinon": "^4.0.1", - "@types/sinon": "^2.3.3" + "@types/sinon": "^2.3.3", + "@types/chai": "^4.0.3", + "chai": "^4.1.1", + "@types/chai-as-promised": "7.1.0", + "chai-as-promised": "^7.1.1" }, "dependencies": { "b64-to-blob": "^1.2.19" diff --git a/client/camera/test/Camera-spec.ts b/client/camera/test/Camera-spec.ts index d1ad1ca..9d9e27f 100644 --- a/client/camera/test/Camera-spec.ts +++ b/client/camera/test/Camera-spec.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import * as sinon from 'sinon'; import { buildCameraOptions } from '../src/buildCameraOptions'; import { Camera } from '../src/Camera'; import { mockCordovaCamera } from './mocks/camera'; @@ -16,9 +17,12 @@ describe('Camera', function() { describe('cleanup', function() { xit('should call the cordova cleanup function', function() { subject.cleanup(); + sinon.assert.called(mockCordovaCamera.cleanup); }); }); describe('capture', function() { - it('should resolve to a local file uri'); + it('should resolve to a local file uri', function() { + + }); }); }); diff --git a/client/camera/test/mocks/camera.ts b/client/camera/test/mocks/camera.ts index 28e2c82..2f1a142 100644 --- a/client/camera/test/mocks/camera.ts +++ b/client/camera/test/mocks/camera.ts @@ -1,4 +1,4 @@ -import {} from 'sinon'; +import { stub } from 'sinon'; export const mockCordovaCamera = { // properties DestinationType: { @@ -14,9 +14,7 @@ export const mockCordovaCamera = { PICTURE: 1 }, // methods - cleanup(onSuccess) { - onSuccess(); - } + cleanup: stub().callsArg(0) }; declare var global: any; diff --git a/client/filestore-client/package.json b/client/filestore-client/package.json index 2479eb8..af78a85 100644 --- a/client/filestore-client/package.json +++ b/client/filestore-client/package.json @@ -35,22 +35,22 @@ "branches": 80 }, "devDependencies": { - "@types/bluebird": "^3.5.16", - "@types/chai": "^4.0.4", - "@types/chai-as-promised": "^7.1.0", + "@types/bluebird": "^3.5.8", + "@types/chai": "^4.0.3", + "@types/chai-as-promised": "7.1.0", "@types/cordova-plugin-file": "0.0.3", "@types/cordova-plugin-file-transfer": "0.0.3", "@types/lodash": "^4.14.73", "@types/mocha": "^2.2.41", "@types/proxyquire": "^1.3.27", - "@types/sinon": "^2.3.7", - "chai": "^4.1.2", + "@types/sinon": "^2.3.3", + "chai": "^4.1.1", "chai-as-promised": "^7.1.1", - "del-cli": "^1.0.0", + "del-cli": "^1.1.0", "mocha": "^4.0.1", "nyc": "^11.1.0", "proxyquire": "^1.8.0", - "sinon": "^4.1.1", + "sinon": "^4.0.1", "source-map-support": "^0.5.0", "ts-node": "^3.3.0", "typescript": "^2.5.0" diff --git a/cloud/filestore/package.json b/cloud/filestore/package.json index 68f6f23..e75e15a 100644 --- a/cloud/filestore/package.json +++ b/cloud/filestore/package.json @@ -51,13 +51,17 @@ "source-map-support": "^0.5.0", "string-to-stream": "^1.1.0", "ts-node": "^3.3.0", - "typescript": "^2.5.0" + "typescript": "^2.5.0", + "@types/chai": "^4.0.3", + "chai": "^4.1.1", + "@types/chai-as-promised": "7.1.0", + "chai-as-promised": "^7.1.1" }, "dependencies": { "@raincatcher/logger": "0.0.1", "base64-stream": "0.1.3", "bluebird": "^3.5.0", - "express": "4.15.4", + "express": "^4.15.4", "gridfs-stream": "^1.1.1", "lodash": "^4.17.4", "mkdirp": "^0.5.1", From 54b0bd8319b341ce84bf46636985012044223b66 Mon Sep 17 00:00:00 2001 From: Paolo Bueno Date: Fri, 10 Nov 2017 11:52:55 -0200 Subject: [PATCH 4/5] Add tests for capture() --- client/camera/test/Camera-spec.ts | 37 ++++++++++++++++++++++++++---- client/camera/test/mocks/camera.ts | 3 ++- client/camera/test/mocks/file.ts | 7 ++++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 client/camera/test/mocks/file.ts diff --git a/client/camera/test/Camera-spec.ts b/client/camera/test/Camera-spec.ts index 9d9e27f..d91c7aa 100644 --- a/client/camera/test/Camera-spec.ts +++ b/client/camera/test/Camera-spec.ts @@ -1,8 +1,16 @@ +import * as Promise from 'bluebird'; import { expect } from 'chai'; +import * as chai from 'chai'; +import * as chaiAsPromised from 'chai-as-promised'; import * as sinon from 'sinon'; import { buildCameraOptions } from '../src/buildCameraOptions'; import { Camera } from '../src/Camera'; import { mockCordovaCamera } from './mocks/camera'; +import { resolveLocalFileSystemURL } from './mocks/file'; + +declare var global: any; + +chai.use(chaiAsPromised); describe('Camera', function() { const subject = new Camera(function() { @@ -15,14 +23,35 @@ describe('Camera', function() { }); }); describe('cleanup', function() { - xit('should call the cordova cleanup function', function() { - subject.cleanup(); - sinon.assert.called(mockCordovaCamera.cleanup); + it('should call the cordova cleanup function', function() { + return subject.cleanup().then(function() { + sinon.assert.called(mockCordovaCamera.cleanup); + }); }); }); describe('capture', function() { - it('should resolve to a local file uri', function() { + it('return a local uri when successful', function() { + return subject.capture().then(result => { + expect(result).to.deep.equal({ + type: 'uri', + value: 'some-uri' + }); + sinon.assert.called(resolveLocalFileSystemURL); + }); + }); + + it('should return a base64 data-uri when not a local file', function() { + // replace cordova file function to call error callback + const failingResolveLocalFileSystemURL = sinon.stub().callsArg(2); + global.window.resolveLocalFileSystemURL = failingResolveLocalFileSystemURL; + return subject.capture().then(result => { + sinon.assert.called(failingResolveLocalFileSystemURL); + expect(result).to.deep.equal({ + value: 'data:image/jpg;base64,some-uri', + type: 'base64' + }); + }); }); }); }); diff --git a/client/camera/test/mocks/camera.ts b/client/camera/test/mocks/camera.ts index 2f1a142..67f5ddf 100644 --- a/client/camera/test/mocks/camera.ts +++ b/client/camera/test/mocks/camera.ts @@ -14,7 +14,8 @@ export const mockCordovaCamera = { PICTURE: 1 }, // methods - cleanup: stub().callsArg(0) + cleanup: stub().callsArgAsync(0), + getPicture: stub().callsArgWithAsync(0, 'some-uri') }; declare var global: any; diff --git a/client/camera/test/mocks/file.ts b/client/camera/test/mocks/file.ts new file mode 100644 index 0000000..eb69262 --- /dev/null +++ b/client/camera/test/mocks/file.ts @@ -0,0 +1,7 @@ +import { stub } from 'sinon'; +declare var global: any; + +export const resolveLocalFileSystemURL = stub().callsArgAsync(1); + +global.window = global.window || {}; +global.window.resolveLocalFileSystemURL = resolveLocalFileSystemURL; From 66692592531c639fb8f91baf30881b6193a1b675 Mon Sep 17 00:00:00 2001 From: Paolo Bueno Date: Fri, 10 Nov 2017 11:59:19 -0200 Subject: [PATCH 5/5] Fix linting and remove coverage check on unfinished tests --- client/camera/test/index.ts | 2 +- client/filestore-client/package.json | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/client/camera/test/index.ts b/client/camera/test/index.ts index 58522cc..31cdb35 100644 --- a/client/camera/test/index.ts +++ b/client/camera/test/index.ts @@ -1 +1 @@ -/// \ No newline at end of file +/// diff --git a/client/filestore-client/package.json b/client/filestore-client/package.json index af78a85..7896917 100644 --- a/client/filestore-client/package.json +++ b/client/filestore-client/package.json @@ -29,7 +29,6 @@ "text" ], "report-dir": "coverage_report", - "check-coverage": true, "lines": 75, "functions": 100, "branches": 80