Skip to content

Commit b9ce5f8

Browse files
committed
test: added unit tests for fetchDataForIncidentsAndAlerts
1 parent 58cdea9 commit b9ce5f8

1 file changed

Lines changed: 70 additions & 1 deletion

File tree

web/src/components/Incidents/api.spec.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
},
88
};
99

10-
import { createAlertsQuery } from './api';
10+
import { createAlertsQuery, fetchDataForIncidentsAndAlerts } from './api';
11+
import { PrometheusResponse } from '@openshift-console/dynamic-plugin-sdk';
12+
import { buildPrometheusUrl } from '../utils';
1113

1214
// Mock the SDK
1315
jest.mock('@openshift-console/dynamic-plugin-sdk', () => ({
@@ -78,3 +80,70 @@ describe('createAlertsQuery', () => {
7880
]);
7981
});
8082
});
83+
84+
describe('fetchDataForIncidentsAndAlerts', () => {
85+
it('should fetch data for incidents and alerts', async () => {
86+
(buildPrometheusUrl as jest.Mock).mockReturnValue('/mock/url');
87+
const now = Date.now();
88+
89+
const result1 = {
90+
metric: {
91+
alertname: 'test',
92+
severity: 'critical',
93+
namespace: 'test',
94+
},
95+
values: [
96+
[now - 1000, '1'],
97+
[now - 500, '2'],
98+
] as [number, string][],
99+
};
100+
101+
const result2 = {
102+
metric: {
103+
alertname: 'test2',
104+
severity: 'warning',
105+
namespace: 'test2',
106+
},
107+
values: [
108+
[now - 2000, '3'],
109+
[now - 1500, '4'],
110+
] as [number, string][],
111+
};
112+
113+
const mockPrometheusResponse1: PrometheusResponse = {
114+
status: 'success',
115+
data: {
116+
resultType: 'matrix',
117+
result: [result1],
118+
},
119+
};
120+
121+
const mockPrometheusResponse2: PrometheusResponse = {
122+
status: 'success',
123+
data: {
124+
resultType: 'matrix',
125+
result: [result2],
126+
},
127+
};
128+
129+
const fetch = jest
130+
.fn()
131+
.mockResolvedValueOnce(mockPrometheusResponse1)
132+
.mockResolvedValueOnce(mockPrometheusResponse2);
133+
134+
const range = { endTime: now, duration: 86400000 };
135+
const customQuery = [
136+
'ALERTS{alertname="test", severity="critical", namespace="test"}',
137+
'ALERTS{alertname="test2", severity="warning", namespace="test2"}',
138+
];
139+
const result = await fetchDataForIncidentsAndAlerts(fetch, range, customQuery);
140+
expect(result).toEqual({
141+
status: 'success',
142+
data: {
143+
resultType: 'matrix',
144+
result: [result1, result2],
145+
},
146+
});
147+
expect(fetch).toHaveBeenCalledTimes(2);
148+
});
149+
});

0 commit comments

Comments
 (0)