-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreact-native.ts
More file actions
101 lines (90 loc) · 2.67 KB
/
Copy pathreact-native.ts
File metadata and controls
101 lines (90 loc) · 2.67 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
type Listener = {eventName: string; callback: (data: any) => void};
function createMockEmitter() {
let listeners: Listener[] = [];
const addListener = jest.fn(
(eventName: string, callback: (data: any) => void) => {
listeners.push({eventName, callback});
return {remove: jest.fn()};
},
);
const removeAllListeners = jest.fn((eventName?: string) => {
if (eventName) {
listeners = listeners.filter(l => l.eventName !== eventName);
} else {
listeners = [];
}
});
const emit = jest.fn((eventName: string, data: any) => {
const callbacks = listeners
.filter(l => l.eventName === eventName)
.map(l => l.callback);
callbacks.forEach(cb => cb(data));
// Clear listeners after emit to avoid cross-test leakage
listeners = [];
});
return {addListener, removeAllListeners, emit};
}
const requireNativeComponent = (..._args: any[]) => {
const React = require('react');
return (props: any) =>
React.createElement('View', {
...props,
testID: props?.testID ?? 'accelerated-checkout-buttons',
});
};
const codegenNativeComponent = requireNativeComponent;
const StyleSheet = {
flatten: jest.fn(style => style),
};
const exampleConfig = {
colorScheme: 'automatic',
logLevel: 'error',
};
const shopifyCheckoutKitEventEmitter = createMockEmitter();
const ShopifyCheckoutKit = {
version: '0.7.0',
getConstants: jest.fn(() => ({
version: '0.7.0',
dispatchEventTypes: ['close', 'fail', 'geolocationRequest'],
})),
onDispatch: jest.fn((callback: (envelopeJson: string) => void) =>
shopifyCheckoutKitEventEmitter.addListener('onDispatch', callback),
),
preload: jest.fn(),
present: jest.fn(),
dismiss: jest.fn(),
invalidateCache: jest.fn(),
getConfig: jest.fn(() => exampleConfig),
setConfig: jest.fn(),
respondToGeolocationRequest: jest.fn(),
configureAcceleratedCheckouts: jest.fn(() => true),
isAcceleratedCheckoutAvailable: jest.fn(() => true),
isApplePayAvailable: jest.fn(() => true),
addListener: jest.fn(),
removeListeners: jest.fn(),
};
// CommonJS export for Jest manual mock resolution
module.exports = {
Platform: {OS: 'ios'},
PermissionsAndroid: {
requestMultiple: jest.fn(async () => ({})),
},
NativeEventEmitter: jest.fn(() => shopifyCheckoutKitEventEmitter),
requireNativeComponent,
codegenNativeComponent,
TurboModuleRegistry: {
getEnforcing: jest.fn((name: string) => {
if (name === 'ShopifyCheckoutKit') {
return ShopifyCheckoutKit;
}
return null;
}),
},
NativeModules: {
ShopifyCheckoutKit: {
...ShopifyCheckoutKit,
eventEmitter: shopifyCheckoutKitEventEmitter,
},
},
StyleSheet,
};