Skip to content

Commit

Permalink
feat: add setAlertMessage, enhance cancelTechnologyRequest
Browse files Browse the repository at this point in the history
* setAlertMessage is no-op for Android, and the same as setAlertMessageIOS
for iOS.
* cancelTechnology won't throw any exceptions by default.
  • Loading branch information
whitedogg13 committed Apr 21, 2021
1 parent 18f3f44 commit b8f3345
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 13 deletions.
30 changes: 29 additions & 1 deletion __tests__/NfcManager.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
jest.mock('../src/NativeNfcManager');

import {Platform} from 'react-native';
import {NfcManagerEmitter, callNative} from '../src/NativeNfcManager';
import {
NativeNfcManager,
NfcManagerEmitter,
callNative,
} from '../src/NativeNfcManager';

describe('NfcManager (ios)', () => {
Platform.setOS('ios');
Expand Down Expand Up @@ -92,6 +96,30 @@ describe('NfcManager (ios)', () => {
expect(options.invalidateAfterFirstRead).toBe(false);
});

test('API: cancelTechnologyRequest', async () => {
// won't throw any error during cancellation by default
NativeNfcManager.setNextError('fake-error');
await NfcManager.cancelTechnologyRequest();

NativeNfcManager.setNextError('fake-error-again');
try {
// default can be overriden by throwOnError
await NfcManager.cancelTechnologyRequest({throwOnError: true});
} catch (ex) {
expect(ex).toEqual('fake-error-again');
}
});

test('API: setAlertMessage', () => {
NfcManager.setAlertMessageIOS('hello');
expect(lastNativeCall()[0]).toEqual('setAlertMessage');
expect(lastNativeCall()[1]).toEqual(['hello']);

NfcManager.setAlertMessage('hello');
expect(lastNativeCall()[0]).toEqual('setAlertMessage');
expect(lastNativeCall()[1]).toEqual(['hello']);
});

test('NfcErrorIOS', () => {
expect(NfcErrorIOS.parse({})).toEqual(NfcErrorIOS.errCodes.unknown);
expect(NfcErrorIOS.parse('nosucherror')).toEqual(
Expand Down
6 changes: 6 additions & 0 deletions __tests__/NfcManagerAndroid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ describe('NfcManager (android)', () => {
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);
});
});
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ declare module 'react-native-nfc-manager' {
readerModeDelay?: number;
}

export interface CancelTechReqOpts {
throwOnError?: boolean = false;
}

interface NdefHandler {
writeNdefMessage: (bytes: number[]) => Promise<void>;
getNdefMessage: () => Promise<TagEvent | null>;
Expand Down Expand Up @@ -185,8 +189,9 @@ declare module 'react-native-nfc-manager' {
tech: NfcTech | NfcTech[],
options?: RegisterTagEventOpts,
): Promise<NfcTech | null>;
cancelTechnologyRequest: () => Promise<void>;
cancelTechnologyRequest: (options?: CancelTechReqOpts) => Promise<void>;
getTag: () => Promise<TagEvent | null>;
setAlertMessage: (alertMessage: string) => Promise<void>;

/**
* common tech handler getters for both iOS / Android
Expand Down
6 changes: 6 additions & 0 deletions src/NfcManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ function NotImpl() {
throw new Error('not implemented');
}

async function DoNothing() {
// allow derived class to not implment it
}

class NfcManagerBase {
constructor() {
this._subscribeNativeEvents();
Expand Down Expand Up @@ -84,6 +88,8 @@ class NfcManagerBase {

cancelTechnologyRequest = NotImpl;

setAlertMessage = DoNothing;

async writeNdefMessage(bytes) {
return callNative('writeNdefMessage', [bytes]);
}
Expand Down
18 changes: 13 additions & 5 deletions src/NfcManagerAndroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ class NfcManagerAndroid extends NfcManagerBase {
}
};

cancelTechnologyRequest = async () => {
await callNative('cancelTechnologyRequest');
cancelTechnologyRequest = async (options = {}) => {
const {throwOnError = false} = options;

if (!this.cleanUpTagRegistration) {
await this.unregisterTagEvent();
this.cleanUpTagRegistration = false;
try {
await callNative('cancelTechnologyRequest');

if (!this.cleanUpTagRegistration) {
await this.unregisterTagEvent();
this.cleanUpTagRegistration = false;
}
} catch (ex) {
if (throwOnError) {
throw ex;
}
}
};

Expand Down
22 changes: 17 additions & 5 deletions src/NfcManagerIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,28 @@ class NfcManagerIOS extends NfcManagerBase {
}
};

cancelTechnologyRequest = async () => callNative('cancelTechnologyRequest');
cancelTechnologyRequest = async (options = {}) => {
const {throwOnError = false} = options;

try {
return await callNative('cancelTechnologyRequest');
} catch (ex) {
if (throwOnError) {
throw ex;
}
}
};

// -------------------------------------
// public only for iOS
// -------------------------------------
setAlertMessage = (alertMessage) => {
// override the parent one
return callNative('setAlertMessage', [alertMessage]);
};

setAlertMessageIOS = (alertMessage) => {
if (Platform.OS !== 'ios') {
return Promise.resolve(); //no-op
}
callNative('setAlertMessage', [alertMessage]);
return callNative('setAlertMessage', [alertMessage]);
};

invalidateSessionIOS = () => callNative('invalidateSession');
Expand Down
13 changes: 12 additions & 1 deletion src/__mocks__/NativeNfcManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
let _nextError = null;

const NativeNfcManager = {
MIFARE_BLOCK_SIZE: 16,
MIFARE_ULTRALIGHT_PAGE_SIZE: 4,
setNextError: (err) => {
_nextError = err;
},
};

const NfcManagerEmitterListener = {};
Expand All @@ -14,6 +19,12 @@ const NfcManagerEmitter = {
},
};

const callNative = jest.fn();
const callNative = jest.fn((...args) => {
if (_nextError) {
const err = _nextError;
_nextError = null;
return Promise.reject(err);
}
});

export {NativeNfcManager, NfcManagerEmitter, callNative};

0 comments on commit b8f3345

Please sign in to comment.