Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/urish/web-bluetooth-mock
Mock Implementation of Web Bluetooth (for tests)
https://github.com/urish/web-bluetooth-mock
iot mock testing-tools web-bluetooth
Last synced: about 2 months ago
JSON representation
Mock Implementation of Web Bluetooth (for tests)
- Host: GitHub
- URL: https://github.com/urish/web-bluetooth-mock
- Owner: urish
- Created: 2017-11-03T00:34:19.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-05-10T12:40:48.000Z (over 1 year ago)
- Last Synced: 2024-11-10T05:25:05.604Z (about 2 months ago)
- Topics: iot, mock, testing-tools, web-bluetooth
- Language: TypeScript
- Size: 20.5 KB
- Stars: 15
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Web Bluetooth API Mock
[![Build Status](https://travis-ci.org/urish/web-bluetooth-mock.png?branch=master)](https://travis-ci.org/urish/web-bluetooth-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](https://facebook.github.io/jest/) testing framework, though the code
can be very easily adjusted for a different testing framework.```javascript
import { WebBluetoothMock, DeviceMock } from './web-bluetooth.mock';describe('connectToDevice', () => {
it('should connect to bluetooth device', async () => {
// Setup a Mock device and register the Web Bluetooth Mock
const device = new DeviceMock('Dummy-Device', [0xffe0]);
global.navigator = global.navigator || {};
global.navigator.bluetooth = new WebBluetoothMock([device]);// This is a Jest specific mock, change to just `spyOn(...)` for Jasmine
jest.spyOn(device.gatt, 'connect');// Calling the method we want to be tested
await connectToDevice();// Checking if the function has been invoked. This also means
// that requestDevice() has been called with a filter that matches
// the device we defined above.
expect(device.gatt.connect).toHaveBeenCalled();
});
});
```For a more complete example, check out [muse-js library tests](https://github.com/urish/muse-js/blob/master/src/muse.spec.ts).