forked from noble/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-service.js
120 lines (88 loc) · 3.49 KB
/
test-service.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
var should = require('should');
var sinon = require('sinon');
var Service = require('../lib/service');
describe('service', function() {
var mockNoble = null;
var mockPeripheralId = 'mock-peripheral-id';
var mockUuid = 'mock-uuid';
var service = null;
beforeEach(function() {
mockNoble = {
discoverIncludedServices: sinon.spy(),
discoverCharacteristics: sinon.spy()
};
service = new Service(mockNoble, mockPeripheralId, mockUuid);
});
afterEach(function() {
service = null;
});
it('should have a uuid', function() {
service.uuid.should.equal(mockUuid);
});
it('should lookup name and type by uuid', function() {
service = new Service(mockNoble, mockPeripheralId, '1800');
service.name.should.equal('Generic Access');
service.type.should.equal('org.bluetooth.service.generic_access');
});
describe('toString', function() {
it('should be uuid, name, type, includedServiceUuids', function() {
service.toString().should.equal('{"uuid":"mock-uuid","name":null,"type":null,"includedServiceUuids":null}');
});
});
describe('discoverIncludedServices', function() {
it('should delegate to noble', function() {
service.discoverIncludedServices();
mockNoble.discoverIncludedServices.calledWithExactly(mockPeripheralId, mockUuid, undefined).should.equal(true);
});
it('should delegate to noble, with uuids', function() {
var mockUuids = [];
service.discoverIncludedServices(mockUuids);
mockNoble.discoverIncludedServices.calledWithExactly(mockPeripheralId, mockUuid, mockUuids).should.equal(true);
});
it('should callback', function() {
var calledback = false;
service.discoverIncludedServices(null, function() {
calledback = true;
});
service.emit('includedServicesDiscover');
calledback.should.equal(true);
});
it('should callback with data', function() {
var mockIncludedServiceUuids = [];
var callbackIncludedServiceUuids = null;
service.discoverIncludedServices(null, function(error, includedServiceUuids) {
callbackIncludedServiceUuids = includedServiceUuids;
});
service.emit('includedServicesDiscover', mockIncludedServiceUuids);
callbackIncludedServiceUuids.should.equal(mockIncludedServiceUuids);
});
});
describe('discoverCharacteristics', function() {
it('should delegate to noble', function() {
service.discoverCharacteristics();
mockNoble.discoverCharacteristics.calledWithExactly(mockPeripheralId, mockUuid, undefined).should.equal(true);
});
it('should delegate to noble, with uuids', function() {
var mockUuids = [];
service.discoverCharacteristics(mockUuids);
mockNoble.discoverCharacteristics.calledWithExactly(mockPeripheralId, mockUuid, mockUuids).should.equal(true);
});
it('should callback', function() {
var calledback = false;
service.discoverCharacteristics(null, function() {
calledback = true;
});
service.emit('characteristicsDiscover');
calledback.should.equal(true);
});
it('should callback with data', function() {
var mockCharacteristics = [];
var callbackCharacteristics = null;
service.discoverCharacteristics(null, function(error, mockCharacteristics) {
callbackCharacteristics = mockCharacteristics;
});
service.emit('characteristicsDiscover', mockCharacteristics);
callbackCharacteristics.should.equal(mockCharacteristics);
});
});
});