forked from noble/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-descriptor.js
108 lines (81 loc) · 2.88 KB
/
test-descriptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var should = require('should');
var sinon = require('sinon');
var Descriptor = require('../lib/descriptor');
describe('Descriptor', function() {
var mockNoble = null;
var mockPeripheralUuid = 'mock-peripheral-uuid';
var mockServiceUuid = 'mock-service-uuid';
var mockCharacteristicUuid = 'mock-characteristic-uuid';
var mockUuid = 'mock-uuid';
var descriptor = null;
beforeEach(function() {
mockNoble = {
readValue: sinon.spy(),
writeValue: sinon.spy()
};
descriptor = new Descriptor(mockNoble, mockPeripheralUuid, mockServiceUuid, mockCharacteristicUuid, mockUuid);
});
afterEach(function() {
descriptor = null;
});
it('should have a uuid', function() {
descriptor.uuid.should.equal(mockUuid);
});
it('should lookup name and type by uuid', function() {
descriptor = new Descriptor(mockNoble, mockPeripheralUuid, mockServiceUuid, mockCharacteristicUuid, '2900');
descriptor.name.should.equal('Characteristic Extended Properties');
descriptor.type.should.equal('org.bluetooth.descriptor.gatt.characteristic_extended_properties');
});
describe('toString', function() {
it('should be uuid, name, type', function() {
descriptor.toString().should.equal('{"uuid":"mock-uuid","name":null,"type":null}');
});
});
describe('readValue', function() {
it('should delegate to noble', function() {
descriptor.readValue();
mockNoble.readValue.calledWithExactly(mockPeripheralUuid, mockServiceUuid, mockCharacteristicUuid, mockUuid).should.equal(true);
});
it('should callback', function() {
var calledback = false;
descriptor.readValue(function() {
calledback = true;
});
descriptor.emit('valueRead');
calledback.should.equal(true);
});
it('should callback with error, data', function() {
var mockData = new Buffer(0);
var callbackData = null;
descriptor.readValue(function(error, data) {
callbackData = data;
});
descriptor.emit('valueRead', mockData);
callbackData.should.equal(mockData);
});
});
describe('writeValue', function() {
var mockData = null;
beforeEach(function() {
mockData = new Buffer(0);
});
it('should only accept data as a buffer', function() {
mockData = {};
(function(){
descriptor.writeValue(mockData);
}).should.throwError('data must be a Buffer');
});
it('should delegate to noble', function() {
descriptor.writeValue(mockData);
mockNoble.writeValue.calledWithExactly(mockPeripheralUuid, mockServiceUuid, mockCharacteristicUuid, mockUuid, mockData).should.equal(true);
});
it('should callback', function() {
var calledback = false;
descriptor.writeValue(mockData, function() {
calledback = true;
});
descriptor.emit('valueWrite');
calledback.should.equal(true);
});
});
});