From 6357a9dd61e6549aa839f57f37d861580dde5377 Mon Sep 17 00:00:00 2001 From: Jeronimo Vallelunga Date: Sun, 16 Jul 2017 16:00:45 -0300 Subject: [PATCH] Unit Test: increase --- README.md | 7 -- package.json | 2 +- src/peripheral/bleno/characteristic.js | 4 - src/peripheral/bleno/characteristic.test.js | 105 ++++++++++++++++++++ src/peripheral/bleno/descriptor.test.js | 13 +++ src/peripheral/bleno/service.test.js | 16 +++ 6 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 src/peripheral/bleno/characteristic.test.js create mode 100644 src/peripheral/bleno/descriptor.test.js create mode 100644 src/peripheral/bleno/service.test.js diff --git a/README.md b/README.md index 0d0771f..10dba5c 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,3 @@ PRs are welcome. To begin developing, do this: ```shell $ yarn install # or npm install ``` - -# TODO -- [ ] dotenv -- [ ] https://github.com/vadimdemedes/ink -- [x] bundlesize -- [ ] Prepack -- [ ] https://greenkeeper.io/ diff --git a/package.json b/package.json index 55e53e6..ddd7caf 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "pretest:bundlesize": "npm run uglify", "posttest:bundlesize": "rm -fR .tmp bundle.js", "test:watch": "npm test -- --watch", - "test:coverage": "npm test -- --coverage", + "test:coverage": "npm test -- --coverage --forceExit", "prepublish": "npm run build", "release": "np", "example:install": "npm run example:webapp:install", diff --git a/src/peripheral/bleno/characteristic.js b/src/peripheral/bleno/characteristic.js index dfb5802..0900135 100644 --- a/src/peripheral/bleno/characteristic.js +++ b/src/peripheral/bleno/characteristic.js @@ -29,10 +29,6 @@ export default function Characteristic(uuid, Parent, util, descriptor, { encode, return; } - if (!this.store) { - callback(this.RESULT_SUCCESS, null); - return; - } callback(this.RESULT_SUCCESS, this.state); }; diff --git a/src/peripheral/bleno/characteristic.test.js b/src/peripheral/bleno/characteristic.test.js new file mode 100644 index 0000000..3c3baee --- /dev/null +++ b/src/peripheral/bleno/characteristic.test.js @@ -0,0 +1,105 @@ +/* global jest, test, expect, beforeEach */ +import util from 'util'; +import bleno from 'bleno'; + +import Characteristic from './characteristic'; + +function Parent({ uuid, properties, descriptors }) { + this.uuid = uuid; + this.properties = properties; + this.descriptors = descriptors; +} + +let encode = null; +let decode = null; + +beforeEach(() => { + encode = jest.fn().mockReturnValue('mockEncode'); + decode = jest.fn().mockReturnValue('mockDecode'); +}); + +test('new Characteristic', () => { + const characteristic = Characteristic('mockUUID', Parent, util, 'mockDescriptor', { encode, decode }); + + expect(characteristic.uuid).toBe('mockUUID'); + expect(characteristic.properties).toEqual(['read', 'write', 'notify']); + expect(characteristic.descriptors).toEqual(['mockDescriptor']); + expect(characteristic.state).toBe(null); +}); + +test('Characteristic.onWriteRequest: RESULT_ATTR_NOT_LONG', () => { + const characteristic = Characteristic('mockUUID', bleno.Characteristic, util, 'mockDescriptor', { encode, decode }); + + const callback = jest.fn(); + characteristic.onWriteRequest(null, true, false, callback); + + expect(callback).toBeCalledWith(bleno.Characteristic.RESULT_ATTR_NOT_LONG); +}); + + +test('Characteristic.onWriteRequest: RESULT_SUCCESS', () => { + let callback = null; + const characteristic = Characteristic('mockUUID', bleno.Characteristic, util, 'mockDescriptor', { encode, decode }); + + callback = jest.fn(); + const spyOnAction = jest.spyOn(characteristic, 'onAction'); + characteristic.onWriteRequest(null, false, false, callback); + + expect(spyOnAction).toBeCalledWith('mockDecode'); + expect(callback).toBeCalledWith(bleno.Characteristic.RESULT_SUCCESS); + + callback = jest.fn(); + characteristic.onAction = jest.fn(); + characteristic.onWriteRequest(null, false, false, callback); + + expect(characteristic.onAction).toBeCalledWith('mockDecode'); + expect(callback).toBeCalledWith(bleno.Characteristic.RESULT_SUCCESS); +}); + +test('Characteristic.onReadRequest: RESULT_ATTR_NOT_LONG', () => { + const characteristic = Characteristic('mockUUID', bleno.Characteristic, util, 'mockDescriptor', { encode, decode }); + + const callback = jest.fn(); + characteristic.onReadRequest(true, callback); + + expect(callback).toBeCalledWith(bleno.Characteristic.RESULT_ATTR_NOT_LONG, null); +}); + +test('Characteristic.onReadRequest: RESULT_SUCCESS', () => { + const characteristic = Characteristic('mockUUID', bleno.Characteristic, util, 'mockDescriptor', { encode, decode }); + + const callback = jest.fn(); + characteristic.onReadRequest(false, callback); + + expect(callback).toBeCalledWith(bleno.Characteristic.RESULT_SUCCESS, characteristic.state); +}); + +test('Characteristic.onSubscribe', () => { + const characteristic = Characteristic('mockUUID', bleno.Characteristic, util, 'mockDescriptor', { encode, decode }); + + characteristic.onSubscribe(null, 'mockUpdateValueCallback'); + + expect(characteristic.updateValueCallback).toBe('mockUpdateValueCallback'); +}); + +test('Characteristic.onUnsubscribe', () => { + const characteristic = Characteristic('mockUUID', bleno.Characteristic, util, 'mockDescriptor', { encode, decode }); + + characteristic.onSubscribe(null, 'mockUpdateValueCallback'); + characteristic.onUnsubscribe(); + + expect(characteristic.updateValueCallback).toBe(null); +}); + +test('Characteristic.updateState', () => { + const characteristic = Characteristic('mockUUID', bleno.Characteristic, util, 'mockDescriptor', { encode, decode }); + + characteristic.updateState('mockState'); + expect(characteristic.state).toBe('mockEncode'); + + const updateValueCallback = jest.fn(); + characteristic.onSubscribe(null, updateValueCallback); + characteristic.updateState('mockState'); + + expect(updateValueCallback).toBeCalledWith('mockEncode'); +}); diff --git a/src/peripheral/bleno/descriptor.test.js b/src/peripheral/bleno/descriptor.test.js new file mode 100644 index 0000000..1907fc0 --- /dev/null +++ b/src/peripheral/bleno/descriptor.test.js @@ -0,0 +1,13 @@ +/* global test, expect, */ +import Descriptor from './descriptor'; + +function Parent({ uuid, value }) { + return { uuid, value }; +} + +test('descriptor', () => { + const descriptor = Descriptor('mockUUID', Parent); + + expect(descriptor.uuid).toBe('mockUUID'); + expect(descriptor.value).toBe('Redux Characteristic.'); +}); diff --git a/src/peripheral/bleno/service.test.js b/src/peripheral/bleno/service.test.js new file mode 100644 index 0000000..8a3d881 --- /dev/null +++ b/src/peripheral/bleno/service.test.js @@ -0,0 +1,16 @@ +/* global test, expect, */ +import util from 'util'; + +import Service from './service'; + +function Parent({ uuid, characteristics }) { + this.uuid = uuid; + this.characteristics = characteristics; +} + +test('service', () => { + const service = Service('mockUUID', Parent, util, 'mockCharacteristic'); + + expect(service.uuid).toBe('mockUUID'); + expect(service.characteristics).toEqual(['mockCharacteristic']); +});