Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge 11a9721 into f25b881
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobueno committed Nov 10, 2017
2 parents f25b881 + 11a9721 commit b323b61
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 39 deletions.
11 changes: 8 additions & 3 deletions client/camera/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,7 +29,6 @@
"text"
],
"report-dir": "coverage_report",
"check-coverage": true,
"lines": 75,
"functions": 100,
"branches": 80
Expand All @@ -45,7 +44,13 @@
"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",
"@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"
Expand Down
4 changes: 2 additions & 2 deletions client/camera/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export interface CaptureResponse {
type optionsBuilderFn = (camera: any) => CameraOptions;

export class Camera {
protected initPromise: Promise<CameraOptions>;
protected options: CameraOptions;
public options: CameraOptions;
public initPromise: Promise<CameraOptions>;

constructor(optionsBuilderFunction?: optionsBuilderFn) {
this.init(optionsBuilderFunction);
Expand Down
57 changes: 57 additions & 0 deletions client/camera/test/Camera-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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() {
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() {
it('should call the cordova cleanup function', function() {
return subject.cleanup().then(function() {
sinon.assert.called(mockCordovaCamera.cleanup);
});
});
});
describe('capture', 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'
});
});
});
});
});
17 changes: 17 additions & 0 deletions client/camera/test/buildCameraOptions-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from 'chai';
import { buildCameraOptions } from '../src/buildCameraOptions';
import { mockCordovaCamera } from './mocks/camera';

describe('buildCameraOptions', function() {
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');
});
});
1 change: 1 addition & 0 deletions client/camera/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="mocha" />
27 changes: 27 additions & 0 deletions client/camera/test/mocks/camera.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { stub } from 'sinon';
export const mockCordovaCamera = {
// properties
DestinationType: {
FILE_URI: 1
},
EncodingType: {
JPEG: 1
},
PictureSourceType: {
CAMERA: 1
},
MediaType: {
PICTURE: 1
},
// methods
cleanup: stub().callsArgAsync(0),
getPicture: stub().callsArgWithAsync(0, 'some-uri')
};

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;
7 changes: 7 additions & 0 deletions client/camera/test/mocks/file.ts
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 7 additions & 8 deletions client/filestore-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,27 @@
"text"
],
"report-dir": "coverage_report",
"check-coverage": true,
"lines": 75,
"functions": 100,
"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"
Expand Down
2 changes: 1 addition & 1 deletion client/filestore-client/src/FileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class FileManager {
return Bluebird.resolve(result);
}).catch(function(err) {
// Adds a file to the upload queue if file is not available.
self.uploadQueue.addItem(file);
self.downloadQueue.addItem(file);
});
}
}
Expand Down
10 changes: 6 additions & 4 deletions client/filestore-client/src/FileQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { FileQueueEntry } from './FileQueueEntry';
*/
export class FileQueue {
private queueName: string;
private queueData: FileQueueEntry[] = [];
private queueData: FileQueueEntry[];

public constructor(private localStorage, name: string) {
this.queueName = name;
this.queueData = [];
}

/**
Expand All @@ -27,12 +28,13 @@ export class FileQueue {
* Restore the local queueData to reflect the data of the queue in localStorage.
*/
public restoreData() {
this.queueData = [];
const queueDataString = this.localStorage.getItem(this.queueName);
if (queueDataString) {
const queueData = JSON.parse(queueDataString);
this.queueData = queueData.queue;
} else {
this.queueData = [];
if (!_.isEmpty(queueData.queue)) {
this.queueData = queueData.queue;
}
}
return this;
}
Expand Down
35 changes: 16 additions & 19 deletions client/filestore-client/test/FileManagerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import * as chai from 'chai';
import * as chaiAsPromise from 'chai-as-promised';
import * as proxyquire from 'proxyquire';
import * as sinon from 'sinon';
import { FileManager } from '../src/FileManager';
import { FileQueue } from '../src/FileQueue';
import { FileQueueEntry } from '../src/FileQueueEntry';
import { HttpClient } from '../src/HttpClient';

declare var global: NodeJS.Global | any;

Expand All @@ -18,7 +20,7 @@ describe('File Manager Tests', function() {
};
const mockLocalStorage = {
setItem: sinon.stub(),
getItem: sinon.stub().returns('{}')
getItem: sinon.stub().returns('{"queue": []}')
};

const mockFileOnline: FileQueueEntry = {
Expand All @@ -36,22 +38,22 @@ describe('File Manager Tests', function() {
const fileCreatedMessage = 'mockFileCreated';
const fileNotCreatedMessage = 'mockFileNotCreated';

const mockUploadFile = function() {
const buildUploadFileMock = function() {
const stub = sinon.stub();
stub.withArgs(mockFileOnline).resolves(fileCreatedMessage);
stub.withArgs(mockFileOffline).rejects(fileNotCreatedMessage);
stub.rejects('offline');
return stub;
};

const CordovaFileSupport = function(serverUrl, HttpInterface) {
const CordovaFileSupportMock = function(serverUrl, HttpInterface) {
return {
downloadFileFromServer: sinon.stub(),
uploadFile: mockUploadFile()
uploadFile: buildUploadFileMock()
};
};

let testSubject;
let testSubject: FileManager;

// Mock DOM objects used by FileManager
global.document = {
Expand All @@ -61,30 +63,25 @@ describe('File Manager Tests', function() {
localStorage: mockLocalStorage
};

const FileManager = proxyquire.load('../src/FileManager', {
const FileManagerProxy: {
new(serverUrl: string, name: string, httpInterface: HttpClient): FileManager
} = proxyquire.load('../src/FileManager', {
'./CordovaFileSupport': {
'CordovaFileSupport': CordovaFileSupport
'CordovaFileSupport': CordovaFileSupportMock
}
}).FileManager;

beforeEach(function() {
testSubject = new FileManager(mockServerUrl, mockFileQueueName, mockHttpClient);
testSubject = new FileManagerProxy(mockServerUrl, mockFileQueueName, mockHttpClient);
});

it('should upload a file when the device is online', function(done) {
testSubject.scheduleFileToBeUploaded(mockFileOnline).then(function(result) {
it('should upload a file when the device is online', function() {
return testSubject.scheduleFileToBeUploaded(mockFileOnline).then(function(result) {
expect(result).to.equal(fileCreatedMessage);
done();
});
});

it('should not upload a file when the device is offline', function(done) {
testSubject.scheduleFileToBeUploaded(mockFileOffline).then(function(result) {
console.log('result', result);
done();
}).catch(function(err) {
console.log('err', err);
done();
});
it('should not upload a file when the device is offline', function() {
return testSubject.scheduleFileToBeUploaded(mockFileOffline);
});
});
8 changes: 6 additions & 2 deletions cloud/filestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b323b61

Please sign in to comment.