diff --git a/packages/kbn-alerts-ui-shared/src/maintenance_window_callout/index.test.tsx b/packages/kbn-alerts-ui-shared/src/maintenance_window_callout/index.test.tsx index 9644958ea0245..b29f0ddc613c4 100644 --- a/packages/kbn-alerts-ui-shared/src/maintenance_window_callout/index.test.tsx +++ b/packages/kbn-alerts-ui-shared/src/maintenance_window_callout/index.test.tsx @@ -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([])), @@ -32,49 +33,6 @@ const TestProviders: React.FC<{}> = ({ children }) => { ); }; -const RUNNING_MAINTENANCE_WINDOW_1: Partial = { - 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 = { - 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> = { - 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 = { - 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 = { diff --git a/packages/kbn-alerts-ui-shared/src/maintenance_window_callout/mock.ts b/packages/kbn-alerts-ui-shared/src/maintenance_window_callout/mock.ts new file mode 100644 index 0000000000000..9e6d263a3810f --- /dev/null +++ b/packages/kbn-alerts-ui-shared/src/maintenance_window_callout/mock.ts @@ -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 = { + 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 = { + 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> = { + 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 = { + 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' }, + ], +}; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.test.tsx index 0c2c508b3f719..da7a78956348e 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.test.tsx @@ -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'; @@ -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'); @@ -104,11 +107,11 @@ jest.mock('react-router-dom', () => ({ pathname: '/triggersActions/rules/', }), })); -jest.mock('@kbn/alerts-ui-shared', () => ({ - MaintenanceWindowCallout: jest.fn(() => ( -
- )), + +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), @@ -158,6 +161,7 @@ describe('Update Api Key', () => { const addDanger = jest.fn(); beforeAll(() => { + fetchActiveMaintenanceWindowsMock.mockResolvedValue([]); loadRulesWithKueryFilter.mockResolvedValue({ page: 1, perPage: 10000, @@ -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, @@ -203,6 +214,7 @@ describe('Update Api Key', () => { describe('rules_list component empty', () => { beforeEach(() => { + fetchActiveMaintenanceWindowsMock.mockResolvedValue([]); loadRulesWithKueryFilter.mockResolvedValue({ page: 1, perPage: 10000, @@ -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; }); @@ -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(); - 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 () => {