RealOrangeOne / react-native-mock

A fully mocked and test-friendly version of react native (maintainers wanted)
MIT License
571 stars 153 forks source link

Any interest in exposing or extracting utilities? #105

Closed randycoulman closed 7 years ago

randycoulman commented 7 years ago

Thank you for this project! It's been very helpful and works great!

While this project works for the core React Native components, it doesn't help with other native components (such as react-native-camera). However, the utilities and approach taken here work just fine with other components.

Is there any interest in exposing some utilities from this library, or extracting them to a different library and then using them from here?

I'm thinking particularly of createMockComponent and the code in mock.js, but there may be other reusable helpers as well.

In my project, I extracted a function out of mock.js that looks like this:

function registerNativeMock(pkg, component) {
  const key = require.resolve(pkg);

  require.cache[key] = {
    id: key,
    filename: key,
    loaded: true,
    exports: component
  };
}

Using this function, mock.js would end up looking like this:

const ReactNativeMock = require('./build/react-native');

registerNativeMock('react-native', ReactNativeMock);

If this idea is of interest, let me know which direction you'd like to go (expose or extract), and I'll be happy to put together a PR.

Also, if you can identify any additional utilities that might be useful when mocking other libraries, point them out and I'll include them.

RealOrangeOne commented 7 years ago

Something like this isnt really in the scope of this project. as the components youre looking to mock arent actually a part of react-native. However, instead of rolling your own mocking function, I recommend using https://github.com/mfncooper/mockery, it works great for exactly what youre doing!