-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotification.test.tsx
177 lines (131 loc) · 5.66 KB
/
Notification.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
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
170
171
172
173
174
175
176
177
import React from 'react';
import { Notification } from './Notification';
import { render, waitFor, screen, user, mockFetchCPUAverage } from 'test-utils';
import {
DEFAULT_ALERT_DELTA,
fetchCPUAverageAsync,
setThreshold,
setAlertDelta,
DEFAULT_THRESHOLD,
} from './metricsSlice';
let restoreNotification: any;
beforeAll(() => {
restoreNotification = window.Notification as any;
});
afterAll(() => {
window.Notification = restoreNotification;
});
beforeEach(() => {
// @ts-expect-error
window.Notification = jest.fn();
// @ts-expect-error
window.Notification.permission = 'granted';
});
const TIMESTAMP = 100000000000000;
const THRESHOLD = 0.7;
test('prompt Notification', async () => {
const requestPermission = jest.fn().mockReturnValue(true);
// @ts-expect-error
window.Notification = {
requestPermission,
};
render(<Notification />);
await user.click(screen.getByTestId('notification'));
await waitFor(() => {
expect(requestPermission).toHaveBeenCalledTimes(1);
});
});
test('full notification cycle', async () => {
const { store } = render(<Notification />);
expect(window.Notification).not.toHaveBeenCalled();
mockFetchCPUAverage(TIMESTAMP, DEFAULT_THRESHOLD + 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).not.toHaveBeenCalled();
})
mockFetchCPUAverage(TIMESTAMP + DEFAULT_ALERT_DELTA, DEFAULT_THRESHOLD + 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).toHaveBeenNthCalledWith(1, 'Your CPU has been over 1 load for over 2 minute(s) or more');
})
mockFetchCPUAverage(TIMESTAMP + DEFAULT_ALERT_DELTA + (DEFAULT_ALERT_DELTA / 2), DEFAULT_THRESHOLD - 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).not.toHaveBeenNthCalledWith(2, 'Your CPU has recovered from 1 load');
})
mockFetchCPUAverage(TIMESTAMP + DEFAULT_ALERT_DELTA + (DEFAULT_ALERT_DELTA / 2) + DEFAULT_ALERT_DELTA, DEFAULT_THRESHOLD - 0.2);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).toHaveBeenNthCalledWith(2, 'Your CPU has recovered from 1 load');
})
});
test('does not notify if cpu load does not last more than 2 minutes', async () => {
const { store } = render(<Notification />);
expect(window.Notification).not.toHaveBeenCalled();
const customThreshold = 0.5;
store.dispatch(setThreshold(customThreshold));
mockFetchCPUAverage(TIMESTAMP, customThreshold);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).not.toHaveBeenCalled();
})
mockFetchCPUAverage(TIMESTAMP + DEFAULT_ALERT_DELTA - 1, customThreshold + 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).not.toHaveBeenCalled();
})
});
test('does not notify if cpu load oscilates between up and down for no more than alertDelta', async () => {
const { store } = render(<Notification />);
expect(window.Notification).not.toHaveBeenCalled();
const customAlertDelta = 30 * 1000;
const customThreshold = 0.7;
store.dispatch(setThreshold(customThreshold));
store.dispatch(setAlertDelta(customAlertDelta));
mockFetchCPUAverage(TIMESTAMP, customThreshold + 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).not.toHaveBeenCalled();
})
mockFetchCPUAverage(TIMESTAMP + customAlertDelta - 1, customThreshold + 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).not.toHaveBeenCalled();
})
// this will just reset the curret peak, no notification should be sent
mockFetchCPUAverage(TIMESTAMP + customAlertDelta * 2, customThreshold - 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).not.toHaveBeenCalled();
})
// start a new peak
mockFetchCPUAverage(TIMESTAMP + customAlertDelta * 2 + 1, customThreshold + 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).not.toHaveBeenCalled();
})
// accept it
mockFetchCPUAverage(TIMESTAMP + customAlertDelta * 2 + 1 + customAlertDelta, customThreshold + 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).toHaveBeenNthCalledWith(1, 'Your CPU has been over 0.7 load for over 0.5 minute(s) or more');
})
// try to recover
mockFetchCPUAverage(TIMESTAMP + customAlertDelta * 2 + 1 + customAlertDelta + 1, customThreshold - 0.1);
await store.dispatch(fetchCPUAverageAsync());
expect(window.Notification).not.toHaveBeenNthCalledWith(2, 'Your CPU has recovered from 0.7 load');
// go up again (reseting previous state)
mockFetchCPUAverage(TIMESTAMP + customAlertDelta * 2 + 1 + customAlertDelta + 1 + 1, customThreshold + 0.1);
await store.dispatch(fetchCPUAverageAsync());
expect(window.Notification).not.toHaveBeenNthCalledWith(2, 'Your CPU has recovered from 0.7 load');
// go down again starting
mockFetchCPUAverage(TIMESTAMP + customAlertDelta * 2 + 1 + customAlertDelta + 1 + 1 + 1, customThreshold - 0.1);
await store.dispatch(fetchCPUAverageAsync());
expect(window.Notification).not.toHaveBeenNthCalledWith(2, 'Your CPU has recovered from 0.7 load');
// finally recover
mockFetchCPUAverage(TIMESTAMP + customAlertDelta * 2 + 1 + customAlertDelta + 1 + 1 + 1 + customAlertDelta, customThreshold - 0.1);
await store.dispatch(fetchCPUAverageAsync());
await waitFor(() => {
expect(window.Notification).toHaveBeenNthCalledWith(2, 'Your CPU has recovered from 0.7 load');
})
});