-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Actionable observability] Add tests for alert search bar (#145259)
Closes #143641 ## Summary As a follow-up to this PR #145067, in this PR I've added tests to make sure the same issue will not happen in the future. ### Checklist - [X] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
1 parent
768d5b8
commit 1566897
Showing
5 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...plugins/observability/public/components/shared/alert_search_bar/alert_search_bar.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { triggersActionsUiMock } from '@kbn/triggers-actions-ui-plugin/public/mocks'; | ||
import React from 'react'; | ||
import { act, waitFor } from '@testing-library/react'; | ||
import { AlertSearchBarProps } from './types'; | ||
import { ObservabilityAlertSearchBar } from './alert_search_bar'; | ||
import { observabilityAlertFeatureIds } from '../../../config'; | ||
import { useKibana } from '../../../utils/kibana_react'; | ||
import { kibanaStartMock } from '../../../utils/kibana_react.mock'; | ||
import { render } from '../../../utils/test_helper'; | ||
|
||
const useKibanaMock = useKibana as jest.Mock; | ||
const getAlertsSearchBarMock = jest.fn(); | ||
const ALERT_SEARCH_BAR_DATA_TEST_SUBJ = 'alerts-search-bar'; | ||
const ACTIVE_BUTTON_DATA_TEST_SUBJ = 'alert-status-filter-active-button'; | ||
|
||
jest.mock('../../../utils/kibana_react'); | ||
|
||
const mockKibana = () => { | ||
useKibanaMock.mockReturnValue({ | ||
services: { | ||
...kibanaStartMock.startContract().services, | ||
triggersActionsUi: { | ||
...triggersActionsUiMock.createStart(), | ||
getAlertsSearchBar: getAlertsSearchBarMock.mockReturnValue( | ||
<div data-test-subj={ALERT_SEARCH_BAR_DATA_TEST_SUBJ} /> | ||
), | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
describe('ObservabilityAlertSearchBar', () => { | ||
const renderComponent = (props: Partial<AlertSearchBarProps> = {}) => { | ||
const alertSearchBarProps: AlertSearchBarProps = { | ||
appName: 'testAppName', | ||
rangeFrom: 'now-15m', | ||
setRangeFrom: jest.fn(), | ||
rangeTo: 'now', | ||
setRangeTo: jest.fn(), | ||
kuery: '', | ||
setKuery: jest.fn(), | ||
status: 'active', | ||
setStatus: jest.fn(), | ||
setEsQuery: jest.fn(), | ||
...props, | ||
}; | ||
return render(<ObservabilityAlertSearchBar {...alertSearchBarProps} />); | ||
}; | ||
|
||
beforeAll(() => { | ||
mockKibana(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render alert search bar', async () => { | ||
const observabilitySearchBar = renderComponent(); | ||
|
||
await waitFor(() => | ||
expect(observabilitySearchBar.queryByTestId(ALERT_SEARCH_BAR_DATA_TEST_SUBJ)).toBeTruthy() | ||
); | ||
}); | ||
|
||
it('should call alert search bar with correct props', () => { | ||
act(() => { | ||
renderComponent(); | ||
}); | ||
|
||
expect(getAlertsSearchBarMock).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
appName: 'testAppName', | ||
featureIds: observabilityAlertFeatureIds, | ||
rangeFrom: 'now-15m', | ||
rangeTo: 'now', | ||
query: '', | ||
}), | ||
{} | ||
); | ||
}); | ||
|
||
it('should filter active alerts', async () => { | ||
const mockedSetEsQuery = jest.fn(); | ||
const mockedFrom = '2022-11-15T09:38:13.604Z'; | ||
const mockedTo = '2022-11-15T09:53:13.604Z'; | ||
const { getByTestId } = renderComponent({ | ||
setEsQuery: mockedSetEsQuery, | ||
rangeFrom: mockedFrom, | ||
rangeTo: mockedTo, | ||
}); | ||
|
||
await act(async () => { | ||
const activeButton = getByTestId(ACTIVE_BUTTON_DATA_TEST_SUBJ); | ||
activeButton.click(); | ||
}); | ||
|
||
expect(mockedSetEsQuery).toHaveBeenCalledWith({ | ||
bool: { | ||
filter: [ | ||
{ | ||
bool: { | ||
minimum_should_match: 1, | ||
should: [{ match_phrase: { 'kibana.alert.status': 'active' } }], | ||
}, | ||
}, | ||
{ | ||
range: { | ||
'@timestamp': expect.objectContaining({ | ||
format: 'strict_date_optional_time', | ||
gte: mockedFrom, | ||
lte: mockedTo, | ||
}), | ||
}, | ||
}, | ||
], | ||
must: [], | ||
must_not: [], | ||
should: [], | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters