-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetupTests.ts
65 lines (53 loc) · 1.55 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import '@testing-library/jest-dom/extend-expect';
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import { act, configure, screen, waitFor } from '@testing-library/react';
import GiantSwarm from 'giantswarm';
import * as featureFlags from 'model/featureFlags';
import nock from 'nock';
import { forceRemoveAll } from 'utils/flashMessage';
let isOnline = false;
// Let the browser know it's online, since we're disabling internet connectivity
Object.defineProperty(window.navigator, 'onLine', {
get() {
return isOnline;
},
set(value) {
isOnline = value;
},
});
window.scrollTo = jest.fn();
configure({
asyncUtilTimeout: 4500,
});
beforeAll(() => {
featureFlags.init();
nock.disableNetConnect();
// initialize a GiantSwarm api client
// this is usually done trough file src/components/Layout.js
const defaultClient = GiantSwarm.ApiClient.instance;
defaultClient.basePath = window.config.apiEndpoint;
defaultClient.timeout = 10000;
});
afterAll(() => {
nock.enableNetConnect();
});
afterEach(async () => {
await waitFor(() => {
const done = nock.isDone();
// Uncomment the next lines to debug hanging requests
const pendingMocks = nock.pendingMocks().map((mock) => `${mock}\n`);
if (!done) {
throw new Error(`You still have pending mocks bro:\n ${pendingMocks}`);
}
expect(done).toBeTruthy();
});
nock.cleanAll();
await act(async () => {
forceRemoveAll();
await waitFor(() =>
expect(screen.queryByRole('status')).not.toBeInTheDocument()
);
});
jest.clearAllTimers();
});