Skip to content

Instantly share code, notes, and snippets.

@jrapala
Created July 16, 2020 20:55
Show Gist options
  • Save jrapala/343e7a83fb1738dc15163ea50535692f to your computer and use it in GitHub Desktop.
Save jrapala/343e7a83fb1738dc15163ea50535692f to your computer and use it in GitHub Desktop.
Mocking RN Linking
@Coffeegerm
Copy link

@anabeatrizzz the second parameter for jest.mock is a module factory that returns back the mocked methods of the module, for this instance we are returning back a mocked implementation for the openURL method that Linking exports. If you don't mock the module and attempt to do any sort of verification of the module being called you will receive an error from jest due to the function not being a mocked one. Does that help?

@FanchenBao
Copy link

Good method, but I have to also mock event listener functions to make it work.

jest.mock('react-native/Libraries/Linking/Linking', () => ({
  openURL: jest.fn().mockResolvedValue(null),
  addEventListener: jest.fn(),
  removeEventListener: jest.fn(),
}));

@B0Y3R
Copy link

B0Y3R commented May 16, 2023

needed an example of mocking openURL in a pinch and this did the trick. Thank you!

@anabeatrizzz
Copy link

@Coffeegerm yes, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment