forked from Root-App/react-native-mock-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.js
44 lines (37 loc) · 987 Bytes
/
mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const createMockComponent = require('./build/components/createMockComponent');
const ReactNativeMock = require('./build/react-native');
const createMock = exports => {
if (Array.isArray(exports)) {
return exports.reduce(
(acc, componentName) => ({
...acc,
[componentName]: createMockComponent(componentName)
}),
{}
);
} else {
return createMockComponent(exports);
}
};
const register = (name, mock) => {
// the cache key that real lib would get
const key = require.resolve(name);
// make sure the cache is filled with our lib
require.cache[key] = {
id: key,
filename: key,
loaded: true,
exports: mock
};
};
const createMocks = options => {
register('react-native', ReactNativeMock);
const {externalLibs} = options;
if (externalLibs) {
externalLibs.forEach(({name, exports}) => {
const mock = createMock(exports);
register(name, mock);
});
}
};
module.exports = createMocks;