This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RAINCATCH-1386][WIP] Add unit tests for FileStore Client (#181)
* add tests for fileQueue * add fileManager tests
- Loading branch information
Showing
6 changed files
with
180 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,5 @@ export interface FileQueueEntry { | |
/** | ||
* File identifier | ||
*/ | ||
id?: string; | ||
id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import * as chai from 'chai'; | ||
import * as chaiAsPromise from 'chai-as-promised'; | ||
import * as proxyquire from 'proxyquire'; | ||
import * as sinon from 'sinon'; | ||
import { FileQueue } from '../src/FileQueue'; | ||
import { FileQueueEntry } from '../src/FileQueueEntry'; | ||
|
||
declare var global: NodeJS.Global | any; | ||
|
||
const { expect } = chai; | ||
|
||
describe('File Manager Tests', function() { | ||
const mockServerUrl = 'mockServerUrl'; | ||
const mockFileQueueName = 'mockQueueName'; | ||
const mockHttpClient = { | ||
upload: sinon.stub(), | ||
download: sinon.stub() | ||
}; | ||
const mockLocalStorage = { | ||
setItem: sinon.stub(), | ||
getItem: sinon.stub().returns('{}') | ||
}; | ||
|
||
const mockFileOnline: FileQueueEntry = { | ||
uri: 'mockURIOnline', | ||
type: 'uri', | ||
id: 'mockFileIdOnline' | ||
}; | ||
|
||
const mockFileOffline: FileQueueEntry = { | ||
uri: 'mockURIOffline', | ||
type: 'uri', | ||
id: 'mockFileIdOffline' | ||
}; | ||
|
||
const fileCreatedMessage = 'mockFileCreated'; | ||
const fileNotCreatedMessage = 'mockFileNotCreated'; | ||
|
||
const mockUploadFile = 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) { | ||
return { | ||
downloadFileFromServer: sinon.stub(), | ||
uploadFile: mockUploadFile() | ||
}; | ||
}; | ||
|
||
let testSubject; | ||
|
||
// Mock DOM objects used by FileManager | ||
global.document = { | ||
addEventListener: sinon.stub() | ||
}; | ||
global.window = { | ||
localStorage: mockLocalStorage | ||
}; | ||
|
||
const FileManager = proxyquire.load('../src/FileManager', { | ||
'./CordovaFileSupport': { | ||
'CordovaFileSupport': CordovaFileSupport | ||
} | ||
}).FileManager; | ||
|
||
beforeEach(function() { | ||
testSubject = new FileManager(mockServerUrl, mockFileQueueName, mockHttpClient); | ||
}); | ||
|
||
it('should upload a file when the device is online', function(done) { | ||
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as chai from 'chai'; | ||
import * as sinon from 'sinon'; | ||
import { FileQueue } from '../src/FileQueue'; | ||
import { FileQueueEntry } from '../src/FileQueueEntry'; | ||
|
||
const { expect } = chai; | ||
|
||
describe('File Queue Tests', function() { | ||
const mockQueue = '{"queue": [{"uri": "mockURI", "type": "uri", "id": "mockID"}]}'; | ||
const queueName = 'testFileQueue'; | ||
const mockFileQueueEntry: FileQueueEntry = { | ||
uri: 'mockURI', | ||
type: 'uri', | ||
id: 'mockID' | ||
}; | ||
let testSubject; | ||
let mockLocalStorage; | ||
|
||
beforeEach(function() { | ||
mockLocalStorage = { | ||
getItem: sinon.stub().returns(mockQueue), | ||
setItem: sinon.stub() | ||
}; | ||
testSubject = new FileQueue(mockLocalStorage, queueName); | ||
}); | ||
|
||
it('should be able to add an item to the queue', function() { | ||
const expectedQueueList: FileQueueEntry[] = []; | ||
expectedQueueList.push(mockFileQueueEntry); | ||
testSubject.addItem(mockFileQueueEntry); | ||
|
||
expect(testSubject.getItemList()).to.deep.equal(expectedQueueList); | ||
sinon.assert.calledOnce(mockLocalStorage.setItem); | ||
sinon.assert.calledWith(mockLocalStorage.setItem, queueName, JSON.stringify({queue: expectedQueueList})); | ||
}); | ||
|
||
it('should be able to remove an item from the queue', function() { | ||
testSubject.addItem(mockFileQueueEntry); | ||
const expectedQueueList: FileQueueEntry[] = []; | ||
testSubject.removeItem(mockFileQueueEntry); | ||
|
||
expect(testSubject.getItemList()).to.deep.equal(expectedQueueList); | ||
sinon.assert.calledWith(mockLocalStorage.setItem, queueName, JSON.stringify({queue: expectedQueueList})); | ||
}); | ||
|
||
it('should be able to read an item from the queue', function() { | ||
testSubject.addItem(mockFileQueueEntry); | ||
|
||
expect(testSubject.readItem(mockFileQueueEntry.id)).to.deep.equal(mockFileQueueEntry); | ||
}); | ||
|
||
it('should restore the local queue data to reflect the queue in local storage', function() { | ||
const expectedQueueList: FileQueueEntry[] = []; | ||
expectedQueueList.push(mockFileQueueEntry); | ||
|
||
expect(testSubject.restoreData().getItemList()).to.deep.equal(expectedQueueList); | ||
sinon.assert.calledOnce(mockLocalStorage.getItem); | ||
sinon.assert.calledWith(mockLocalStorage.getItem, queueName); | ||
}); | ||
|
||
it('should restore the queue data to an empty array if the queue in the local storage is empty', function() { | ||
mockLocalStorage = { | ||
getItem: sinon.stub().returns(undefined), | ||
setItem: sinon.stub() | ||
}; | ||
testSubject = new FileQueue(mockLocalStorage, queueName); | ||
|
||
expect(testSubject.restoreData().getItemList()).to.deep.equal([]); | ||
sinon.assert.calledOnce(mockLocalStorage.getItem); | ||
sinon.assert.calledWith(mockLocalStorage.getItem, queueName); | ||
}); | ||
}); |