forked from noble/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-descriptor.js
133 lines (99 loc) · 3.41 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var should = require('should');
var sinon = require('sinon');
var Descriptor = require('../lib/descriptor');
describe('Descriptor', function() {
var mockNoble = null;
var mockPeripheralId = 'mock-peripheral-id';
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, mockPeripheralId, 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, mockPeripheralId, 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(mockPeripheralId, 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 not call callback twice', function() {
var calledback = 0;
descriptor.readValue(function() {
calledback += 1;
});
descriptor.emit('valueRead');
descriptor.emit('valueRead');
calledback.should.equal(1);
});
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(mockPeripheralId, 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);
});
it('should not call callback twice', function() {
var calledback = 0;
descriptor.writeValue(mockData, function() {
calledback += 1;
});
descriptor.emit('valueWrite');
descriptor.emit('valueWrite');
calledback.should.equal(1);
});
});
});