mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
0dc003aadc
commit
4d4bf39184
@ -6,6 +6,8 @@ import { dateTime } from '@grafana/data';
|
|||||||
import { selectors } from '@grafana/e2e-selectors';
|
import { selectors } from '@grafana/e2e-selectors';
|
||||||
import { config, locationService, setDataSourceSrv } from '@grafana/runtime';
|
import { config, locationService, setDataSourceSrv } from '@grafana/runtime';
|
||||||
import { setupMswServer } from 'app/features/alerting/unified/mockApi';
|
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 { MatcherOperator } from 'app/plugins/datasource/alertmanager/types';
|
||||||
import { AccessControlAction } from 'app/types';
|
import { AccessControlAction } from 'app/types';
|
||||||
|
|
||||||
@ -239,6 +241,8 @@ describe('Silence create/edit', () => {
|
|||||||
renderSilences(baseUrlPath);
|
renderSilences(baseUrlPath);
|
||||||
expect(await ui.editor.durationField.find()).toBeInTheDocument();
|
expect(await ui.editor.durationField.find()).toBeInTheDocument();
|
||||||
|
|
||||||
|
const postRequest = waitForServerRequest(silenceCreateHandler());
|
||||||
|
|
||||||
const start = new Date();
|
const start = new Date();
|
||||||
const end = new Date(start.getTime() + 24 * 60 * 60 * 1000);
|
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());
|
await userEvent.click(ui.editor.submit.get());
|
||||||
|
|
||||||
expect(await ui.notExpiredTable.find()).toBeInTheDocument();
|
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
|
TEST_TIMEOUT
|
||||||
);
|
);
|
||||||
@ -275,6 +293,8 @@ describe('Silence create/edit', () => {
|
|||||||
async () => {
|
async () => {
|
||||||
const user = userEvent.setup();
|
const user = userEvent.setup();
|
||||||
|
|
||||||
|
const postRequest = waitForServerRequest(silenceCreateHandler());
|
||||||
|
|
||||||
renderSilences(`${baseUrlPath}?alertmanager=Alertmanager`);
|
renderSilences(`${baseUrlPath}?alertmanager=Alertmanager`);
|
||||||
await waitFor(() => expect(ui.editor.durationField.query()).not.toBeNull());
|
await waitFor(() => expect(ui.editor.durationField.query()).not.toBeNull());
|
||||||
|
|
||||||
@ -285,6 +305,14 @@ describe('Silence create/edit', () => {
|
|||||||
expect(await ui.notExpiredTable.find()).toBeInTheDocument();
|
expect(await ui.notExpiredTable.find()).toBeInTheDocument();
|
||||||
|
|
||||||
expect(locationService.getSearch().get('alertmanager')).toBe('Alertmanager');
|
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
|
TEST_TIMEOUT
|
||||||
);
|
);
|
||||||
|
23
public/app/features/alerting/unified/mocks/server/events.ts
Normal file
23
public/app/features/alerting/unified/mocks/server/events.ts
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user