/
App.test.tsx
86 lines (75 loc) · 2.18 KB
/
App.test.tsx
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
import { createMount } from '@material-ui/core/test-utils';
import axios from 'axios';
import React from 'react';
import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';
import { Provider } from 'react-redux';
import * as singleSpa from 'single-spa';
import App from './App';
import { flushPromises } from './setupTests';
import { loadAuthProvider } from './state/actions/scigateway.actions';
describe('App', () => {
let mount;
beforeEach(() => {
mount = createMount();
singleSpa.start();
});
afterEach(() => {
mount.cleanUp();
jest.useRealTimers();
});
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});
it('loadMaintenanceState dispatched when maintenance changes', async () => {
// this test only works with old jest fake timers
// when they remove legacy timers refactor this test to use real timers
jest.useFakeTimers('legacy');
(axios.get as jest.Mock).mockImplementation(() =>
Promise.resolve({
data: {
show: true,
message: '',
},
})
);
const wrapper = mount(<App />);
const realStore = wrapper.find(Provider).prop('store');
// Set provider to icat as that supports maintenance states
realStore.dispatch(
loadAuthProvider('icat.user/pass', 'http://localhost:8000')
);
expect(realStore.getState().scigateway.maintenance).toEqual({
show: false,
message: '',
});
jest.runOnlyPendingTimers();
await act(async () => {
await flushPromises();
wrapper.update();
});
expect(realStore.getState().scigateway.maintenance).toEqual({
show: true,
message: '',
});
(axios.get as jest.Mock).mockImplementation(() =>
Promise.resolve({
data: {
show: true,
message: 'test message',
},
})
);
jest.runOnlyPendingTimers();
await act(async () => {
await flushPromises();
wrapper.update();
});
expect(realStore.getState().scigateway.maintenance).toEqual({
show: true,
message: 'test message',
});
});
});