Split handlers out into separate files

This commit is contained in:
Tom Ratcliffe 2024-04-26 11:19:14 +01:00 committed by Tom Ratcliffe
parent f419c9b53a
commit 0dc003aadc
5 changed files with 51 additions and 50 deletions

View File

@ -1,9 +1,12 @@
import { http, HttpResponse } from 'msw';
import { SetupServer } from 'msw/node';
import { mockAlertmanagerAlert } from 'app/features/alerting/unified/mocks';
import {
AlertmanagerChoice,
AlertManagerCortexConfig,
AlertState,
ExternalAlertmanagersResponse,
} from '../../../../plugins/datasource/alertmanager/types';
import { AlertmanagersChoiceResponse } from '../api/alertmanagerApi';
@ -13,8 +16,12 @@ export const defaultAlertmanagerChoiceResponse: AlertmanagersChoiceResponse = {
alertmanagersChoice: AlertmanagerChoice.Internal,
numExternalAlertmanagers: 0,
};
export const alertmanagerChoiceHandler = (response = defaultAlertmanagerChoiceResponse) =>
http.get('/api/v1/ngalert', () => HttpResponse.json(response));
export function mockAlertmanagerChoiceResponse(server: SetupServer, response: AlertmanagersChoiceResponse) {
server.use(http.get('/api/v1/ngalert', () => HttpResponse.json(response)));
server.use(alertmanagerChoiceHandler(response));
}
export const emptyExternalAlertmanagersResponse: ExternalAlertmanagersResponse = {
@ -38,3 +45,17 @@ export function mockAlertmanagerConfigResponse(
)
);
}
export const alertmanagerAlertsListHandler = () =>
http.get('/api/alertmanager/:datasourceUid/api/v2/alerts', () =>
HttpResponse.json([
mockAlertmanagerAlert({
labels: { foo: 'bar', buzz: 'bazz' },
status: { state: AlertState.Suppressed, silencedBy: ['12345'], inhibitedBy: [] },
}),
mockAlertmanagerAlert({
labels: { foo: 'bar', buzz: 'bazz' },
status: { state: AlertState.Suppressed, silencedBy: ['12345'], inhibitedBy: [] },
}),
])
);

View File

@ -0,0 +1,5 @@
import { HttpResponse, http } from 'msw';
// TODO: Add more accurate endpoint responses as tests require
export const datasourceBuildInfoHandler = () =>
http.get('/api/datasources/proxy/uid/:datasourceUid/api/v1/status/buildinfo', () => HttpResponse.json({}));

View File

@ -1,5 +1,5 @@
import server from 'app/features/alerting/unified/mockApi';
import { alertmanagerChoiceHandler } from 'app/features/alerting/unified/mocks/server/handlers';
import { alertmanagerChoiceHandler } from 'app/features/alerting/unified/mocks/alertmanagerApi';
import { AlertmanagerChoice } from 'app/plugins/datasource/alertmanager/types';
/**

View File

@ -1,53 +1,13 @@
/**
* Contains definitions for all handlers that are required for test rendering of components within Alerting
* Contains all handlers that are required for test rendering of components within Alerting
*/
import { HttpResponse, http } from 'msw';
import { mockAlertmanagerAlert, mockSilences } from 'app/features/alerting/unified/mocks';
import { defaultAlertmanagerChoiceResponse } from 'app/features/alerting/unified/mocks/alertmanagerApi';
import { AlertState } from 'app/plugins/datasource/alertmanager/types';
///////////////////
// Alertmanagers //
///////////////////
export const alertmanagerChoiceHandler = (response = defaultAlertmanagerChoiceResponse) =>
http.get('/api/v1/ngalert', () => HttpResponse.json(response));
const alertmanagerAlertsListHandler = () =>
http.get('/api/alertmanager/:datasourceUid/api/v2/alerts', () =>
HttpResponse.json([
mockAlertmanagerAlert({
labels: { foo: 'bar', buzz: 'bazz' },
status: { state: AlertState.Suppressed, silencedBy: ['12345'], inhibitedBy: [] },
}),
mockAlertmanagerAlert({
labels: { foo: 'bar', buzz: 'bazz' },
status: { state: AlertState.Suppressed, silencedBy: ['12345'], inhibitedBy: [] },
}),
])
);
/////////////////
// Datasources //
/////////////////
// TODO: Add more accurate endpoint responses as tests require
const datasourceBuildInfoHandler = () =>
http.get('/api/datasources/proxy/uid/:datasourceUid/api/v1/status/buildinfo', () => HttpResponse.json({}));
//////////////
// Silences //
//////////////
const silencesListHandler = (silences = mockSilences) =>
http.get('/api/alertmanager/:datasourceUid/api/v2/silences', () => HttpResponse.json(silences));
const createSilenceHandler = () =>
http.post('/api/alertmanager/:datasourceUid/api/v2/silences', () =>
HttpResponse.json({ silenceId: '4bda5b38-7939-4887-9ec2-16323b8e3b4e' })
);
import {
alertmanagerAlertsListHandler,
alertmanagerChoiceHandler,
} from 'app/features/alerting/unified/mocks/alertmanagerApi';
import { datasourceBuildInfoHandler } from 'app/features/alerting/unified/mocks/datasources';
import { silenceCreateHandler, silencesListHandler } from 'app/features/alerting/unified/mocks/silences';
/**
* All mock handlers that are required across Alerting tests
@ -55,7 +15,7 @@ const createSilenceHandler = () =>
const allHandlers = [
alertmanagerChoiceHandler(),
silencesListHandler(),
createSilenceHandler(),
silenceCreateHandler(),
alertmanagerAlertsListHandler(),
datasourceBuildInfoHandler(),
];

View File

@ -0,0 +1,15 @@
import { HttpResponse, http } from 'msw';
import { mockSilences } from 'app/features/alerting/unified/mocks';
//////////////
// Silences //
//////////////
export const silencesListHandler = (silences = mockSilences) =>
http.get('/api/alertmanager/:datasourceUid/api/v2/silences', () => HttpResponse.json(silences));
export const silenceCreateHandler = () =>
http.post('/api/alertmanager/:datasourceUid/api/v2/silences', () =>
HttpResponse.json({ silenceId: '4bda5b38-7939-4887-9ec2-16323b8e3b4e' })
);