-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock.ts
63 lines (55 loc) · 1.95 KB
/
mock.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { ADDED_CONNECTIONS_RESPONSE } from './responses/with-added-connections';
import { ADD_CONNECTION_RESPONSE } from './responses/add-connection';
import { CONFIG } from './responses/config';
const fetchMock = (response?: any) => {
(window.fetch as any).mockImplementation((url: string) => {
if (
url === 'https://unify.apideck.com/vault/connections/crm/activecampaign'
) {
return { json: async () => ADD_CONNECTION_RESPONSE };
}
if (url.includes('/config')) {
return { json: async () => CONFIG };
}
return {
ok: true,
status: 200,
json: async () => response || ADDED_CONNECTIONS_RESPONSE,
};
});
};
const fetchMockRejected = (response) => {
window.fetch = jest.fn().mockImplementation(() => Promise.reject(response));
};
// Utility function that mocks the `IntersectionObserver` API. Necessary for components that rely
// on it, otherwise the tests will crash. Recommended to execute inside `beforeEach`.
const setupIntersectionObserverMock = ({
root = null,
rootMargin = '',
thresholds = [],
disconnect = () => null,
observe = () => null,
takeRecords = () => [],
unobserve = () => null,
} = {}): void => {
class MockIntersectionObserver implements IntersectionObserver {
readonly root: Element | null = root;
readonly rootMargin: string = rootMargin;
readonly thresholds: ReadonlyArray<number> = thresholds;
disconnect: () => void = disconnect;
observe: (target: Element) => void = observe;
takeRecords: () => IntersectionObserverEntry[] = takeRecords;
unobserve: (target: Element) => void = unobserve;
}
Object.defineProperty(window, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver,
});
Object.defineProperty(global, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver,
});
};
export { fetchMock, fetchMockRejected, setupIntersectionObserverMock };