Web Bluetooth API Mock
Mock for the Web Bluetooth API, useful for testing code that uses Web Bluetooth.
Copyright (C) 2017, Uri Shaked. Licensed under the terms of MIT License.
Installation
The Web Bluetooth API mock is available on npm, and can be installed by running:
npm install --save-dev web-bluetooth-mock
Usage example
The following code will test whether a method called connectToDevice()
scans for a device containing a 0xffe0
service and connects to it.
It assumes jest testing framework, though the code
can be very easily adjusted for a different testing framework.
import { WebBluetoothMock, DeviceMock } from './web-bluetooth.mock';
describe('connectToDevice', () => {
it('should connect to bluetooth device', async () => {
const device = new DeviceMock('Dummy-Device', [0xffe0]);
global.navigator = global.navigator || {};
global.navigator.bluetooth = new WebBluetoothMock([device]);
jest.spyOn(device.gatt, 'connect');
await connectToDevice();
expect(device.gatt.connect).toHaveBeenCalled();
});
});
For a more complete example, check out muse-js library tests.