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

Commit

Permalink
Improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bryfox committed Apr 13, 2018
1 parent 284b743 commit 55f5864
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
40 changes: 37 additions & 3 deletions src/main/data-managers/__tests__/ProtocolManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jest.mock('jszip');
jest.mock('../ProtocolDB');

describe('ProtocolManager', () => {
const mockFileContents = new Buffer([]);
const errorMessages = ProtocolManager.ErrorMessages;
let importer;
let invalidFileErr;
Expand All @@ -22,10 +23,35 @@ describe('ProtocolManager', () => {
});

describe('UI hook', () => {
const mockFileList = ['a.netcanvas'];

it('presents a dialog', () => {
importer.presentImportDialog();
expect(dialog.showOpenDialog).toHaveBeenCalled();
});

it('allows an import via the open dialog', (done) => {
const simulateChooseFile = (opts, callback) => {
callback(mockFileList);
expect(importer.validateAndImport).toHaveBeenCalled();
done();
};
importer.validateAndImport = jest.fn().mockReturnValue(Promise.resolve(mockFileList));
dialog.showOpenDialog.mockImplementation(simulateChooseFile);
importer.presentImportDialog();
});

it('allows dialog to be cancelled', (done) => {
expect.assertions(1);
const simulateChooseNothing = (opts, callback) => {
callback();
expect(importer.validateAndImport).not.toHaveBeenCalled();
done();
};
importer.validateAndImport = jest.fn();
dialog.showOpenDialog.mockImplementation(simulateChooseNothing);
importer.presentImportDialog();
});
});

describe('import interface', () => {
Expand Down Expand Up @@ -140,7 +166,6 @@ describe('ProtocolManager', () => {

it('returns raw content buffer', async () => {
expect.assertions(2);
const mockFileContents = new Buffer([]);
fs.readFile.mockImplementation((file, opts, cb) => {
const fn = typeof cb === 'undefined' ? opts : cb;
expect(opts.encoding).toBeUndefined();
Expand All @@ -164,9 +189,7 @@ describe('ProtocolManager', () => {
});

describe('post-processing', () => {
let mockFileContents;
beforeAll(() => {
mockFileContents = new Buffer([]);
fs.readFile.mockImplementation((file, cb) => cb(null, mockFileContents));
const mockProtocolZipObj = {
async: () => JSON.stringify({ name: 'myProtocol' }),
Expand All @@ -185,4 +208,15 @@ describe('ProtocolManager', () => {
expect(importer.db.save).toHaveBeenCalled();
});
});

describe('post-processing failures', () => {
beforeAll(() => {
fs.readFile.mockImplementation((file, cb) => cb(null, mockFileContents));
JSZip.loadAsync.mockImplementation(() => Promise.reject({}));
});

it('rejects if protocol cannot be read', async () => {
await expect(importer.postProcessFile('')).rejects.toMatchObject(invalidFileErr);
});
});
});
18 changes: 18 additions & 0 deletions src/main/worker/__tests__/DeviceAPI-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,23 @@ describe('the DeviceAPI', () => {
expect(res.json.data).toContainEqual({ downloadUrl: expectedUrl });
});
});

describe('GET /protocols/:filename', () => {
const mockFilename = 'a.netcanvas';
const mockFileContents = new Buffer(['a'.charCodeAt()]);
beforeAll(() => {
ProtocolManager.mockImplementation(() => ({
fileContents: () => Promise.resolve(mockFileContents),
}));
});

it('returns file data', async () => {
const res = await jsonClient.get(makeUrl(`/protocols/${mockFilename}`, deviceApi.server.url));
expect(res.statusCode).toBe(200);
// For the purposes of testing, compare string
expect(res.json).toBeUndefined();
expect(res.data).toEqual(mockFileContents.toString());
});
});
});
});
27 changes: 24 additions & 3 deletions src/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ const url = require('url');

enzyme.configure({ adapter: new Adapter() });

/**
* HTTP Client for asserting response body formats.
*
* Response contains a (parsed) json prop, for example:
*
* {
* statusCode: 200,
* json: {
* "status": "ok",
* "data": {
* "item": "data from server"
* }
* }
* }
*
* Also contains the raw (string) response body; not typically needed.
*/
const jsonClient = {
post: (uri, data) => jsonClient.request(uri, data),

Expand All @@ -20,13 +37,17 @@ const jsonClient = {
},
};
const req = http.request(options, (res) => {
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
const isJson = res.headers['content-type'] === 'application/json';
let stringData = '';
res.on('data', (chunk) => { stringData += chunk.toString(); });
res.on('end', () => {
const respBody = {
statusCode: res.statusCode,
json: JSON.parse(rawData),
data: stringData,
};
if (isJson) {
respBody.json = JSON.parse(stringData);
}
if (res.statusCode === 200) {
resolve(respBody);
} else {
Expand Down

0 comments on commit 55f5864

Please sign in to comment.