/
setup.js
183 lines (164 loc) · 5.78 KB
/
setup.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Adds Expo-related mocks to the Jest environment. Jest runs this setup module after it runs the
* React Native setup module.
*/
'use strict';
const mockNativeModules = require('react-native/Libraries/BatchedBridge/NativeModules');
const createMockConstants = require('./createMockConstants');
const publicExpoModules = require('./expoModules');
const internalExpoModules = require('./internalExpoModules');
// window isn't defined as of react-native 0.45+ it seems
if (typeof window !== 'object') {
global.window = global;
global.window.navigator = {};
}
const mockImageLoader = {
configurable: true,
enumerable: true,
get: () => ({
prefetchImage: jest.fn(),
getSize: jest.fn((uri, success) => process.nextTick(() => success(320, 240))),
}),
};
Object.defineProperty(mockNativeModules, 'ImageLoader', mockImageLoader);
Object.defineProperty(mockNativeModules, 'ImageViewManager', mockImageLoader);
Object.defineProperty(mockNativeModules, 'LinkingManager', {
configurable: true,
enumerable: true,
get: () => mockNativeModules.Linking,
});
const expoModules = {
...publicExpoModules,
...internalExpoModules,
};
function mock(property, customMock) {
const propertyType = property.type;
let mockValue;
if (customMock !== undefined) {
mockValue = customMock;
} else if (propertyType === 'function') {
if (property.functionType === 'promise') {
mockValue = jest.fn(() => Promise.resolve());
} else {
mockValue = jest.fn();
}
} else if (propertyType === 'number') {
mockValue = 1;
} else if (propertyType === 'string') {
mockValue = 'mock';
} else if (propertyType === 'array') {
mockValue = [];
} else if (propertyType === 'mock') {
mockValue = mockByMockDefinition(property.mockDefinition);
} else {
mockValue = {};
}
return mockValue;
}
function mockProperties(moduleProperties, customMocks) {
const mockedProperties = {};
for (let propertyName of Object.keys(moduleProperties)) {
const property = moduleProperties[propertyName];
const customMock =
customMocks && customMocks.hasOwnProperty(propertyName)
? customMocks[propertyName]
: property.mock;
mockedProperties[propertyName] = mock(property, customMock);
}
return mockedProperties;
}
function mockByMockDefinition(definition) {
const mock = {};
for (const key of Object.keys(definition)) {
mock[key] = mockProperties(definition[key]);
}
return mock;
}
for (let moduleName of Object.keys(expoModules)) {
const moduleProperties = expoModules[moduleName];
const mockedProperties = mockProperties(moduleProperties);
Object.defineProperty(mockNativeModules, moduleName, {
configurable: true,
enumerable: true,
get: () => mockedProperties,
});
}
mockNativeModules.NativeUnimoduleProxy.viewManagersNames.forEach(viewManagerName => {
Object.defineProperty(mockNativeModules.UIManager, `ViewManagerAdapter_${viewManagerName}`, {
get: () => ({
NativeProps: {},
directEventTypes: [],
}),
});
});
const modulesConstants = mockNativeModules.NativeUnimoduleProxy.modulesConstants;
mockNativeModules.NativeUnimoduleProxy.modulesConstants = {
...modulesConstants,
ExponentConstants: {
...modulesConstants.ExponentConstants,
...createMockConstants(),
},
};
try {
jest.mock('expo-file-system', () => ({
downloadAsync: jest.fn(() => Promise.resolve({ md5: 'md5', uri: 'uri' })),
getInfoAsync: jest.fn(() => Promise.resolve({ exists: true, md5: 'md5', uri: 'uri' })),
readAsStringAsync: jest.fn(() => Promise.resolve()),
writeAsStringAsync: jest.fn(() => Promise.resolve()),
deleteAsync: jest.fn(() => Promise.resolve()),
moveAsync: jest.fn(() => Promise.resolve()),
copyAsync: jest.fn(() => Promise.resolve()),
makeDirectoryAsync: jest.fn(() => Promise.resolve()),
readDirectoryAsync: jest.fn(() => Promise.resolve()),
createDownloadResumable: jest.fn(() => Promise.resolve()),
}));
} catch (error) {
// Allow this module to be optional for bare-workflow
if (error.code !== 'MODULE_NOT_FOUND') {
throw error;
}
}
jest.mock('react-native/Libraries/Image/AssetRegistry', () => ({
registerAsset: jest.fn(() => 1),
getAssetByID: jest.fn(() => ({
fileSystemLocation: '/full/path/to/directory',
httpServerLocation: '/assets/full/path/to/directory',
scales: [1],
fileHashes: ['md5'],
name: 'name',
exists: true,
type: 'type',
hash: 'md5',
uri: 'uri',
width: 1,
height: 1,
})),
}));
jest.doMock('react-native/Libraries/BatchedBridge/NativeModules', () => mockNativeModules);
try {
jest.mock('@unimodules/react-native-adapter', () => {
const ReactNativeAdapter = require.requireActual('@unimodules/react-native-adapter');
const { NativeModulesProxy } = ReactNativeAdapter;
// After the NativeModules mock is set up, we can mock NativeModuleProxy's functions that call
// into the native proxy module. We're not really interested in checking whether the underlying
// method is called, just that the proxy method is called, since we have unit tests for the
// adapter and believe it works correctly.
//
// NOTE: The adapter validates the number of arguments, which we don't do in the mocked functions.
// This means the mock functions will not throw validation errors the way they would in an app.
for (const moduleName of Object.keys(NativeModulesProxy)) {
const nativeModule = NativeModulesProxy[moduleName];
for (const propertyName of Object.keys(nativeModule)) {
if (typeof nativeModule[propertyName] === 'function') {
nativeModule[propertyName] = jest.fn(async () => {});
}
}
}
return ReactNativeAdapter;
});
} catch (error) {
// Allow this module to be optional for bare-workflow
if (error.code !== 'MODULE_NOT_FOUND') {
throw error;
}
}