Skip to content

Commit

Permalink
Add basic tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cierpliwy committed Jul 12, 2019
1 parent 7e77bd7 commit 0840129
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
65 changes: 65 additions & 0 deletions __tests__/BleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BleErrorCode, BleErrorCodeMessage } from '../src/BleError'
import * as Native from '../src/BleModule'

import { NativeEventEmitter } from './Utils'
import { Descriptor } from '../src/Descriptor'
Native.EventEmitter = NativeEventEmitter

/* eslint-disable no-unused-vars */
Expand Down Expand Up @@ -32,9 +33,12 @@ beforeEach(() => {
discoverAllServicesAndCharacteristicsForDevice: jest.fn(),
servicesForDevice: jest.fn(),
characteristicsForDevice: jest.fn(),
descriptorsForDevice: jest.fn(),
readCharacteristicForDevice: jest.fn(),
writeCharacteristicForDevice: jest.fn(),
monitorCharacteristicForDevice: jest.fn(),
readDescriptorForDevice: jest.fn(),
writeDescriptorForDevice: jest.fn(),
requestMTUForDevice: jest.fn(),
requestConnectionPriorityForDevice: jest.fn(),
ScanEvent: 'scan_event',
Expand Down Expand Up @@ -261,6 +265,19 @@ test('BleManager properly calls characteristicsForDevice BleModule function', as
expect(Native.BleModule.characteristicsForDevice).toBeCalledWith('id', 'aa')
})

test('BleManager properly calls descriptorsForDevice BleModule function', async () => {
Native.BleModule.descriptorsForDevice = jest
.fn()
.mockReturnValueOnce(Promise.resolve([{ uuid: 'a', deviceId: 'id' }, { uuid: 'b', deviceId: 'id' }]))
const descriptors = await bleManager.descriptorsForDevice('deviceId', 'serviceUUID', 'characteristicUUID')
expect(descriptors.length).toBe(2)
expect(descriptors[0]).toBeInstanceOf(Descriptor)
expect(descriptors[1]).toBeInstanceOf(Descriptor)
expect(descriptors[0].uuid).toBe('a')
expect(descriptors[1].uuid).toBe('b')
expect(Native.BleModule.descriptorsForDevice).toBeCalledWith('deviceId', 'serviceUUID', 'characteristicUUID')
})

test('BleManager properly reads characteristic value', async () => {
Native.BleModule.readCharacteristicForDevice = jest
.fn()
Expand Down Expand Up @@ -339,3 +356,51 @@ test('BleManager properly requests connection priority', async () => {
bleManager.requestConnectionPriorityForDevice('id', 2, 'trId')
expect(Native.BleModule.requestConnectionPriorityForDevice).toBeCalledWith('id', 2, 'trId')
})

test('BleManager properly reads descriptors value', async () => {
Native.BleModule.readDescriptorForDevice = jest
.fn()
.mockReturnValueOnce(Promise.resolve({ uuid: 'aaaa', value: '=AA' }))
const descriptor = await bleManager.readDescriptorForDevice(
'id',
'serviceUUID',
'characteristicUUID',
'descriptorUUID',
'trans'
)
expect(descriptor).toBeInstanceOf(Descriptor)
expect(descriptor.uuid).toBe('aaaa')
expect(descriptor.value).toBe('=AA')
expect(Native.BleModule.readDescriptorForDevice).toBeCalledWith(
'id',
'serviceUUID',
'characteristicUUID',
'descriptorUUID',
'trans'
)
})

test('BleManager properly writes descriptors value', async () => {
Native.BleModule.writeDescriptorForDevice = jest
.fn()
.mockReturnValueOnce(Promise.resolve({ uuid: 'aaaa', value: 'value' }))
const descriptor = await bleManager.writeDescriptorForDevice(
'id',
'serviceUUID',
'characteristicUUID',
'descriptorUUID',
'value',
'trans'
)
expect(descriptor).toBeInstanceOf(Descriptor)
expect(descriptor.uuid).toBe('aaaa')
expect(descriptor.value).toBe('value')
expect(Native.BleModule.writeDescriptorForDevice).toBeCalledWith(
'id',
'serviceUUID',
'characteristicUUID',
'descriptorUUID',
'value',
'trans'
)
})
15 changes: 15 additions & 0 deletions __tests__/Characteristic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ describe("Test if Characteristic is properly calling BleManager's utility functi
bleManager
)

test('descriptors', async () => {
await characteristic.descriptors()
expect(bleManager._descriptorsForCharacteristic).toBeCalledWith('cId')
})

test('read', async () => {
await characteristic.read('id')
expect(bleManager._readCharacteristic).toBeCalledWith('cId', 'id')
Expand All @@ -29,4 +34,14 @@ describe("Test if Characteristic is properly calling BleManager's utility functi
await characteristic.monitor(listener, 'id')
expect(bleManager._monitorCharacteristic).toBeCalledWith('cId', listener, 'id')
})

test('readDescriptor', async () => {
await characteristic.readDescriptor('uuid', 'transId')
expect(bleManager._readDescriptorForCharacteristic).toBeCalledWith('cId', 'uuid', 'transId')
})

test('writeDescriptor', async () => {
await characteristic.writeDescriptor('uuid', 'value', 'transId')
expect(bleManager._writeDescriptorForCharacteristic).toBeCalledWith('cId', 'uuid', 'value', 'transId')
})
})
27 changes: 27 additions & 0 deletions __tests__/Descriptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
jest.mock('../src/BleManager')
const { BleManager } = require('../src/BleManager')
const { Descriptor } = require('../src/Descriptor')

describe("Test if Descriptor is properly calling BleManager's utility function: ", () => {
const bleManager = new BleManager()
const descriptor = new Descriptor(
{
id: 'dId',
uuid: 'uuid',
characteristicUUID: 'characteristricUUID',
serviceUUID: 'serviceUUID',
deviceID: 'deviceId'
},
bleManager
)

test('read', async () => {
await descriptor.read('id')
expect(bleManager._readDescriptor).toBeCalledWith('dId', 'id')
})

test('write', async () => {
await descriptor.write('value', 'id')
expect(bleManager._writeDescriptor).toBeCalledWith('dId', 'value', 'id')
})
})
26 changes: 26 additions & 0 deletions __tests__/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ describe("Test if Service is properly calling BleManager's utility function: ",
expect(bleManager._characteristicsForService).toBeCalledWith('serviceId')
})

test('descriptorsForCharacteristic', async () => {
await service.descriptorsForCharacteristic('characteristicUUID')
expect(bleManager._descriptorsForService).toBeCalledWith('serviceId', 'characteristicUUID')
})

test('readCharacteristic', async () => {
await service.readCharacteristic('bbbb', 'id')
expect(bleManager._readCharacteristicForService).toBeCalledWith('serviceId', 'bbbb', 'id')
Expand All @@ -31,4 +36,25 @@ describe("Test if Service is properly calling BleManager's utility function: ",
await service.monitorCharacteristic('bbbb', listener, 'id')
expect(bleManager._monitorCharacteristicForService).toBeCalledWith('serviceId', 'bbbb', listener, 'id')
})

test('readDescriptorForCharacteristic', async () => {
await service.readDescriptorForCharacteristic('characteristicUUID', 'descriptorUUID', 'transactionId')
expect(bleManager._readDescriptorForService).toBeCalledWith(
'serviceId',
'characteristicUUID',
'descriptorUUID',
'transactionId'
)
})

test('writeDescriptorForCharacteristic', async () => {
await service.writeDescriptorForCharacteristic('characteristicUUID', 'descriptorUUID', 'value', 'transactionId')
expect(bleManager._writeDescriptorForService).toBeCalledWith(
'serviceId',
'characteristicUUID',
'descriptorUUID',
'value',
'transactionId'
)
})
})
2 changes: 0 additions & 2 deletions src/Descriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import type { BleManager } from './BleManager'
import type { NativeDescriptor } from './BleModule'
import type { DeviceId, Identifier, UUID, TransactionId, Base64 } from './TypeDefinition'

// "value": valueBase64 as Any

/**
* Descriptor object.
*/
Expand Down

0 comments on commit 0840129

Please sign in to comment.