react-native-nfc-manager/__tests__/NfcManagerAndroid.test.js
Richie 43fd209335
feat: enhance error checking (#394)
* feat: add NfcError module

* chore: update index.d.ts for NfcError

* draft: enhance NfcError

* fix: export correct NfcErrorIOS, and fix bug in cancelTechnology

* feat: wrap all callNative with handleNativeException

* fix(android): use buildNfcExceptionAndroid rather than
handleNativeException for requestTechnology and cancelTechnologyRequest

* feat: enhance Android's native error message

* feat: enhance index.d.ts

* fix: export NfcError namespace
2021-05-10 22:47:19 +08:00

74 lines
2.2 KiB
JavaScript

jest.mock('../src/NativeNfcManager');
import {Platform} from 'react-native';
import {NativeNfcManager} from '../src/NativeNfcManager';
import * as NfcError from '../src/NfcError';
describe('NfcManager (android)', () => {
Platform.setOS('android');
const NfcManagerModule = require('../src/index.js');
const NfcManager = NfcManagerModule.default;
const {NfcTech} = NfcManagerModule;
test('constructor', () => {
expect(Platform.OS).toBe('android');
// the NfcManager instance doest exist
expect(!!NfcManager).toEqual(true);
});
test('mifareClassicHandler', async () => {
expect(!!NfcManager.mifareClassicHandlerAndroid).toBe(true);
try {
// should throw exception if the data is not an array of length 16
await NfcManager.mifareClassicHandlerAndroid.mifareClassicWriteBlock(1, [
1,
]);
expect(true).toBe(false);
} catch (ex) {}
// https://github.com/whitedogg13/react-native-nfc-manager/issues/371
await NfcManager.mifareClassicHandlerAndroid.mifareClassicWriteBlock(
5,
Array.from({length: 16}).map((_, i) => i),
);
});
test('mifareUltralightHandler', async () => {
expect(!!NfcManager.mifareUltralightHandlerAndroid).toBe(true);
try {
// should throw exception if the data is not an array of length 4
await NfcManager.mifareUltralightHandlerAndroid.mifareUltralightWritePage(
1,
[1],
);
expect(true).toBe(false);
} catch (ex) {}
// https://github.com/whitedogg13/react-native-nfc-manager/issues/386
// https://github.com/whitedogg13/react-native-nfc-manager/issues/387
await NfcManager.mifareUltralightHandlerAndroid.mifareUltralightWritePage(
5,
Array.from({length: 4}).map((_, i) => i),
);
});
test('API: setAlertMessage', async () => {
// test if the method stub exists and can be called without exception
await NfcManager.setAlertMessage();
expect(true).toBe(true);
});
test('NfcError', async () => {
try {
NativeNfcManager.setNextError('cancelled');
await NfcManager.requestTechnology(NfcTech.Ndef);
} catch (ex) {
if (!(ex instanceof NfcError.UserCancel)) {
expect(true).toBe(false);
}
}
});
});