Skip to content

Instantly share code, notes, and snippets.

@julioxavierr
Created April 16, 2021 12:50
Show Gist options
  • Save julioxavierr/22905066045b81aae9324b0a90ad98c7 to your computer and use it in GitHub Desktop.
Save julioxavierr/22905066045b81aae9324b0a90ad98c7 to your computer and use it in GitHub Desktop.
Mock event callback
import { scan } from '../scan';
import { BleManager, Device } from 'react-native-ble-plx';
import {
BleErrorCodeMessage,
BleError,
} from 'react-native-ble-plx/src/BleError';
let cb: Parameters<BleManager['startDeviceScan']>[2] = null;
jest.mock('react-native-ble-plx', () => ({
BleManager: () => ({
startDeviceScan: jest.fn(((_, __, receivedCb) => {
cb = receivedCb;
}) as BleManager['startDeviceScan']),
}),
}));
it('should log the callback param', () => {
scan();
// send a scanned device
cb(null, { id: 'DEVICE_A' } as Device);
// send an error
cb(new BleError('Something', BleErrorCodeMessage), null);
// expect
// ...
});
import { BleManager } from 'react-native-ble-plx';
export const scan = () => {
const bleManager = new BleManager();
bleManager.startDeviceScan(null, null, (error, scannedDevice) => {
console.log('ERROR', error);
console.log('SCANNED DEVICE', scannedDevice);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment