Created
April 16, 2021 12:50
-
-
Save julioxavierr/22905066045b81aae9324b0a90ad98c7 to your computer and use it in GitHub Desktop.
Mock event callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
// ... | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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