|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +import * as React from 'react'; |
| 7 | +import uuid from 'uuid'; |
| 8 | +import { shallow } from 'enzyme'; |
| 9 | +import { ToastsApi } from 'kibana/public'; |
| 10 | +import { AlertInstancesRoute, getAlertState } from './alert_instances_route'; |
| 11 | +import { Alert } from '../../../../types'; |
| 12 | +import { EuiLoadingSpinner } from '@elastic/eui'; |
| 13 | + |
| 14 | +jest.mock('../../../app_context', () => { |
| 15 | + const toastNotifications = jest.fn(); |
| 16 | + return { |
| 17 | + useAppDependencies: jest.fn(() => ({ toastNotifications })), |
| 18 | + }; |
| 19 | +}); |
| 20 | +describe('alert_state_route', () => { |
| 21 | + it('render a loader while fetching data', () => { |
| 22 | + const alert = mockAlert(); |
| 23 | + |
| 24 | + expect( |
| 25 | + shallow(<AlertInstancesRoute alert={alert} {...mockApis()} />).containsMatchingElement( |
| 26 | + <EuiLoadingSpinner size="l" /> |
| 27 | + ) |
| 28 | + ).toBeTruthy(); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('getAlertState useEffect handler', () => { |
| 33 | + beforeEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('fetches alert state', async () => { |
| 38 | + const alert = mockAlert(); |
| 39 | + const alertState = mockAlertState(); |
| 40 | + const { loadAlertState } = mockApis(); |
| 41 | + const { setAlertState } = mockStateSetter(); |
| 42 | + |
| 43 | + loadAlertState.mockImplementationOnce(async () => alertState); |
| 44 | + |
| 45 | + const toastNotifications = ({ |
| 46 | + addDanger: jest.fn(), |
| 47 | + } as unknown) as ToastsApi; |
| 48 | + |
| 49 | + await getAlertState(alert.id, loadAlertState, setAlertState, toastNotifications); |
| 50 | + |
| 51 | + expect(loadAlertState).toHaveBeenCalledWith(alert.id); |
| 52 | + expect(setAlertState).toHaveBeenCalledWith(alertState); |
| 53 | + }); |
| 54 | + |
| 55 | + it('displays an error if the alert state isnt found', async () => { |
| 56 | + const actionType = { |
| 57 | + id: '.server-log', |
| 58 | + name: 'Server log', |
| 59 | + enabled: true, |
| 60 | + }; |
| 61 | + const alert = mockAlert({ |
| 62 | + actions: [ |
| 63 | + { |
| 64 | + group: '', |
| 65 | + id: uuid.v4(), |
| 66 | + actionTypeId: actionType.id, |
| 67 | + params: {}, |
| 68 | + }, |
| 69 | + ], |
| 70 | + }); |
| 71 | + |
| 72 | + const { loadAlertState } = mockApis(); |
| 73 | + const { setAlertState } = mockStateSetter(); |
| 74 | + |
| 75 | + loadAlertState.mockImplementation(async () => { |
| 76 | + throw new Error('OMG'); |
| 77 | + }); |
| 78 | + |
| 79 | + const toastNotifications = ({ |
| 80 | + addDanger: jest.fn(), |
| 81 | + } as unknown) as ToastsApi; |
| 82 | + await getAlertState(alert.id, loadAlertState, setAlertState, toastNotifications); |
| 83 | + expect(toastNotifications.addDanger).toHaveBeenCalledTimes(1); |
| 84 | + expect(toastNotifications.addDanger).toHaveBeenCalledWith({ |
| 85 | + title: 'Unable to load alert state: OMG', |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +function mockApis() { |
| 91 | + return { |
| 92 | + loadAlertState: jest.fn(), |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +function mockStateSetter() { |
| 97 | + return { |
| 98 | + setAlertState: jest.fn(), |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +function mockAlert(overloads: Partial<Alert> = {}): Alert { |
| 103 | + return { |
| 104 | + id: uuid.v4(), |
| 105 | + enabled: true, |
| 106 | + name: `alert-${uuid.v4()}`, |
| 107 | + tags: [], |
| 108 | + alertTypeId: '.noop', |
| 109 | + consumer: 'consumer', |
| 110 | + schedule: { interval: '1m' }, |
| 111 | + actions: [], |
| 112 | + params: {}, |
| 113 | + createdBy: null, |
| 114 | + updatedBy: null, |
| 115 | + createdAt: new Date(), |
| 116 | + updatedAt: new Date(), |
| 117 | + apiKeyOwner: null, |
| 118 | + throttle: null, |
| 119 | + muteAll: false, |
| 120 | + mutedInstanceIds: [], |
| 121 | + ...overloads, |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +function mockAlertState(overloads: Partial<any> = {}): any { |
| 126 | + return { |
| 127 | + alertTypeState: { |
| 128 | + some: 'value', |
| 129 | + }, |
| 130 | + alertInstances: { |
| 131 | + first_instance: { |
| 132 | + state: {}, |
| 133 | + meta: { |
| 134 | + lastScheduledActions: { |
| 135 | + group: 'first_group', |
| 136 | + date: new Date(), |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + second_instance: {}, |
| 141 | + }, |
| 142 | + }; |
| 143 | +} |
0 commit comments