revtel / react-native-nfc-manager

React Native NFC module for Android & iOS
MIT License
1.35k stars 317 forks source link

Trying to mock nfcManager using react native testing library, but it doesn't work & nfcManager.start() #597

Closed PanosPapad closed 1 year ago

PanosPapad commented 1 year ago

Hello,

I am developing an android app that requires to read nfc tags, so I am using this library. However, I have come across two issues that I could not figure out at all.

const mockNfcManager = jest.fn();

jest.mock("react-native-nfc-manager", ()=>({ NfcManagerModule: { start: mockNfcManager, }, }));

and then render the component and check if expect(mockedNavigate).toHaveBeenCalled()

Sorry for the long message, but I saw you are pretty responsive in the issues of this library, and also I have not seen anyone mentioning these issues anywhere, so I was hoping to solve this issue here :) Thanks a lot!

dylanclements commented 1 year ago

This worked for me at the top level of my test scripts. Alternatively you could also place this in a __mocks__ folder in your root directory and include it in your setupFiles in jest.config.js


jest.mock('react-native-nfc-manager', () => ({
  start: () => Promise.resolve(),
}));
whitedogg13 commented 1 year ago

Hi @PanosPapad , sorry for the late response.

The start() function is simply used to initialize the native layer.

Regarding the mock, I think @dylanclements 's suggestion should work:

jest.mock('react-native-nfc-manager', () => ({
  start: () => Promise.resolve(),
  isSupported: () => Promise.resolve(false), // bypass NFC feature when running test cases
}));
github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 1 year ago

This issue was closed because it has been stalled for 5 days with no activity.