-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.tsx
More file actions
183 lines (155 loc) · 5.29 KB
/
Copy pathjest.setup.tsx
File metadata and controls
183 lines (155 loc) · 5.29 KB
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
/**
* NOTE: below line will cause lint error because jest-fetch-mock
* is added to dev-dependencies (and it should be)
* TODO: consider adjusting eslint config to exclude
* test-related files from that eslint rule
*/
// eslint-disable-next-line import/no-extraneous-dependencies
import fetchMock from 'jest-fetch-mock';
import React from 'react';
import { type ScaledSize, View } from 'react-native';
import 'react-native/jest/setup';
import 'react-native-gesture-handler/jestSetup';
import '@amazon-devices/react-native-gesture-handler/jestSetup';
import { type HWEvent } from '@amazon-devices/react-native-kepler';
import '@amazon-devices/react-native-kepler/jest/setup';
import type Icon from '@amazon-devices/react-native-vector-icons/MaterialCommunityIcons';
import '@testing-library/jest-native/extend-expect';
// Mock NativeModules before any imports
jest.mock('react-native/Libraries/BatchedBridge/NativeModules', () => ({
UIManager: {
RCTView: () => {},
},
PlatformConstants: {
getConstants: () => ({
isTesting: true,
}),
},
NativeModules: {},
}));
fetchMock.enableMocks();
jest.mock('@amazon-devices/react-native-screens', () => ({
...jest.requireActual('@amazon-devices/react-native-screens'),
enableScreens: jest.fn(),
}));
jest.mock('@react-native-async-storage/async-storage', () =>
require('@react-native-async-storage/async-storage/jest/async-storage-mock'),
);
jest.mock('@amazon-devices/react-native-device-info', () =>
require('@amazon-devices/react-native-device-info/jest/react-native-device-info-mock'),
);
const IconMock: typeof Icon = ({ ...props }) => <View {...props} />;
jest.mock(
'@amazon-devices/react-native-vector-icons/MaterialCommunityIcons',
() => IconMock,
);
jest.mock('@amazon-devices/react-native-vector-icons/MaterialIcons', () => IconMock);
// @ts-expect-error
global.navigator = {};
jest.mock('@amazon-devices/keplerscript-netmgr-lib', () =>
require('src/test-utils/mocks/keplerscript-netmgr-lib'),
);
jest.mock('@amazon-devices/asset-resolver-lib', () =>
require('src/test-utils/mocks/asset-resolver-lib'),
);
jest.mock('@amazon-devices/keplerscript-kepleri18n-lib', () =>
require('src/test-utils/mocks/keplerscript-kepleri18n-lib'),
);
jest.mock(
'@amazon-devices/react-native-kepler/Libraries/Utilities/registerGeneratedViewConfig',
() => () => {},
);
jest.mock('@amazon-devices/react-native-kepler', () => {
return {
__esModule: true,
HWEvent: jest.fn(),
KeplerAppStateChange: jest.fn(),
useCallback: jest.fn(),
useTVEventHandler: jest.fn((evt: HWEvent) => {
return evt;
}),
useAddKeplerAppStateListenerCallback: jest.fn().mockReturnValue(jest.fn()),
TVFocusGuideView: jest.fn(({ children }) => children),
default: jest.fn(),
InteractionManager: jest.requireActual(
'@amazon-devices/react-native-kepler/Libraries/Interaction/InteractionManager',
),
useHideSplashScreenCallback: jest.fn(() => () => {}),
usePreventHideSplashScreen: jest.fn(),
Switch: jest.fn(),
findNodeHandle: jest.fn(() => 1),
FocusManager: {
setNextFocus: jest.fn(),
},
Platform: {
isTV: true,
},
};
});
// Mock Platform
jest.mock('react-native/Libraries/Utilities/Platform', () => ({
OS: 'ios',
select: jest.fn((obj) => obj.ios),
}));
// Mock StyleSheet
jest.mock('react-native/Libraries/StyleSheet/StyleSheet', () => ({
create: jest.fn((styles) => ({ ...styles })),
compose: (style1: Object, style2: Object) => ({ ...style1, ...style2 }),
flatten: (style: Object) => {
const flattenStyle = require('react-native/Libraries/StyleSheet/flattenStyle');
return flattenStyle(style);
},
hairlineWidth: 1,
}));
// Mock NativeEventEmitter
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter', () => {
class MockNativeEventEmitter {
addListener = jest.fn();
removeListener = jest.fn();
emit = jest.fn();
}
return MockNativeEventEmitter;
});
const mockedDimensionsValue: ScaledSize = {
width: 375,
height: 667,
scale: 2,
fontScale: 2,
};
jest.mock('react-native/Libraries/Utilities/Dimensions', () => {
return {
default: {
get: () => mockedDimensionsValue,
},
};
});
const mockUseWindowDimensions = jest.fn(() => mockedDimensionsValue);
jest.mock('react-native/Libraries/Utilities/useWindowDimensions', () => ({
default: mockUseWindowDimensions,
}));
jest.mock('react-native/Libraries/TurboModule/TurboModuleRegistry', () => {
return {
getEnforcing: jest.fn((name) => {
switch (name) {
case 'ImageLoader':
return {
getSize: jest.fn((_url) => Promise.resolve([320, 240])),
prefetchImage: jest.fn(),
};
default:
return {};
}
}),
get: jest.fn(),
};
});
// include this section and the NativeAnimatedHelper section for mocking react-native-reanimated
jest.mock('@amazon-devices/react-native-reanimated', () => {
const Reanimated = require('@amazon-devices/react-native-reanimated/mock');
// The mock for `call` immediately calls the callback which is incorrect
// So we override it with a no-op
Reanimated.default.call = () => {};
return Reanimated;
});
// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');