-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjest.setup.js
More file actions
142 lines (122 loc) · 3.75 KB
/
jest.setup.js
File metadata and controls
142 lines (122 loc) · 3.75 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
/* eslint-disable no-undef */
jest.mock('@expo/vector-icons', () => {
const mockReact = require('react');
const mockRN = require('react-native');
const MockIcon = (props) => {
return mockReact.createElement(mockRN.Text, {
...props,
testID: props.testID || `mock-icon-${props.name}`,
accessibilityLabel: props.name,
});
};
return {
Ionicons: MockIcon,
MaterialIcons: MockIcon,
FontAwesome: MockIcon,
FontAwesome5: MockIcon,
FontAwesome6: MockIcon,
MaterialCommunityIcons: MockIcon,
Feather: MockIcon,
AntDesign: MockIcon,
Entypo: MockIcon,
SimpleLineIcons: MockIcon,
};
});
// Also mock the direct Ionicons import path
jest.mock('@expo/vector-icons/Ionicons', () => {
const mockReact = require('react');
const mockRN = require('react-native');
const MockIonicons = (props) => {
return mockReact.createElement(mockRN.Text, {
...props,
testID: props.testID || `mock-icon-${props.name}`,
accessibilityLabel: props.name,
});
};
// Export as both default and named
MockIonicons.default = MockIonicons;
return MockIonicons;
});
// Make Animated.timing complete immediately
jest.spyOn(require('react-native').Animated, 'timing').mockImplementation((value, config) => {
return {
start: (callback) => {
value.setValue(config.toValue);
callback && callback({ finished: true });
},
stop: () => {},
reset: () => {},
};
});
try {
const { ToastAndroid } = require('react-native');
if (ToastAndroid) {
ToastAndroid.show = jest.fn();
ToastAndroid.showWithGravity = jest.fn();
ToastAndroid.showWithGravityAndOffset = jest.fn();
}
} catch {}
try {
const { notifyManager } = require('@tanstack/query-core');
if (notifyManager && typeof notifyManager.setBatchNotifyFunction === 'function') {
const { act } = require('@testing-library/react-native');
notifyManager.setBatchNotifyFunction((cb) => {
act(() => {
cb();
});
});
}
} catch {}
// Mock expo-video across all tests to avoid environment issues with the native video player
jest.mock('expo-video', () => {
const addListener = jest.fn(() => ({ remove: jest.fn() }));
const React = jest.requireActual('react');
const { View } = jest.requireActual('react-native');
return {
useVideoPlayer: jest.fn(() => ({
play: jest.fn(),
pause: jest.fn(),
loop: false,
muted: true,
addListener,
currentTime: 0,
})),
VideoView: (props) => React.createElement(View, props),
};
});
jest.mock('@shopify/flash-list', () => {
const ReactActual = jest.requireActual('react');
function FlashList(props) {
const { data = [], renderItem, ListEmptyComponent, ListFooterComponent, keyExtractor } = props;
return ReactActual.createElement(
ReactActual.Fragment,
null,
data.length > 0 && renderItem
? data.map((item, index) => {
const element = renderItem({ item, index });
if (!element) return null;
const key = keyExtractor?.(item, index) ?? index;
return ReactActual.cloneElement(element, { key });
})
: ListEmptyComponent
? ReactActual.isValidElement(ListEmptyComponent)
? ListEmptyComponent
: ReactActual.createElement(ListEmptyComponent)
: null,
ListFooterComponent
? ReactActual.isValidElement(ListFooterComponent)
? ListFooterComponent
: ReactActual.createElement(ListFooterComponent)
: null
);
}
return { FlashList };
});
// Add global cleanup to handle timers and async operations
afterEach(() => {
// Clear all timers after each test
jest.clearAllTimers();
// Clear all mocks after each test
jest.clearAllMocks();
});
jest.useFakeTimers();