Unit Testing: > 90%

This commit is contained in:
Jeronimo Vallelunga 2017-07-23 10:52:15 -03:00
parent 90121621f3
commit a620b12b95
5 changed files with 198 additions and 3 deletions

View File

@ -11,3 +11,11 @@ test('encode / decode', () => {
expect(result).toEqual({ type: 'TEST', payload: 'PAYLOAD' });
});
test('encode / decode: string', () => {
const data = encode('{ "type": "TEST", "payload": "PAYLOAD" }');
const result = decode(data);
expect(result).toEqual({ type: 'TEST', payload: 'PAYLOAD' });
});

View File

@ -1,4 +1,4 @@
/* global jest, test, expect, beforeEach */
/* global jest, test, expect, beforeEach, afterEach */
import util from 'util';
import bleno from 'bleno';
@ -18,6 +18,11 @@ beforeEach(() => {
decode = jest.fn().mockReturnValue('mockDecode');
});
afterEach(() => {
encode = null;
decode = null;
});
test('new Characteristic', () => {
const characteristic = Characteristic('mockUUID', Parent, util, 'mockDescriptor', { encode, decode });

View File

@ -0,0 +1,165 @@
/* global jest, test, expect, beforeEach, afterEach */
import { BLENO_CONFIG } from '../../common/config';
import { Bleno } from './index';
let encode = null;
let decode = null;
let mockCharacteristic = null;
let BLENO = null;
jest.mock('./service', () => jest.fn().mockReturnValue('mockService'));
jest.mock('./characteristic', () => () => mockCharacteristic);
jest.mock('./descriptor', () => jest.fn());
beforeEach(() => {
encode = jest.fn().mockReturnValue('mockEncode');
decode = jest.fn().mockReturnValue('mockDecode');
mockCharacteristic = { updateState: jest.fn() };
BLENO = {
Descriptor: 'mockDescriptor',
Characteristic: 'mockCharacteristic',
startAdvertising: jest.fn(),
stopAdvertising: jest.fn(),
setServices: jest.fn(),
};
});
afterEach(() => {
encode = null;
decode = null;
mockCharacteristic = null;
BLENO = null;
});
test('Initialize', () => {
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
expect(Object.keys(bleno)).toEqual([
'start',
'handler',
'notify',
]);
});
test('start', () => {
BLENO.on = jest.fn();
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
bleno.start('mockName', 'mockState');
expect(BLENO.on).toHaveBeenCalledTimes(2);
});
test('start.stateChange: poweredOn > success', () => {
BLENO.startAdvertising = (name, services, callback) => {
expect(name).toBe('mockName');
expect(services).toEqual([BLENO_CONFIG.SERVICE_UUID]);
callback(false);
expect(mockCharacteristic.updateState).toHaveBeenCalledWith('mockStateStart');
};
const spyStartAdvertising = jest.spyOn(BLENO, 'startAdvertising');
BLENO.on = (name, callback) => {
if (name === 'stateChange') {
callback('poweredOn');
expect(spyStartAdvertising).toHaveBeenCalled();
}
};
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
bleno.start('mockName', 'mockStateStart');
});
test('start.stateChange: poweredOn > fail', () => {
BLENO.startAdvertising = (name, services, callback) => {
expect(name).toBe('mockName');
expect(services).toEqual([BLENO_CONFIG.SERVICE_UUID]);
callback(true);
expect(mockCharacteristic.updateState).not.toHaveBeenCalled();
};
const spyStartAdvertising = jest.spyOn(BLENO, 'startAdvertising');
BLENO.on = (name, callback) => {
if (name === 'stateChange') {
callback('poweredOn');
expect(spyStartAdvertising).toHaveBeenCalled();
}
};
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
bleno.start('mockName', 'mockStateStart');
});
// test('start.stateChange: poweredOn > fail', () => {
// BLENO.on = (name, callback) => {
// if (name === 'stateChange') {
// callback('poweredOn');
// expect(BLENO.startAdvertising).toHaveBeenCalled();
// }
// };
// const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
// bleno.start('mockName', 'mockState');
// });
test('start.stateChange: poweredOff', () => {
BLENO.on = (name, callback) => {
if (name === 'stateChange') {
callback('poweredOff');
expect(BLENO.stopAdvertising).toHaveBeenCalled();
}
};
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
bleno.start('mockName', 'mockState');
});
test('start.advertisingStart: success', () => {
BLENO.on = (name, callback) => {
if (name === 'advertisingStart') {
callback(false);
expect(BLENO.setServices).toHaveBeenCalledWith(['mockService']);
}
};
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
bleno.start('mockName', 'mockState');
});
test('start.advertisingStart: fail', () => {
BLENO.on = (name, callback) => {
if (name === 'advertisingStart') {
callback(true);
expect(BLENO.setServices).toHaveBeenCalledTimes(0);
}
};
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
bleno.start('mockName', 'mockState');
});
test('handler', () => {
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
expect(mockCharacteristic.onAction).not.toBeDefined();
bleno.handler('mockAction');
expect(mockCharacteristic.onAction).toBe('mockAction');
});
test('notify', () => {
const bleno = Bleno(BLENO, { encode, decode }, BLENO_CONFIG);
bleno.notify('mockState');
expect(mockCharacteristic.updateState).toHaveBeenCalledWith('mockState');
});

View File

@ -1,4 +1,4 @@
/* global jest, test, expect, beforeEach */
/* global jest, test, expect, beforeEach, afterEach */
import connectSyncStore from '.';
let store = null;
@ -18,6 +18,11 @@ beforeEach(() => {
};
});
afterEach(() => {
store = null;
bleno = null;
});
test('connectSyncStore', () => {
const { handleSubscribe, handleActions } = connectSyncStore(bleno)('mockName', store);

View File

@ -12,5 +12,17 @@ jest.mock('../reducers', () => jest.fn());
test('createSyncStore', () => {
const store = createSyncStore(['ACTION']);
return expect(store).toBe('mockStore');
expect(store).toBe('mockStore');
});
test('createSyncStore: debug', () => {
/* eslint-disable no-underscore-dangle */
global.__REDUX_DEVTOOLS_EXTENSION__ = jest.fn();
const store = createSyncStore(['ACTION']);
expect(store).toBe('mockStore');
expect(global.__REDUX_DEVTOOLS_EXTENSION__).toHaveBeenCalled();
/* eslint-enable */
});