@stoprocent/noble 1.19.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.md +393 -650
  2. package/examples/advertisement-discovery.js +57 -48
  3. package/examples/connect-address.js +59 -34
  4. package/examples/echo.js +59 -69
  5. package/examples/enter-exit.js +55 -49
  6. package/examples/multiple-bindings.js +53 -0
  7. package/examples/peripheral-explorer-async.js +39 -21
  8. package/examples/peripheral-explorer.ts +52 -0
  9. package/index.d.ts +249 -209
  10. package/index.js +4 -1
  11. package/jest.config.js +4 -0
  12. package/lib/characteristic.js +153 -127
  13. package/lib/{win/src/callbacks.h → common/include/Emit.h} +17 -14
  14. package/lib/common/include/Peripheral.h +31 -0
  15. package/lib/common/include/ThreadSafeCallback.h +95 -0
  16. package/lib/{win/src/callbacks.cc → common/src/Emit.cc} +111 -68
  17. package/lib/descriptor.js +57 -54
  18. package/lib/hci-socket/acl-stream.js +2 -4
  19. package/lib/hci-socket/bindings.js +96 -73
  20. package/lib/hci-socket/gap.js +2 -3
  21. package/lib/hci-socket/gatt.js +2 -5
  22. package/lib/hci-socket/hci.js +19 -7
  23. package/lib/hci-socket/signaling.js +2 -3
  24. package/lib/hci-socket/smp.js +2 -3
  25. package/lib/hci-socket/vs.js +1 -0
  26. package/lib/mac/binding.gyp +5 -7
  27. package/lib/mac/bindings.js +1 -3
  28. package/lib/mac/src/ble_manager.h +1 -8
  29. package/lib/mac/src/ble_manager.mm +87 -44
  30. package/lib/mac/src/napi_objc.h +1 -0
  31. package/lib/mac/src/napi_objc.mm +0 -6
  32. package/lib/mac/src/noble_mac.h +5 -3
  33. package/lib/mac/src/noble_mac.mm +99 -57
  34. package/lib/mac/src/objc_cpp.h +3 -2
  35. package/lib/mac/src/objc_cpp.mm +0 -6
  36. package/lib/noble.js +579 -488
  37. package/lib/peripheral.js +171 -174
  38. package/lib/resolve-bindings.js +37 -30
  39. package/lib/service.js +58 -55
  40. package/lib/win/binding.gyp +4 -11
  41. package/lib/win/bindings.js +1 -3
  42. package/lib/win/src/ble_manager.cc +291 -166
  43. package/lib/win/src/ble_manager.h +11 -13
  44. package/lib/win/src/napi_winrt.cc +1 -7
  45. package/lib/win/src/napi_winrt.h +1 -1
  46. package/lib/win/src/noble_winrt.cc +88 -61
  47. package/lib/win/src/noble_winrt.h +5 -3
  48. package/lib/win/src/notify_map.cc +0 -7
  49. package/lib/win/src/notify_map.h +1 -8
  50. package/lib/win/src/peripheral_winrt.cc +29 -11
  51. package/lib/win/src/peripheral_winrt.h +1 -1
  52. package/lib/win/src/radio_watcher.cc +79 -69
  53. package/lib/win/src/radio_watcher.h +30 -11
  54. package/lib/win/src/winrt_cpp.cc +1 -1
  55. package/lib/win/src/winrt_cpp.h +3 -0
  56. package/package.json +14 -17
  57. package/prebuilds/darwin-x64+arm64/@stoprocent+noble.node +0 -0
  58. package/prebuilds/win32-ia32/@stoprocent+noble.node +0 -0
  59. package/prebuilds/win32-x64/@stoprocent+noble.node +0 -0
  60. package/test/lib/characteristic.test.js +202 -322
  61. package/test/lib/descriptor.test.js +62 -95
  62. package/test/lib/hci-socket/acl-stream.test.js +112 -108
  63. package/test/lib/hci-socket/bindings.test.js +576 -365
  64. package/test/lib/hci-socket/hci.test.js +442 -473
  65. package/test/lib/hci-socket/signaling.test.js +45 -48
  66. package/test/lib/hci-socket/smp.test.js +144 -142
  67. package/test/lib/hci-socket/vs.test.js +193 -18
  68. package/test/lib/peripheral.test.js +492 -322
  69. package/test/lib/resolve-bindings.test.js +207 -82
  70. package/test/lib/service.test.js +79 -88
  71. package/test/noble.test.js +381 -1085
  72. package/.editorconfig +0 -11
  73. package/.nycrc.json +0 -4
  74. package/codecov.yml +0 -5
  75. package/examples/cache-gatt-discovery.js +0 -198
  76. package/examples/cache-gatt-reconnect.js +0 -164
  77. package/examples/ext-advertisement-discovery.js +0 -65
  78. package/examples/peripheral-explorer.js +0 -225
  79. package/examples/pizza/central.js +0 -194
  80. package/examples/pizza/pizza.js +0 -60
  81. package/examples/test/test.custom.js +0 -131
  82. package/examples/uart-bind-params.js +0 -28
  83. package/lib/distributed/bindings.js +0 -326
  84. package/lib/mac/src/callbacks.cc +0 -222
  85. package/lib/mac/src/callbacks.h +0 -84
  86. package/lib/mac/src/peripheral.h +0 -23
  87. package/lib/resolve-bindings-web.js +0 -9
  88. package/lib/webbluetooth/bindings.js +0 -368
  89. package/lib/websocket/bindings.js +0 -321
  90. package/lib/win/src/peripheral.h +0 -23
  91. package/test/lib/distributed/bindings.test.js +0 -918
  92. package/test/lib/webbluetooth/bindings.test.js +0 -190
  93. package/test/lib/websocket/bindings.test.js +0 -456
  94. package/test/mocha.setup.js +0 -0
  95. package/with-bindings.js +0 -5
  96. package/with-custom-binding.js +0 -6
  97. package/ws-slave.js +0 -404
@@ -1,8 +1,3 @@
1
- const sinon = require('sinon');
2
- const should = require('should');
3
-
4
- const { assert } = sinon;
5
-
6
1
  const Characteristic = require('../../lib/characteristic');
7
2
 
8
3
  describe('characteristic', () => {
@@ -15,7 +10,16 @@ describe('characteristic', () => {
15
10
  let characteristic = null;
16
11
 
17
12
  beforeEach(() => {
18
- mockNoble = {};
13
+ mockNoble = {
14
+ _withDisconnectHandler: (id, operation) => {
15
+ return operation();
16
+ },
17
+ read: jest.fn(),
18
+ write: jest.fn(),
19
+ broadcast: jest.fn(),
20
+ notify: jest.fn(),
21
+ discoverDescriptors: jest.fn()
22
+ };
19
23
 
20
24
  characteristic = new Characteristic(
21
25
  mockNoble,
@@ -26,11 +30,15 @@ describe('characteristic', () => {
26
30
  );
27
31
  });
28
32
 
29
- it('should have a uuid', () => {
30
- should(characteristic.uuid).equal(mockUuid);
33
+ afterEach(() => {
34
+ jest.clearAllMocks();
31
35
  });
32
36
 
33
- it('should lookup name and type by uuid', () => {
37
+ test('should have a uuid', () => {
38
+ expect(characteristic.uuid).toEqual(mockUuid);
39
+ });
40
+
41
+ test('should lookup name and type by uuid', () => {
34
42
  characteristic = new Characteristic(
35
43
  mockNoble,
36
44
  mockPeripheralId,
@@ -39,62 +47,55 @@ describe('characteristic', () => {
39
47
  mockProperties
40
48
  );
41
49
 
42
- should(characteristic.name).equal('Device Name');
43
- should(characteristic.type).equal(
50
+ expect(characteristic.name).toEqual('Device Name');
51
+ expect(characteristic.type).toEqual(
44
52
  'org.bluetooth.characteristic.gap.device_name'
45
53
  );
46
54
  });
47
55
 
48
- it('should have properties', () => {
49
- should(characteristic.properties).equal(mockProperties);
56
+ test('should have properties', () => {
57
+ expect(characteristic.properties).toEqual(mockProperties);
50
58
  });
51
59
 
52
60
  describe('toString', () => {
53
- it('should be uuid, name, type, properties', () => {
54
- should(characteristic.toString()).equal(
61
+ test('should be uuid, name, type, properties', () => {
62
+ expect(characteristic.toString()).toEqual(
55
63
  '{"uuid":"mock-uuid","name":null,"type":null,"properties":["mock-property-1","mock-property-2"]}'
56
64
  );
57
65
  });
58
66
  });
59
67
 
60
68
  describe('read', () => {
61
- beforeEach(() => {
62
- mockNoble.read = sinon.spy();
63
- });
64
-
65
- afterEach(() => {
66
- sinon.reset();
67
- });
68
-
69
- it('should delegate to noble', () => {
69
+ test('should delegate to noble', () => {
70
70
  characteristic.read();
71
- assert.calledOnceWithExactly(
72
- mockNoble.read,
71
+ expect(mockNoble.read).toHaveBeenCalledWith(
73
72
  mockPeripheralId,
74
73
  mockServiceUuid,
75
74
  mockUuid
76
75
  );
76
+ expect(mockNoble.read).toHaveBeenCalledTimes(1);
77
77
  });
78
78
 
79
- it('should callback without data', () => {
80
- const callback = sinon.spy();
79
+ test('should callback without data', () => {
80
+ const callback = jest.fn();
81
81
 
82
82
  characteristic.read(callback);
83
83
  characteristic.emit('read');
84
84
  // Check for single callback
85
85
  characteristic.emit('read');
86
86
 
87
- assert.calledOnceWithExactly(callback, null, undefined);
88
- assert.calledOnceWithExactly(
89
- mockNoble.read,
87
+ expect(callback).toHaveBeenCalledWith(undefined, undefined);
88
+ expect(callback).toHaveBeenCalledTimes(1);
89
+ expect(mockNoble.read).toHaveBeenCalledWith(
90
90
  mockPeripheralId,
91
91
  mockServiceUuid,
92
92
  mockUuid
93
93
  );
94
+ expect(mockNoble.read).toHaveBeenCalledTimes(1);
94
95
  });
95
96
 
96
- it('should callback with data', () => {
97
- const callback = sinon.spy();
97
+ test('should callback with data', () => {
98
+ const callback = jest.fn();
98
99
  const data = 'data';
99
100
 
100
101
  characteristic.read(callback);
@@ -102,17 +103,18 @@ describe('characteristic', () => {
102
103
  // Check for single callback
103
104
  characteristic.emit('read', data);
104
105
 
105
- assert.calledOnceWithExactly(callback, null, data);
106
- assert.calledOnceWithExactly(
107
- mockNoble.read,
106
+ expect(callback).toHaveBeenCalledWith(undefined, data);
107
+ expect(callback).toHaveBeenCalledTimes(1);
108
+ expect(mockNoble.read).toHaveBeenCalledWith(
108
109
  mockPeripheralId,
109
110
  mockServiceUuid,
110
111
  mockUuid
111
112
  );
113
+ expect(mockNoble.read).toHaveBeenCalledTimes(1);
112
114
  });
113
115
 
114
- it('should not callback as it is notification', () => {
115
- const callback = sinon.spy();
116
+ test('should not callback as it is notification', () => {
117
+ const callback = jest.fn();
116
118
  const data = 'data';
117
119
 
118
120
  characteristic.read(callback);
@@ -120,616 +122,505 @@ describe('characteristic', () => {
120
122
  // Check for single callback
121
123
  characteristic.emit('read', data, true);
122
124
 
123
- assert.notCalled(callback);
124
- assert.calledOnceWithExactly(
125
- mockNoble.read,
125
+ expect(callback).not.toHaveBeenCalled();
126
+ expect(mockNoble.read).toHaveBeenCalledWith(
126
127
  mockPeripheralId,
127
128
  mockServiceUuid,
128
129
  mockUuid
129
130
  );
131
+ expect(mockNoble.read).toHaveBeenCalledTimes(1);
130
132
  });
131
133
  });
132
134
 
133
135
  describe('readAsync', () => {
134
- beforeEach(() => {
135
- mockNoble.read = sinon.spy();
136
- });
137
-
138
- afterEach(() => {
139
- sinon.reset();
140
- });
141
-
142
- it('should delegate to noble', async () => {
136
+ test('should delegate to noble', async () => {
143
137
  const promise = characteristic.readAsync();
144
138
  characteristic.emit('read');
145
- // Check for single callback
146
- characteristic.emit('read');
147
-
148
- should(promise).resolvedWith(undefined);
149
- assert.calledOnceWithExactly(
150
- mockNoble.read,
139
+
140
+ await expect(promise).resolves.toBeUndefined();
141
+ expect(mockNoble.read).toHaveBeenCalledWith(
151
142
  mockPeripheralId,
152
143
  mockServiceUuid,
153
144
  mockUuid
154
145
  );
146
+ expect(mockNoble.read).toHaveBeenCalledTimes(1);
155
147
  });
156
148
 
157
- it('should returns without data', async () => {
149
+ test('should returns without data', async () => {
158
150
  const promise = characteristic.readAsync();
159
151
  characteristic.emit('read');
160
- // Check for single callback
161
- characteristic.emit('read');
162
-
163
- should(promise).resolvedWith(undefined);
164
- assert.calledOnceWithExactly(
165
- mockNoble.read,
152
+
153
+ await expect(promise).resolves.toBeUndefined();
154
+ expect(mockNoble.read).toHaveBeenCalledWith(
166
155
  mockPeripheralId,
167
156
  mockServiceUuid,
168
157
  mockUuid
169
158
  );
159
+ expect(mockNoble.read).toHaveBeenCalledTimes(1);
170
160
  });
171
161
 
172
- it('should callback with data', async () => {
162
+ test('should callback with data', async () => {
173
163
  const data = 'data';
174
164
 
175
165
  const promise = characteristic.readAsync();
176
166
  characteristic.emit('read', data);
177
- // Check for single callback
178
- characteristic.emit('read', data);
179
-
180
- should(promise).resolvedWith(data);
181
- assert.calledOnceWithExactly(
182
- mockNoble.read,
167
+
168
+ await expect(promise).resolves.toEqual(data);
169
+ expect(mockNoble.read).toHaveBeenCalledWith(
183
170
  mockPeripheralId,
184
171
  mockServiceUuid,
185
172
  mockUuid
186
173
  );
174
+ expect(mockNoble.read).toHaveBeenCalledTimes(1);
187
175
  });
188
176
 
189
177
  // This shows that async notification never ends
190
- it.skip('should not callback as it is notification', async () => {
178
+ test.skip('should not callback as it is notification', async () => {
191
179
  const data = 'data';
192
180
 
193
181
  const promise = characteristic.readAsync();
194
182
  characteristic.emit('read', data, true);
195
- // Check for single callback
196
- characteristic.emit('read', data, true);
197
-
198
- should(promise).resolvedWith(undefined);
199
- assert.calledOnceWithExactly(
200
- mockNoble.read,
183
+
184
+ await expect(promise).resolves.toBeUndefined();
185
+ expect(mockNoble.read).toHaveBeenCalledWith(
201
186
  mockPeripheralId,
202
187
  mockServiceUuid,
203
188
  mockUuid
204
189
  );
190
+ expect(mockNoble.read).toHaveBeenCalledTimes(1);
205
191
  });
206
192
  });
207
193
 
208
194
  describe('write', () => {
209
195
  let processTitle = null;
196
+
210
197
  beforeEach(() => {
211
- mockNoble.write = sinon.spy();
212
198
  processTitle = process.title;
213
199
  });
214
200
 
215
201
  afterEach(() => {
216
- sinon.reset();
217
202
  process.title = processTitle;
218
203
  });
219
204
 
220
- it('should only accept data as a buffer', () => {
221
- should(() => characteristic.write({})).throw(
205
+ test('should only accept data as a buffer', () => {
206
+ expect(() => characteristic.write({})).toThrow(
222
207
  'data must be a Buffer or Uint8Array or Uint16Array or Uint32Array'
223
208
  );
224
209
 
225
- assert.notCalled(mockNoble.write);
210
+ expect(mockNoble.write).not.toHaveBeenCalled();
226
211
  });
227
212
 
228
- it('should accept any kind of data as process is browser', () => {
213
+ test('should accept any kind of data as process is browser', () => {
229
214
  process.title = 'browser';
230
215
 
231
216
  const mockData = {};
232
217
  characteristic.write(mockData);
233
218
 
234
- assert.calledOnceWithExactly(
235
- mockNoble.write,
219
+ expect(mockNoble.write).toHaveBeenCalledWith(
236
220
  mockPeripheralId,
237
221
  mockServiceUuid,
238
222
  mockUuid,
239
223
  mockData,
240
224
  undefined
241
225
  );
226
+ expect(mockNoble.write).toHaveBeenCalledTimes(1);
242
227
  });
243
228
 
244
- it('should delegate to noble, withoutResponse false', () => {
229
+ test('should delegate to noble, withoutResponse false', () => {
245
230
  const mockData = Buffer.alloc(0);
246
231
  characteristic.write(mockData, false);
247
232
 
248
- assert.calledOnceWithExactly(
249
- mockNoble.write,
233
+ expect(mockNoble.write).toHaveBeenCalledWith(
250
234
  mockPeripheralId,
251
235
  mockServiceUuid,
252
236
  mockUuid,
253
237
  mockData,
254
238
  false
255
239
  );
240
+ expect(mockNoble.write).toHaveBeenCalledTimes(1);
256
241
  });
257
242
 
258
- it('should delegate to noble, withoutResponse true', () => {
243
+ test('should delegate to noble, withoutResponse true', () => {
259
244
  const mockData = Buffer.alloc(0);
260
245
  characteristic.write(mockData, true);
261
246
 
262
- assert.calledOnceWithExactly(
263
- mockNoble.write,
247
+ expect(mockNoble.write).toHaveBeenCalledWith(
264
248
  mockPeripheralId,
265
249
  mockServiceUuid,
266
250
  mockUuid,
267
251
  mockData,
268
252
  true
269
253
  );
254
+ expect(mockNoble.write).toHaveBeenCalledTimes(1);
270
255
  });
271
256
 
272
- it('should callback', () => {
257
+ test('should callback', () => {
273
258
  const mockData = Buffer.alloc(0);
274
- const callback = sinon.spy();
259
+ const callback = jest.fn();
275
260
 
276
261
  characteristic.write(mockData, true, callback);
277
262
  characteristic.emit('write');
278
263
  // Check for single callback
279
264
  characteristic.emit('write');
280
265
 
281
- assert.calledOnceWithExactly(callback, null);
282
- assert.calledOnceWithExactly(
283
- mockNoble.write,
266
+ expect(callback).toHaveBeenCalledWith(undefined);
267
+ expect(callback).toHaveBeenCalledTimes(1);
268
+ expect(mockNoble.write).toHaveBeenCalledWith(
284
269
  mockPeripheralId,
285
270
  mockServiceUuid,
286
271
  mockUuid,
287
272
  mockData,
288
273
  true
289
274
  );
275
+ expect(mockNoble.write).toHaveBeenCalledTimes(1);
290
276
  });
291
277
  });
292
278
 
293
279
  describe('writeAsync', () => {
294
- beforeEach(() => {
295
- mockNoble.write = sinon.spy();
296
- });
297
-
298
- afterEach(() => {
299
- sinon.reset();
300
- });
301
-
302
- it('should only accept data as a buffer', async () => {
303
- const promise = characteristic.writeAsync({});
304
- should(promise).rejectedWith(
280
+ test('should only accept data as a buffer', async () => {
281
+ await expect(characteristic.writeAsync({})).rejects.toThrow(
305
282
  'data must be a Buffer or Uint8Array or Uint16Array or Uint32Array'
306
283
  );
307
284
 
308
- assert.notCalled(mockNoble.write);
285
+ expect(mockNoble.write).not.toHaveBeenCalled();
309
286
  });
310
287
 
311
- it('should delegate to noble, withoutResponse false', async () => {
288
+ test('should delegate to noble, withoutResponse false', async () => {
312
289
  const mockData = Buffer.alloc(0);
313
290
  const promise = characteristic.writeAsync(mockData, false);
314
291
  characteristic.emit('write');
315
- // Check for single callback
316
- characteristic.emit('write');
317
-
318
- should(promise).resolvedWith(undefined);
319
- assert.calledOnceWithExactly(
320
- mockNoble.write,
292
+
293
+ await expect(promise).resolves.toBeUndefined();
294
+ expect(mockNoble.write).toHaveBeenCalledWith(
321
295
  mockPeripheralId,
322
296
  mockServiceUuid,
323
297
  mockUuid,
324
298
  mockData,
325
299
  false
326
300
  );
301
+ expect(mockNoble.write).toHaveBeenCalledTimes(1);
327
302
  });
328
303
 
329
- it('should delegate to noble, withoutResponse true', async () => {
304
+ test('should delegate to noble, withoutResponse true', async () => {
330
305
  const mockData = Buffer.alloc(0);
331
306
  const promise = characteristic.writeAsync(mockData, true);
332
307
  characteristic.emit('write');
333
- // Check for single callback
334
- characteristic.emit('write');
335
-
336
- should(promise).resolvedWith(undefined);
337
- assert.calledOnceWithExactly(
338
- mockNoble.write,
308
+
309
+ await expect(promise).resolves.toBeUndefined();
310
+ expect(mockNoble.write).toHaveBeenCalledWith(
339
311
  mockPeripheralId,
340
312
  mockServiceUuid,
341
313
  mockUuid,
342
314
  mockData,
343
315
  true
344
316
  );
317
+ expect(mockNoble.write).toHaveBeenCalledTimes(1);
345
318
  });
346
319
 
347
- it('should resolve', async () => {
320
+ test('should resolve', async () => {
348
321
  const mockData = Buffer.alloc(0);
349
322
  const promise = characteristic.writeAsync(mockData, true);
350
323
  characteristic.emit('write');
351
- // Check for single callback
352
- characteristic.emit('write');
353
-
354
- should(promise).resolvedWith(undefined);
324
+
325
+ await expect(promise).resolves.toBeUndefined();
326
+ expect(mockNoble.write).toHaveBeenCalledTimes(1);
355
327
  });
356
328
  });
357
329
 
358
330
  describe('broadcast', () => {
359
- beforeEach(() => {
360
- mockNoble.broadcast = sinon.spy();
361
- });
362
-
363
- afterEach(() => {
364
- sinon.reset();
365
- });
366
-
367
- it('should delegate to noble, true', () => {
331
+ test('should delegate to noble, true', () => {
368
332
  characteristic.broadcast(true);
369
333
 
370
- assert.calledOnceWithExactly(
371
- mockNoble.broadcast,
334
+ expect(mockNoble.broadcast).toHaveBeenCalledWith(
372
335
  mockPeripheralId,
373
336
  mockServiceUuid,
374
337
  mockUuid,
375
338
  true
376
339
  );
340
+ expect(mockNoble.broadcast).toHaveBeenCalledTimes(1);
377
341
  });
378
342
 
379
- it('should delegate to noble, false', () => {
343
+ test('should delegate to noble, false', () => {
380
344
  characteristic.broadcast(false);
381
345
 
382
- assert.calledOnceWithExactly(
383
- mockNoble.broadcast,
346
+ expect(mockNoble.broadcast).toHaveBeenCalledWith(
384
347
  mockPeripheralId,
385
348
  mockServiceUuid,
386
349
  mockUuid,
387
350
  false
388
351
  );
352
+ expect(mockNoble.broadcast).toHaveBeenCalledTimes(1);
389
353
  });
390
354
 
391
- it('should callback', () => {
392
- const callback = sinon.spy();
355
+ test('should callback', () => {
356
+ const callback = jest.fn();
393
357
 
394
358
  characteristic.broadcast(true, callback);
395
359
  characteristic.emit('broadcast');
396
360
  // Check for single callback
397
361
  characteristic.emit('broadcast');
398
362
 
399
- assert.calledOnceWithExactly(callback, null);
400
- assert.calledOnceWithExactly(
401
- mockNoble.broadcast,
363
+ expect(callback).toHaveBeenCalledWith(undefined);
364
+ expect(callback).toHaveBeenCalledTimes(1);
365
+ expect(mockNoble.broadcast).toHaveBeenCalledWith(
402
366
  mockPeripheralId,
403
367
  mockServiceUuid,
404
368
  mockUuid,
405
369
  true
406
370
  );
371
+ expect(mockNoble.broadcast).toHaveBeenCalledTimes(1);
407
372
  });
408
373
  });
409
374
 
410
375
  describe('broadcastAsync', () => {
411
- beforeEach(() => {
412
- mockNoble.broadcast = sinon.spy();
413
- });
414
-
415
- afterEach(() => {
416
- sinon.reset();
417
- });
418
-
419
- it('should delegate to noble, true', async () => {
376
+ test('should delegate to noble, true', async () => {
420
377
  const promise = characteristic.broadcastAsync(true);
421
378
  characteristic.emit('broadcast');
422
- // Check for single callback
423
- characteristic.emit('broadcast');
424
-
425
- should(promise).resolvedWith(undefined);
426
- assert.calledOnceWithExactly(
427
- mockNoble.broadcast,
379
+
380
+ await expect(promise).resolves.toBeUndefined();
381
+ expect(mockNoble.broadcast).toHaveBeenCalledWith(
428
382
  mockPeripheralId,
429
383
  mockServiceUuid,
430
384
  mockUuid,
431
385
  true
432
386
  );
387
+ expect(mockNoble.broadcast).toHaveBeenCalledTimes(1);
433
388
  });
434
389
 
435
- it('should delegate to noble, false', async () => {
390
+ test('should delegate to noble, false', async () => {
436
391
  const promise = characteristic.broadcastAsync(false);
437
392
  characteristic.emit('broadcast');
438
- // Check for single callback
439
- characteristic.emit('broadcast');
440
-
441
- should(promise).resolvedWith(undefined);
442
- assert.calledOnceWithExactly(
443
- mockNoble.broadcast,
393
+
394
+ await expect(promise).resolves.toBeUndefined();
395
+ expect(mockNoble.broadcast).toHaveBeenCalledWith(
444
396
  mockPeripheralId,
445
397
  mockServiceUuid,
446
398
  mockUuid,
447
399
  false
448
400
  );
401
+ expect(mockNoble.broadcast).toHaveBeenCalledTimes(1);
449
402
  });
450
403
 
451
- it('should resolve', async () => {
404
+ test('should resolve', async () => {
452
405
  const promise = characteristic.broadcastAsync(true);
453
406
  characteristic.emit('broadcast');
454
- // Check for single callback
455
- characteristic.emit('broadcast');
456
-
457
- should(promise).resolvedWith(undefined);
458
- assert.calledOnceWithExactly(
459
- mockNoble.broadcast,
407
+
408
+ await expect(promise).resolves.toBeUndefined();
409
+ expect(mockNoble.broadcast).toHaveBeenCalledWith(
460
410
  mockPeripheralId,
461
411
  mockServiceUuid,
462
412
  mockUuid,
463
413
  true
464
414
  );
415
+ expect(mockNoble.broadcast).toHaveBeenCalledTimes(1);
465
416
  });
466
417
  });
467
418
 
468
419
  describe('notify', () => {
469
- beforeEach(() => {
470
- mockNoble.notify = sinon.spy();
471
- });
472
-
473
- afterEach(() => {
474
- sinon.reset();
475
- });
476
-
477
- it('should delegate to noble, true', () => {
420
+ test('should delegate to noble, true', () => {
478
421
  characteristic.notify(true);
479
422
 
480
- assert.calledOnceWithExactly(
481
- mockNoble.notify,
423
+ expect(mockNoble.notify).toHaveBeenCalledWith(
482
424
  mockPeripheralId,
483
425
  mockServiceUuid,
484
426
  mockUuid,
485
427
  true
486
428
  );
429
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
487
430
  });
488
431
 
489
- it('should delegate to noble, false', () => {
432
+ test('should delegate to noble, false', () => {
490
433
  characteristic.notify(false);
491
434
 
492
- assert.calledOnceWithExactly(
493
- mockNoble.notify,
435
+ expect(mockNoble.notify).toHaveBeenCalledWith(
494
436
  mockPeripheralId,
495
437
  mockServiceUuid,
496
438
  mockUuid,
497
439
  false
498
440
  );
441
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
499
442
  });
500
443
 
501
- it('should callback', () => {
502
- const callback = sinon.spy();
444
+ test('should callback', () => {
445
+ const callback = jest.fn();
503
446
 
504
447
  characteristic.notify(true, callback);
505
448
  characteristic.emit('notify');
506
449
  // Check for single callback
507
450
  characteristic.emit('notify');
508
451
 
509
- assert.calledOnceWithExactly(callback, null);
510
- assert.calledOnceWithExactly(
511
- mockNoble.notify,
452
+ expect(callback).toHaveBeenCalledWith(undefined, undefined);
453
+ expect(callback).toHaveBeenCalledTimes(1);
454
+ expect(mockNoble.notify).toHaveBeenCalledWith(
512
455
  mockPeripheralId,
513
456
  mockServiceUuid,
514
457
  mockUuid,
515
458
  true
516
459
  );
460
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
517
461
  });
518
462
  });
519
463
 
520
464
  describe('notifyAsync', () => {
521
- beforeEach(() => {
522
- mockNoble.notify = sinon.spy();
523
- });
524
-
525
- afterEach(() => {
526
- sinon.reset();
527
- });
528
-
529
- it('should delegate to noble, true', async () => {
465
+ test('should delegate to noble, true', async () => {
530
466
  const promise = characteristic.notifyAsync(true);
531
467
  characteristic.emit('notify');
532
- // Check for single callback
533
- characteristic.emit('notify');
534
-
535
- should(promise).resolvedWith(undefined);
536
- assert.calledOnceWithExactly(
537
- mockNoble.notify,
468
+
469
+ await expect(promise).resolves.toBeUndefined();
470
+ expect(mockNoble.notify).toHaveBeenCalledWith(
538
471
  mockPeripheralId,
539
472
  mockServiceUuid,
540
473
  mockUuid,
541
474
  true
542
475
  );
476
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
543
477
  });
544
478
 
545
- it('should delegate to noble, false', async () => {
479
+ test('should delegate to noble, false', async () => {
546
480
  const promise = characteristic.notifyAsync(false);
547
481
  characteristic.emit('notify');
548
- // Check for single callback
549
- characteristic.emit('notify');
550
-
551
- should(promise).resolvedWith(undefined);
552
- assert.calledOnceWithExactly(
553
- mockNoble.notify,
482
+
483
+ await expect(promise).resolves.toBeUndefined();
484
+ expect(mockNoble.notify).toHaveBeenCalledWith(
554
485
  mockPeripheralId,
555
486
  mockServiceUuid,
556
487
  mockUuid,
557
488
  false
558
489
  );
490
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
559
491
  });
560
492
  });
561
493
 
562
494
  describe('subscribe', () => {
563
- beforeEach(() => {
564
- mockNoble.notify = sinon.spy();
565
- });
566
-
567
- afterEach(() => {
568
- sinon.reset();
569
- });
570
-
571
- it('should delegate to noble notify, true', () => {
495
+ test('should delegate to noble notify, true', () => {
572
496
  characteristic.subscribe();
573
497
 
574
- assert.calledOnceWithExactly(
575
- mockNoble.notify,
498
+ expect(mockNoble.notify).toHaveBeenCalledWith(
576
499
  mockPeripheralId,
577
500
  mockServiceUuid,
578
501
  mockUuid,
579
502
  true
580
503
  );
504
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
581
505
  });
582
506
 
583
- it('should callback', () => {
584
- const callback = sinon.spy();
507
+ test('should callback', () => {
508
+ const callback = jest.fn();
585
509
 
586
510
  characteristic.subscribe(callback);
587
511
  characteristic.emit('notify');
588
512
  // Check for single callback
589
513
  characteristic.emit('notify');
590
514
 
591
- assert.calledOnceWithExactly(callback, null);
592
- assert.calledOnceWithExactly(
593
- mockNoble.notify,
515
+ expect(callback).toHaveBeenCalledWith(undefined, undefined);
516
+ expect(callback).toHaveBeenCalledTimes(1);
517
+ expect(mockNoble.notify).toHaveBeenCalledWith(
594
518
  mockPeripheralId,
595
519
  mockServiceUuid,
596
520
  mockUuid,
597
521
  true
598
522
  );
523
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
599
524
  });
600
525
  });
601
526
 
602
527
  describe('subscribeAsync', () => {
603
- beforeEach(() => {
604
- mockNoble.notify = sinon.spy();
605
- });
606
-
607
- afterEach(() => {
608
- sinon.reset();
609
- });
610
-
611
- it('should delegate to noble notify, true', async () => {
528
+ test('should delegate to noble notify, true', async () => {
612
529
  const promise = characteristic.subscribeAsync();
613
530
  characteristic.emit('notify');
614
- // Check for single callback
615
- characteristic.emit('notify');
616
-
617
- should(promise).resolvedWith(undefined);
618
- assert.calledOnceWithExactly(
619
- mockNoble.notify,
531
+
532
+ await expect(promise).resolves.toBeUndefined();
533
+ expect(mockNoble.notify).toHaveBeenCalledWith(
620
534
  mockPeripheralId,
621
535
  mockServiceUuid,
622
536
  mockUuid,
623
537
  true
624
538
  );
539
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
625
540
  });
626
541
  });
627
542
 
628
543
  describe('unsubscribe', () => {
629
- beforeEach(() => {
630
- mockNoble.notify = sinon.spy();
631
- });
632
-
633
- afterEach(() => {
634
- sinon.reset();
635
- });
636
-
637
- it('should delegate to noble notify, false', () => {
544
+ test('should delegate to noble notify, false', () => {
638
545
  characteristic.unsubscribe();
639
546
 
640
- assert.calledOnceWithExactly(
641
- mockNoble.notify,
547
+ expect(mockNoble.notify).toHaveBeenCalledWith(
642
548
  mockPeripheralId,
643
549
  mockServiceUuid,
644
550
  mockUuid,
645
551
  false
646
552
  );
553
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
647
554
  });
648
555
 
649
- it('should callback', () => {
650
- const callback = sinon.spy();
556
+ test('should callback', () => {
557
+ const callback = jest.fn();
651
558
 
652
559
  characteristic.unsubscribe(callback);
653
560
  characteristic.emit('notify');
654
561
  // Check for single callback
655
562
  characteristic.emit('notify');
656
563
 
657
- assert.calledOnceWithExactly(callback, null);
658
- assert.calledOnceWithExactly(
659
- mockNoble.notify,
564
+ expect(callback).toHaveBeenCalledWith(undefined, undefined);
565
+ expect(callback).toHaveBeenCalledTimes(1);
566
+ expect(mockNoble.notify).toHaveBeenCalledWith(
660
567
  mockPeripheralId,
661
568
  mockServiceUuid,
662
569
  mockUuid,
663
570
  false
664
571
  );
572
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
665
573
  });
666
574
  });
667
575
 
668
576
  describe('unsubscribeAsync', () => {
669
- beforeEach(() => {
670
- mockNoble.notify = sinon.spy();
671
- });
672
-
673
- afterEach(() => {
674
- sinon.reset();
675
- });
676
-
677
- it('should delegate to noble notify, false', async () => {
577
+ test('should delegate to noble notify, false', async () => {
678
578
  const promise = characteristic.unsubscribeAsync();
679
579
  characteristic.emit('notify');
680
- // Check for single callback
681
- characteristic.emit('notify');
682
-
683
- should(promise).resolvedWith(undefined);
684
- assert.calledOnceWithExactly(
685
- mockNoble.notify,
580
+
581
+ await expect(promise).resolves.toBeUndefined();
582
+ expect(mockNoble.notify).toHaveBeenCalledWith(
686
583
  mockPeripheralId,
687
584
  mockServiceUuid,
688
585
  mockUuid,
689
586
  false
690
587
  );
588
+ expect(mockNoble.notify).toHaveBeenCalledTimes(1);
691
589
  });
692
590
  });
693
591
 
694
592
  describe('discoverDescriptors', () => {
695
- beforeEach(() => {
696
- mockNoble.discoverDescriptors = sinon.spy();
697
- });
698
-
699
- afterEach(() => {
700
- sinon.reset();
701
- });
702
-
703
- it('should delegate to noble', () => {
593
+ test('should delegate to noble', () => {
704
594
  characteristic.discoverDescriptors();
705
595
 
706
- assert.calledOnceWithExactly(
707
- mockNoble.discoverDescriptors,
596
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledWith(
708
597
  mockPeripheralId,
709
598
  mockServiceUuid,
710
599
  mockUuid
711
600
  );
601
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledTimes(1);
712
602
  });
713
603
 
714
- it('should callback, undefined descriptors', () => {
715
- const callback = sinon.spy();
604
+ test('should callback, undefined descriptors', () => {
605
+ const callback = jest.fn();
716
606
 
717
607
  characteristic.discoverDescriptors(callback);
718
608
  characteristic.emit('descriptorsDiscover');
719
609
  // Check for single callback
720
610
  characteristic.emit('descriptorsDiscover');
721
611
 
722
- assert.calledOnceWithExactly(callback, null, undefined);
723
- assert.calledOnceWithExactly(
724
- mockNoble.discoverDescriptors,
612
+ expect(callback).toHaveBeenCalledWith(undefined, undefined);
613
+ expect(callback).toHaveBeenCalledTimes(1);
614
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledWith(
725
615
  mockPeripheralId,
726
616
  mockServiceUuid,
727
617
  mockUuid
728
618
  );
619
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledTimes(1);
729
620
  });
730
621
 
731
- it('should callback with descriptors', () => {
732
- const callback = sinon.spy();
622
+ test('should callback with descriptors', () => {
623
+ const callback = jest.fn();
733
624
  const descriptors = 'descriptors';
734
625
 
735
626
  characteristic.discoverDescriptors(callback);
@@ -737,55 +628,44 @@ describe('characteristic', () => {
737
628
  // Check for single callback
738
629
  characteristic.emit('descriptorsDiscover', descriptors);
739
630
 
740
- assert.calledOnceWithExactly(callback, null, descriptors);
741
- assert.calledOnceWithExactly(
742
- mockNoble.discoverDescriptors,
631
+ expect(callback).toHaveBeenCalledWith(undefined, descriptors);
632
+ expect(callback).toHaveBeenCalledTimes(1);
633
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledWith(
743
634
  mockPeripheralId,
744
635
  mockServiceUuid,
745
636
  mockUuid
746
637
  );
638
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledTimes(1);
747
639
  });
748
640
  });
749
641
 
750
642
  describe('discoverDescriptorsAsync', () => {
751
- beforeEach(() => {
752
- mockNoble.discoverDescriptors = sinon.spy();
753
- });
754
-
755
- afterEach(() => {
756
- sinon.reset();
757
- });
758
-
759
- it('should delegate to noble', async () => {
643
+ test('should delegate to noble', async () => {
760
644
  const promise = characteristic.discoverDescriptorsAsync();
761
645
  characteristic.emit('descriptorsDiscover');
762
- // Check for single callback
763
- characteristic.emit('descriptorsDiscover');
764
-
765
- should(promise).resolvedWith(null, undefined);
766
- assert.calledOnceWithExactly(
767
- mockNoble.discoverDescriptors,
646
+
647
+ await expect(promise).resolves.toBeUndefined();
648
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledWith(
768
649
  mockPeripheralId,
769
650
  mockServiceUuid,
770
651
  mockUuid
771
652
  );
653
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledTimes(1);
772
654
  });
773
655
 
774
- it('should resolve with descriptors', async () => {
656
+ test('should resolve with descriptors', async () => {
775
657
  const descriptors = 'descriptors';
776
658
 
777
659
  const promise = characteristic.discoverDescriptorsAsync();
778
660
  characteristic.emit('descriptorsDiscover', descriptors);
779
- // Check for single callback
780
- characteristic.emit('descriptorsDiscover', descriptors);
781
-
782
- should(promise).resolvedWith(null, descriptors);
783
- assert.calledOnceWithExactly(
784
- mockNoble.discoverDescriptors,
661
+
662
+ await expect(promise).resolves.toEqual(descriptors);
663
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledWith(
785
664
  mockPeripheralId,
786
665
  mockServiceUuid,
787
666
  mockUuid
788
667
  );
668
+ expect(mockNoble.discoverDescriptors).toHaveBeenCalledTimes(1);
789
669
  });
790
670
  });
791
- });
671
+ });