-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsetupTests.ts
47 lines (45 loc) · 1.09 KB
/
setupTests.ts
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
import { Image } from 'react-native';
import {
mockUriGood,
mockUriBad,
mockUriSlowGood,
mockUriSlowBad,
mockResourceGood,
mockWidth,
mockHeight,
} from './src/__fixtures__';
jest.spyOn(Image, 'getSize').mockImplementation((uri, onLoad = () => {}, onError = () => {}) => {
switch (uri) {
case mockUriGood: {
onLoad(mockWidth, mockHeight);
break;
}
case mockUriBad: {
onError(new Error(uri));
break;
}
case mockUriSlowGood: {
setImmediate(() => {
onLoad(mockWidth, mockHeight);
});
break;
}
case mockUriSlowBad: {
setImmediate(() => {
onError(uri);
});
break;
}
default: {
throw new Error(`Unexpected URI value in test: ${uri}`);
}
}
});
// @ts-expect-error: contrary to what TS thinks, the current implementation of
// resolveAssetSource _can_ return `null`, so this is a valid mock.
jest.spyOn(Image, 'resolveAssetSource').mockImplementation((source) => {
if (source === mockResourceGood) {
return { width: mockWidth, height: mockHeight };
}
return null;
});