diff --git a/public/app/features/alerting/unified/mocks/alertmanagerApi.ts b/public/app/features/alerting/unified/mocks/alertmanagerApi.ts index 1db4862f414..b5ad61cc0b1 100644 --- a/public/app/features/alerting/unified/mocks/alertmanagerApi.ts +++ b/public/app/features/alerting/unified/mocks/alertmanagerApi.ts @@ -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: [] }, + }), + ]) + ); diff --git a/public/app/features/alerting/unified/mocks/datasources.ts b/public/app/features/alerting/unified/mocks/datasources.ts new file mode 100644 index 00000000000..0ff5d12bf65 --- /dev/null +++ b/public/app/features/alerting/unified/mocks/datasources.ts @@ -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({})); diff --git a/public/app/features/alerting/unified/mocks/server/configure.ts b/public/app/features/alerting/unified/mocks/server/configure.ts index 27e36830616..04b17f571ac 100644 --- a/public/app/features/alerting/unified/mocks/server/configure.ts +++ b/public/app/features/alerting/unified/mocks/server/configure.ts @@ -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'; /** diff --git a/public/app/features/alerting/unified/mocks/server/handlers.ts b/public/app/features/alerting/unified/mocks/server/handlers.ts index aea95b81cf5..78bc9baa86c 100644 --- a/public/app/features/alerting/unified/mocks/server/handlers.ts +++ b/public/app/features/alerting/unified/mocks/server/handlers.ts @@ -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(), ]; diff --git a/public/app/features/alerting/unified/mocks/silences.ts b/public/app/features/alerting/unified/mocks/silences.ts new file mode 100644 index 00000000000..e23e6ea9ded --- /dev/null +++ b/public/app/features/alerting/unified/mocks/silences.ts @@ -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' }) + );