Check for server received body

This is not ideal! Preference would be to have a more robust mock server
that responds to the received silence and appends it to a stateful list for the test
and then resets afterwards
This commit is contained in:
Tom Ratcliffe 2024-04-26 11:20:08 +01:00 committed by Tom Ratcliffe
parent 0dc003aadc
commit 4d4bf39184
2 changed files with 51 additions and 0 deletions

View File

@ -6,6 +6,8 @@ import { dateTime } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { config, locationService, setDataSourceSrv } from '@grafana/runtime';
import { setupMswServer } from 'app/features/alerting/unified/mockApi';
import { waitForServerRequest } from 'app/features/alerting/unified/mocks/server/events';
import { silenceCreateHandler } from 'app/features/alerting/unified/mocks/silences';
import { MatcherOperator } from 'app/plugins/datasource/alertmanager/types';
import { AccessControlAction } from 'app/types';
@ -239,6 +241,8 @@ describe('Silence create/edit', () => {
renderSilences(baseUrlPath);
expect(await ui.editor.durationField.find()).toBeInTheDocument();
const postRequest = waitForServerRequest(silenceCreateHandler());
const start = new Date();
const end = new Date(start.getTime() + 24 * 60 * 60 * 1000);
@ -266,6 +270,20 @@ describe('Silence create/edit', () => {
await userEvent.click(ui.editor.submit.get());
expect(await ui.notExpiredTable.find()).toBeInTheDocument();
const createSilenceRequest = await postRequest;
const requestBody = await createSilenceRequest.clone().json();
expect(requestBody).toMatchObject(
expect.objectContaining({
comment: expect.stringMatching(/created (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/),
matchers: [
{ isEqual: true, isRegex: false, name: 'foo', value: 'bar' },
{ isEqual: false, isRegex: false, name: 'bar', value: 'buzz' },
{ isEqual: true, isRegex: true, name: 'region', value: 'us-west-.*' },
{ isEqual: false, isRegex: true, name: 'env', value: 'dev|staging' },
],
})
);
},
TEST_TIMEOUT
);
@ -275,6 +293,8 @@ describe('Silence create/edit', () => {
async () => {
const user = userEvent.setup();
const postRequest = waitForServerRequest(silenceCreateHandler());
renderSilences(`${baseUrlPath}?alertmanager=Alertmanager`);
await waitFor(() => expect(ui.editor.durationField.query()).not.toBeNull());
@ -285,6 +305,14 @@ describe('Silence create/edit', () => {
expect(await ui.notExpiredTable.find()).toBeInTheDocument();
expect(locationService.getSearch().get('alertmanager')).toBe('Alertmanager');
const createSilenceRequest = await postRequest;
const requestBody = await createSilenceRequest.clone().json();
expect(requestBody).toMatchObject(
expect.objectContaining({
matchers: [{ isEqual: true, isRegex: false, name: 'foo', value: 'bar' }],
})
);
},
TEST_TIMEOUT
);

View File

@ -0,0 +1,23 @@
import { HttpHandler, matchRequestUrl } from 'msw';
import server from 'app/features/alerting/unified/mockApi';
/**
* Wait for the mock server to receive a request for the given method + url combination,
* and resolve with information about the request that was made
*
* @deprecated Try not to use this 🙏 instead aim to assert against UI side effects
*/
export function waitForServerRequest(handler: HttpHandler) {
const { method, path } = handler.info;
return new Promise<Request>((resolve) => {
server.events.on('request:match', ({ request }) => {
const matchesMethod = request.method.toLowerCase() === String(method).toLowerCase();
const matchesUrl = matchRequestUrl(new URL(request.url), path);
if (matchesMethod && matchesUrl) {
resolve(request);
}
});
});
}