-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathDashboardAlerts.test.tsx
More file actions
136 lines (135 loc) · 4.25 KB
/
DashboardAlerts.test.tsx
File metadata and controls
136 lines (135 loc) · 4.25 KB
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
import { render } from './__TEST__/util';
import DashboardAlerts from './DashboardAlerts';
import { fireEvent } from '@testing-library/react';
import { useAlerts, useAlertLibrary } from '../containers/AlertProvider';
const topLevelAlert = [
{
id: '3',
severity: 'critical',
labels: {
alertname: 'ClusterAtRisk',
severity: 'critical',
},
childrenJsonPath:
"$[?((@.labels.alertname === 'NodeAtRisk' && @.labels.severity === 'critical') || (@.labels.alertname === 'PlatformServicesAtRisk' && @.labels.severity === 'critical') || (@.labels.alertname === 'VolumeAtRisk' && @.labels.severity === 'critical'))]",
},
];
const alerts = [
{
id: '1',
severity: 'critical',
labels: {
alertname: 'VolumeAtRisk',
severity: 'critical',
},
childrenJsonPath:
"$[?((@.labels.alertname === 'KubePersistentVolumeFillingUp' && @.labels.severity === 'critical') || (@.labels.alertname === 'KubePersistentVolumeErrors' && @.labels.severity === 'critical'))]",
},
{
id: '2',
severity: 'critical',
labels: {
alertname: 'KubePersistentVolumeFillingUp',
severity: 'critical',
},
childrenJsonPath: '',
},
...topLevelAlert,
];
const mockOpenLink = jest.fn();
jest.mock('../containers/ConfigProvider', () => ({
__esModule: true,
default: ({ children }) => <>{children}</>,
useLinkOpener: () => ({
openLink: mockOpenLink,
}),
useDiscoveredViews: () => [
{
app: {
kind: '',
name: '',
version: '',
url: '',
appHistoryBasePath: '',
},
isFederated: true,
view: {
path: '/alerts',
},
},
],
}));
describe('the dashboard alerts sub-panel', () => {
beforeEach(() => {
mockOpenLink.mockReset();
});
test('should display the number of alerts', () => {
(useAlertLibrary as any).mockImplementation(() => ({
getPlatformAlertSelectors: () => {
return {
alertname: ['ClusterAtRisk', 'ClusterDegraded'],
};
},
}));
// topLevelAlerts
(useAlerts as any).mockImplementationOnce(() => ({
alerts: topLevelAlert,
}));
// all the alerts
(useAlerts as any).mockImplementationOnce(() => ({
alerts: alerts,
}));
const { getByTestId } = render(<DashboardAlerts />);
expect(getByTestId('all-alert-badge')).toHaveTextContent('1');
expect(getByTestId('warning-alert-badge')).toHaveTextContent('0');
expect(getByTestId('critical-alert-badge')).toHaveTextContent('1');
expect(getByTestId('view-all-link')).toBeInTheDocument();
});
test('should display no active alerts message if there is no alert', () => {
(useAlerts as any).mockImplementation(() => ({
alerts: [],
}));
const { queryByTestId, getByTestId, getByText } = render(
<DashboardAlerts />,
);
expect(getByText('No active alerts')).toBeInTheDocument();
expect(getByTestId('all-alert-badge')).toHaveTextContent('0');
expect(queryByTestId('warning-alert-badge')).not.toBeInTheDocument();
expect(queryByTestId('critical-alert-badge')).not.toBeInTheDocument();
expect(queryByTestId('view-all-link')).not.toBeInTheDocument();
});
test('should display a warning banner when alerts fail to fetch', () => {
(useAlertLibrary as any).mockImplementation(() => ({
getPlatformAlertSelectors: () => ({
alertname: ['ClusterAtRisk', 'ClusterDegraded'],
}),
}));
(useAlerts as any).mockImplementation(() => ({
alerts: [],
error: new Error('Alert manager responded with 401'),
}));
const { getByText } = render(<DashboardAlerts />);
expect(
getByText('Monitoring information unavailable'),
).toBeInTheDocument();
expect(
getByText('Some data can not be well retrieved'),
).toBeInTheDocument();
});
test('should redirect to alert page with View All link', () => {
(useAlerts as any).mockImplementation(() => ({
alerts: alerts,
}));
const { getByTestId } = render(<DashboardAlerts />);
const viewAllLink = getByTestId('view-all-link');
fireEvent.click(viewAllLink);
expect(mockOpenLink).toHaveBeenCalledTimes(1);
expect(mockOpenLink).toHaveBeenCalledWith(
expect.objectContaining({
view: {
path: '/alerts',
},
}),
);
});
});