Reuse setupMswServer for alerting tests

This commit is contained in:
Tom Ratcliffe
2024-04-18 09:04:53 +01:00
committed by Tom Ratcliffe
parent 76c6e029a5
commit de4510cc70
5 changed files with 20 additions and 52 deletions
@@ -1,7 +1,6 @@
import 'whatwg-fetch';
import { screen, waitFor, waitForElementToBeRemoved, within } from '@testing-library/react';
import userEvent, { PointerEventsCheckLevel } from '@testing-library/user-event';
import { setupServer } from 'msw/node';
import React from 'react';
import { renderRuleEditor, ui } from 'test/helpers/alertingRuleEditor';
import { clickSelectOption } from 'test/helpers/selectOptionInTest';
@@ -9,7 +8,11 @@ import { byRole } from 'testing-library-selector';
import { setDataSourceSrv } from '@grafana/runtime';
import { contextSrv } from 'app/core/services/context_srv';
import { mockApi } from 'app/features/alerting/unified/mockApi';
import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi';
import {
defaultAlertmanagerChoiceResponse,
mockAlertmanagerChoiceResponse,
} from 'app/features/alerting/unified/mocks/alertmanagerApi';
import { DashboardSearchHit, DashboardSearchItemType } from 'app/features/search/types';
import { AccessControlAction } from 'app/types';
import { GrafanaAlertStateDecision, PromApplication } from 'app/types/unified-alerting-dto';
@@ -64,13 +67,13 @@ const mocks = {
},
};
const server = setupServer();
const server = setupMswServer();
const getLabelInput = (selector: HTMLElement) => within(selector).getByRole('combobox');
describe('RuleEditor grafana managed rules', () => {
beforeEach(() => {
mockApi(server).eval({ results: {} });
server.listen();
mockAlertmanagerChoiceResponse(server, defaultAlertmanagerChoiceResponse);
jest.clearAllMocks();
contextSrv.isEditor = true;
contextSrv.hasEditPermissionInFolders = true;
@@ -89,14 +92,6 @@ describe('RuleEditor grafana managed rules', () => {
]);
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});
it('can create new grafana managed alert', async () => {
const dataSources = {
default: mockDataSource(
@@ -1,7 +1,6 @@
import 'whatwg-fetch';
import { screen, waitFor, waitForElementToBeRemoved, within } from '@testing-library/react';
import userEvent, { PointerEventsCheckLevel } from '@testing-library/user-event';
import { setupServer } from 'msw/node';
import React from 'react';
import { renderRuleEditor, ui } from 'test/helpers/alertingRuleEditor';
import { clickSelectOption } from 'test/helpers/selectOptionInTest';
@@ -9,7 +8,7 @@ import { byText } from 'testing-library-selector';
import { setDataSourceSrv } from '@grafana/runtime';
import { contextSrv } from 'app/core/services/context_srv';
import { mockApi } from 'app/features/alerting/unified/mockApi';
import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi';
import { AccessControlAction } from 'app/types';
import { PromApplication } from 'app/types/unified-alerting-dto';
@@ -96,12 +95,11 @@ const mocks = {
const getLabelInput = (selector: HTMLElement) => within(selector).getByRole('combobox');
const server = setupServer();
const server = setupMswServer();
describe('RuleEditor recording rules', () => {
beforeEach(() => {
mockApi(server).eval({ results: {} });
server.listen();
jest.clearAllMocks();
contextSrv.isEditor = true;
contextSrv.hasEditPermissionInFolders = true;
@@ -120,14 +118,6 @@ describe('RuleEditor recording rules', () => {
]);
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});
it('can create a new cloud recording rule', async () => {
setDataSourceSrv(new MockDataSourceSrv(dataSources));
mocks.getAllDataSources.mockReturnValue(Object.values(dataSources));
@@ -1,7 +1,6 @@
import 'whatwg-fetch';
import { render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { setupServer } from 'msw/node';
import React from 'react';
import { Route } from 'react-router-dom';
import { TestProvider } from 'test/helpers/TestProvider';
@@ -21,7 +20,7 @@ import {
} from 'app/features/alerting/unified/api/ruler';
import * as useContactPoints from 'app/features/alerting/unified/components/contact-points/useContactPoints';
import * as dsByPermission from 'app/features/alerting/unified/hooks/useAlertManagerSources';
import { mockApi } from 'app/features/alerting/unified/mockApi';
import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi';
import { MockDataSourceSrv, grantUserPermissions, mockDataSource } from 'app/features/alerting/unified/mocks';
import { AlertmanagerProvider } from 'app/features/alerting/unified/state/AlertmanagerContext';
import { fetchRulerRulesIfNotFetchedYet } from 'app/features/alerting/unified/state/actions';
@@ -105,12 +104,11 @@ const mocks = {
},
};
const server = setupServer();
const server = setupMswServer();
describe('Can create a new grafana managed alert unsing simplified routing', () => {
beforeEach(() => {
mockApi(server).eval({ results: {} });
server.listen();
jest.clearAllMocks();
contextSrv.isEditor = true;
contextSrv.hasEditPermissionInFolders = true;
@@ -142,14 +140,6 @@ describe('Can create a new grafana managed alert unsing simplified routing', ()
});
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});
const dataSources = {
default: mockDataSource(
{
@@ -1,20 +1,19 @@
import 'whatwg-fetch';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { setupServer } from 'msw/node';
import React from 'react';
import { TestProvider } from 'test/helpers/TestProvider';
import { byLabelText, byRole } from 'testing-library-selector';
import { locationService, setDataSourceSrv } from '@grafana/runtime';
import { mockSearchApi } from 'app/features/alerting/unified/mockApi';
import { mockSearchApi, setupMswServer } from 'app/features/alerting/unified/mockApi';
import * as analytics from '../../Analytics';
import { MockDataSourceSrv } from '../../mocks';
import RulesFilter from './RulesFilter';
const server = setupServer();
const server = setupMswServer();
jest.spyOn(analytics, 'logInfo');
jest.mock('./MultipleDataSourcePicker', () => {
@@ -44,15 +43,6 @@ const ui = {
beforeEach(() => {
locationService.replace({ search: '' });
mockSearchApi(server).search([]);
server.listen({ onUnhandledRequest: 'error' });
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});
describe('RulesFilter', () => {
@@ -6,7 +6,11 @@ import React, { PropsWithChildren } from 'react';
import { Router } from 'react-router-dom';
import { TestProvider } from 'test/helpers/TestProvider';
import { mockFolderApi } from 'app/features/alerting/unified/mockApi';
import { mockFolderApi, setupMswServer } from 'app/features/alerting/unified/mockApi';
import {
defaultAlertmanagerChoiceResponse,
mockAlertmanagerChoiceResponse,
} from 'app/features/alerting/unified/mocks/alertmanagerApi';
import { AlertManagerDataSourceJsonData, AlertManagerImplementation } from 'app/plugins/datasource/alertmanager/types';
import { AccessControlAction } from 'app/types';
import { RulerGrafanaRuleDTO } from 'app/types/unified-alerting-dto';
@@ -141,17 +145,18 @@ describe('alertmanager abilities', () => {
});
describe('AlertRule abilities', () => {
const server = setupMswServer();
it('should report that all actions are supported for a Grafana Managed alert rule', async () => {
const rule = getGrafanaRule();
// TODO: Remove server mocking within test once server is run before all tests
const server = setupServer();
mockFolderApi(server).folder(
(rule.rulerRule as RulerGrafanaRuleDTO).grafana_alert.namespace_uid,
mockFolder({
accessControl: { [AccessControlAction.AlertingRuleUpdate]: false },
})
);
mockAlertmanagerChoiceResponse(server, defaultAlertmanagerChoiceResponse);
server.listen();
const abilities = renderHook(() => useAllAlertRuleAbilities(rule), { wrapper: TestProvider });
@@ -165,8 +170,6 @@ describe('AlertRule abilities', () => {
});
expect(abilities.result.current).toMatchSnapshot();
server.close();
});
it('should report no permissions while we are loading data for cloud rule', async () => {