-
Notifications
You must be signed in to change notification settings - Fork 105
/
jestSetup.js
169 lines (140 loc) · 5.05 KB
/
jestSetup.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
/* eslint-disable @typescript-eslint/no-var-requires */
/* globals jest, NativeModules, require, global */
/**
* Set up of the testing environment
*/
import mockAsyncStorage from "@react-native-async-storage/async-storage/jest/async-storage-mock";
import mockClipboard from "@react-native-clipboard/clipboard/jest/clipboard-mock.js";
import nodeFetch from "node-fetch";
import { NativeModules, AccessibilityInfo } from "react-native";
import mockRNDeviceInfo from "react-native-device-info/jest/react-native-device-info-mock";
import mockZendesk from "./ts/__mocks__/io-react-native-zendesk.ts";
import "react-native-get-random-values";
jest.mock("@pagopa/io-react-native-zendesk", () => mockZendesk);
jest.mock("@react-native-async-storage/async-storage", () => mockAsyncStorage);
jest.mock("@react-native-community/push-notification-ios", () => jest.fn());
jest.mock("@react-native-cookies/cookies", () => jest.fn());
jest.mock("react-native-share", () => jest.fn());
jest.mock("@react-native-clipboard/clipboard", () => mockClipboard);
jest.mock("react-native-reanimated", () => {
const Reanimated = require("react-native-reanimated/mock");
// The mock misses the `addWhitelistedUIProps` implementation
// So we override it with a no-op
// eslint-disable-next-line functional/immutable-data,@typescript-eslint/no-empty-function, prettier/prettier
Reanimated.default.addWhitelistedUIProps = () => {};
return {
...Reanimated,
useScrollViewOffset: jest.fn,
useReducedMotion: jest.fn
};
});
jest.mock("react-native-blob-util", () => ({
DocumentDir: () => jest.fn(),
polyfill: () => jest.fn()
}));
// eslint-disable-next-line functional/immutable-data
NativeModules.PlatformConstants = NativeModules.PlatformConstants || {
forceTouchAvailable: false
};
// We need to override the global fetch and AbortController to make the tests
// compatible with node-fetch
const {
AbortController
} = require("abortcontroller-polyfill/dist/cjs-ponyfill");
// eslint-disable-next-line functional/immutable-data
global.fetch = nodeFetch;
// eslint-disable-next-line functional/immutable-data
global.AbortController = AbortController;
jest.mock("remark-directive", () => jest.fn());
jest.mock("remark-rehype", () => jest.fn());
jest.mock("rehype-stringify", () => jest.fn());
jest.mock("rehype-format", () => jest.fn());
jest.mock("unist-util-visit", () => jest.fn());
jest.mock("hastscript", () => jest.fn());
jest.mock("react-native-device-info", () => mockRNDeviceInfo);
// eslint-disable-next-line no-underscore-dangle, functional/immutable-data
global.__reanimatedWorkletInit = () => jest.fn();
jest.mock("@gorhom/bottom-sheet", () => {
const rn = require("react-native");
return {
__esModule: true,
BottomSheetModal: rn.Modal,
BottomSheetScrollView: rn.ScrollView,
TouchableWithoutFeedback: rn.TouchableWithoutFeedback,
useBottomSheetModal: () => ({
dismissAll: jest.fn()
}),
namedExport: {
...require("react-native-reanimated/mock"),
...jest.requireActual("@gorhom/bottom-sheet")
}
};
});
jest.mock("react-native-device-info", () => mockRNDeviceInfo);
jest.mock("react-native-pdf", () => jest.fn());
jest.mock("react-native-permissions", () =>
require("react-native-permissions/mock")
);
/*
* Turbo modules mocks.
*/
jest.mock("react-native/Libraries/TurboModule/TurboModuleRegistry", () => {
const turboModuleRegistry = jest.requireActual(
"react-native/Libraries/TurboModule/TurboModuleRegistry"
);
return {
...turboModuleRegistry,
getEnforcing: name => {
// List of TurboModules libraries to mock.
const modulesToMock = [
"RNDocumentPicker",
"RNHapticFeedback",
"RNCWebViewModule"
];
if (modulesToMock.includes(name)) {
return null;
}
return turboModuleRegistry.getEnforcing(name);
}
};
});
jest.mock("mixpanel-react-native", () => ({
__esModule: true,
default: () => jest.fn(),
Mixpanel: jest.fn(() => ({
init: jest.fn()
}))
}));
jest.mock("react-native", () => {
const RN = jest.requireActual("react-native"); // use original implementation, which comes with mocks out of the box
// eslint-disable-next-line functional/immutable-data
RN.NativeModules.JailMonkey = jest.requireActual("jail-monkey");
return RN;
});
// eslint-disable-next-line functional/immutable-data
NativeModules.CameraView = {
getConstants: jest.fn()
};
jest.mock("react-native-vision-camera", () => ({
useCodeScanner: jest.fn(() => ({
codeTypes: ["qr", "data-matrix"],
onCodeScanned: jest.fn()
}))
}));
/* Force the useBoldTextEnabled to return false to resolve tests */
jest.mock("@pagopa/io-app-design-system", () => {
const actual = jest.requireActual("@pagopa/io-app-design-system");
return {
...actual,
useBoldTextEnabled: jest.fn(() => Promise.resolve(false))
};
});
jest
.spyOn(AccessibilityInfo, "isBoldTextEnabled")
.mockImplementation(() => Promise.resolve(false));
/**
* NefInfo's `fetch` method mock
*/
jest.mock("@react-native-community/netinfo", () => ({
fetch: jest.fn().mockResolvedValue({ isConnected: true })
}));