Skip to content

Commit

Permalink
add specific jest test to make sure the window maintenance is showing up
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM committed Aug 11, 2023
1 parent 9c8b4e4 commit 7a89261
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import React from 'react';
import { I18nProvider } from '@kbn/i18n-react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, waitFor, cleanup } from '@testing-library/react';
import {
MaintenanceWindowStatus,
MAINTENANCE_WINDOW_FEATURE_ID,
} from '@kbn/alerting-plugin/common';
import type { MaintenanceWindow } from '@kbn/alerting-plugin/common';
import type { AsApiContract } from '@kbn/alerting-plugin/server/routes/lib';
import { MAINTENANCE_WINDOW_FEATURE_ID } from '@kbn/alerting-plugin/common';
import { MaintenanceWindowCallout } from '.';
import { fetchActiveMaintenanceWindows } from './api';
import {
RECURRING_RUNNING_MAINTENANCE_WINDOW,
RUNNING_MAINTENANCE_WINDOW_1,
RUNNING_MAINTENANCE_WINDOW_2,
UPCOMING_MAINTENANCE_WINDOW,
} from './mock';

jest.mock('./api', () => ({
fetchActiveMaintenanceWindows: jest.fn(() => Promise.resolve([])),
Expand All @@ -32,49 +33,6 @@ const TestProviders: React.FC<{}> = ({ children }) => {
);
};

const RUNNING_MAINTENANCE_WINDOW_1: Partial<MaintenanceWindow> = {
title: 'Running maintenance window 1',
id: '63057284-ac31-42ba-fe22-adfe9732e5ae',
status: MaintenanceWindowStatus.Running,
events: [{ gte: '2023-04-20T16:27:30.753Z', lte: '2023-04-20T16:57:30.753Z' }],
};

const RUNNING_MAINTENANCE_WINDOW_2: Partial<MaintenanceWindow> = {
title: 'Running maintenance window 2',
id: '45894340-df98-11ed-ac81-bfcb4982b4fd',
status: MaintenanceWindowStatus.Running,
events: [{ gte: '2023-04-20T16:47:42.871Z', lte: '2023-04-20T17:11:32.192Z' }],
};

const RECURRING_RUNNING_MAINTENANCE_WINDOW: Partial<AsApiContract<MaintenanceWindow>> = {
title: 'Recurring running maintenance window',
id: 'e2228300-e9ad-11ed-ba37-db17c6e6182b',
status: MaintenanceWindowStatus.Running,
events: [
{ gte: '2023-05-03T12:27:18.569Z', lte: '2023-05-03T12:57:18.569Z' },
{ gte: '2023-05-10T12:27:18.569Z', lte: '2023-05-10T12:57:18.569Z' },
],
expiration_date: '2024-05-03T12:27:35.088Z',
r_rule: {
dtstart: '2023-05-03T12:27:18.569Z',
tzid: 'Europe/Amsterdam',
freq: 3,
interval: 1,
count: 2,
byweekday: ['WE'],
},
};

const UPCOMING_MAINTENANCE_WINDOW: Partial<MaintenanceWindow> = {
title: 'Upcoming maintenance window',
id: '5eafe070-e030-11ed-ac81-bfcb4982b4fd',
status: MaintenanceWindowStatus.Upcoming,
events: [
{ gte: '2023-04-21T10:36:14.028Z', lte: '2023-04-21T10:37:00.000Z' },
{ gte: '2023-04-28T10:36:14.028Z', lte: '2023-04-28T10:37:00.000Z' },
],
};

const fetchActiveMaintenanceWindowsMock = fetchActiveMaintenanceWindows as jest.Mock;

const kibanaServicesMock = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { AsApiContract } from '@kbn/alerting-plugin/server/routes/lib';
import { MaintenanceWindow, MaintenanceWindowStatus } from './types';

export const RUNNING_MAINTENANCE_WINDOW_1: Partial<MaintenanceWindow> = {
title: 'Running maintenance window 1',
id: '63057284-ac31-42ba-fe22-adfe9732e5ae',
status: MaintenanceWindowStatus.Running,
events: [{ gte: '2023-04-20T16:27:30.753Z', lte: '2023-04-20T16:57:30.753Z' }],
};

export const RUNNING_MAINTENANCE_WINDOW_2: Partial<MaintenanceWindow> = {
title: 'Running maintenance window 2',
id: '45894340-df98-11ed-ac81-bfcb4982b4fd',
status: MaintenanceWindowStatus.Running,
events: [{ gte: '2023-04-20T16:47:42.871Z', lte: '2023-04-20T17:11:32.192Z' }],
};

export const RECURRING_RUNNING_MAINTENANCE_WINDOW: Partial<AsApiContract<MaintenanceWindow>> = {
title: 'Recurring running maintenance window',
id: 'e2228300-e9ad-11ed-ba37-db17c6e6182b',
status: MaintenanceWindowStatus.Running,
events: [
{ gte: '2023-05-03T12:27:18.569Z', lte: '2023-05-03T12:57:18.569Z' },
{ gte: '2023-05-10T12:27:18.569Z', lte: '2023-05-10T12:57:18.569Z' },
],
expiration_date: '2024-05-03T12:27:35.088Z',
r_rule: {
dtstart: '2023-05-03T12:27:18.569Z',
tzid: 'Europe/Amsterdam',
freq: 3,
interval: 1,
count: 2,
byweekday: ['WE'],
},
};

export const UPCOMING_MAINTENANCE_WINDOW: Partial<MaintenanceWindow> = {
title: 'Upcoming maintenance window',
id: '5eafe070-e030-11ed-ac81-bfcb4982b4fd',
status: MaintenanceWindowStatus.Upcoming,
events: [
{ gte: '2023-04-21T10:36:14.028Z', lte: '2023-04-21T10:37:00.000Z' },
{ gte: '2023-04-28T10:36:14.028Z', lte: '2023-04-28T10:37:00.000Z' },
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
waitForElementToBeRemoved,
cleanup,
} from '@testing-library/react';

import { fetchActiveMaintenanceWindows } from '@kbn/alerts-ui-shared/src/maintenance_window_callout/api';
import { RUNNING_MAINTENANCE_WINDOW_1 } from '@kbn/alerts-ui-shared/src/maintenance_window_callout/mock';
import { actionTypeRegistryMock } from '../../../action_type_registry.mock';
import { ruleTypeRegistryMock } from '../../../rule_type_registry.mock';
import { percentileFields, RulesList } from './rules_list';
Expand All @@ -38,7 +41,7 @@ import {
getDisabledByLicenseRuleTypeFromApi,
} from './test_helpers';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { parseDuration } from '@kbn/alerting-plugin/common';
import { MAINTENANCE_WINDOW_FEATURE_ID, parseDuration } from '@kbn/alerting-plugin/common';
import { getFormattedDuration } from '../../../lib/monitoring_utils';

jest.mock('../../../../common/lib/kibana');
Expand Down Expand Up @@ -104,11 +107,11 @@ jest.mock('react-router-dom', () => ({
pathname: '/triggersActions/rules/',
}),
}));
jest.mock('@kbn/alerts-ui-shared', () => ({
MaintenanceWindowCallout: jest.fn(() => (
<div data-test-subj="mocked-MaintenanceWindowCallout-component" />
)),

jest.mock('@kbn/alerts-ui-shared/src/maintenance_window_callout/api', () => ({
fetchActiveMaintenanceWindows: jest.fn(() => Promise.resolve([])),
}));
const fetchActiveMaintenanceWindowsMock = fetchActiveMaintenanceWindows as jest.Mock;

jest.mock('../../../lib/capabilities', () => ({
hasAllPrivilege: jest.fn(() => true),
Expand Down Expand Up @@ -158,6 +161,7 @@ describe('Update Api Key', () => {
const addDanger = jest.fn();

beforeAll(() => {
fetchActiveMaintenanceWindowsMock.mockResolvedValue([]);
loadRulesWithKueryFilter.mockResolvedValue({
page: 1,
perPage: 10000,
Expand All @@ -167,6 +171,13 @@ describe('Update Api Key', () => {
loadActionTypes.mockResolvedValue([]);
loadRuleTypes.mockResolvedValue([ruleTypeFromApi]);
loadAllActions.mockResolvedValue([]);
useKibanaMock().services.application.capabilities = {
...useKibanaMock().services.application.capabilities,
[MAINTENANCE_WINDOW_FEATURE_ID]: {
save: true,
show: true,
},
};
useKibanaMock().services.notifications.toasts = {
addSuccess,
addError,
Expand Down Expand Up @@ -203,6 +214,7 @@ describe('Update Api Key', () => {

describe('rules_list component empty', () => {
beforeEach(() => {
fetchActiveMaintenanceWindowsMock.mockResolvedValue([]);
loadRulesWithKueryFilter.mockResolvedValue({
page: 1,
perPage: 10000,
Expand Down Expand Up @@ -234,7 +246,13 @@ describe('rules_list component empty', () => {

ruleTypeRegistry.list.mockReturnValue([ruleType]);
actionTypeRegistry.list.mockReturnValue([]);

useKibanaMock().services.application.capabilities = {
...useKibanaMock().services.application.capabilities,
[MAINTENANCE_WINDOW_FEATURE_ID]: {
save: true,
show: true,
},
};
useKibanaMock().services.ruleTypeRegistry = ruleTypeRegistry;
useKibanaMock().services.actionTypeRegistry = actionTypeRegistry;
});
Expand All @@ -250,11 +268,11 @@ describe('rules_list component empty', () => {
expect(await screen.findByTestId('createFirstRuleEmptyPrompt')).toBeInTheDocument();
});

it('renders MaintenanceWindowCallout mocked component', async () => {
it('renders MaintenanceWindowCallout if one exists', async () => {
fetchActiveMaintenanceWindowsMock.mockResolvedValue([RUNNING_MAINTENANCE_WINDOW_1]);
renderWithProviders(<RulesList />);
expect(
await screen.findByTestId('mocked-MaintenanceWindowCallout-component')
).toBeInTheDocument();
expect(await screen.findByText('Maintenance window is running')).toBeInTheDocument();
expect(fetchActiveMaintenanceWindowsMock).toHaveBeenCalledTimes(1);
});

it('renders Create rule button', async () => {
Expand Down

0 comments on commit 7a89261

Please sign in to comment.