morenoh149 / react-native-contacts

React Native Contacts
MIT License
1.65k stars 564 forks source link

how to mock react-native-contacts with jest #632

Closed dsagredo closed 3 years ago

dsagredo commented 3 years ago

Any solutions or advice on how to mock both components with Jest, and not just one? I am very new to Jest.

morenoh149 commented 3 years ago

nope, that is a jest question.

rajAmukhliS commented 2 years ago

mock react-native-contacts inside test file

jest.mock('react-native-contacts', () => ({
  checkPermission: jest
    .fn()
    .mockImplementation(() => Promise.resolve('undefined')),
  getAll: jest.fn().mockImplementation(() => Promise.resolve([])),
  requestPermission: jest
    .fn()
    .mockImplementation(() => Promise.resolve('undefined')),
  getContactsMatchingString: jest
    .fn()
    .mockImplementation(() => Promise.resolve([])),
}));

and mock react-native-permissions also in jest.setup.js or on top of test file

jest.mock('react-native-permissions', () =>
  require('react-native-permissions/mock'),
);