-
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.
fix maintenance window issue, add tests
- Loading branch information
1 parent
a29fa86
commit 5c0eb06
Showing
4 changed files
with
267 additions
and
7 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
packages/kbn-alerts-ui-shared/src/alerts_search_bar/index.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,128 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; | ||
import { httpServiceMock, notificationServiceMock } from '@kbn/core/public/mocks'; | ||
import { Filter } from '@kbn/es-query'; | ||
import { useLoadRuleTypesQuery, useAlertsDataView, useRuleAADFields } from '../common/hooks'; | ||
import { AlertsSearchBar } from '.'; | ||
|
||
jest.mock('../common/hooks/use_load_rule_types_query'); | ||
jest.mock('../common/hooks/use_rule_aad_fields'); | ||
jest.mock('../common/hooks/use_alerts_data_view'); | ||
|
||
jest.mocked(useAlertsDataView).mockReturnValue({ | ||
isLoading: false, | ||
dataView: { | ||
title: '.alerts-*', | ||
fields: [ | ||
{ | ||
name: 'event.action', | ||
type: 'string', | ||
aggregatable: true, | ||
searchable: true, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
jest.mocked(useLoadRuleTypesQuery).mockReturnValue({ | ||
ruleTypesState: { | ||
isInitialLoad: false, | ||
data: new Map(), | ||
isLoading: false, | ||
error: null, | ||
}, | ||
authorizedToReadAnyRules: false, | ||
hasAnyAuthorizedRuleType: false, | ||
authorizedRuleTypes: [], | ||
authorizedToCreateAnyRules: false, | ||
isSuccess: false, | ||
}); | ||
|
||
jest.mocked(useRuleAADFields).mockReturnValue({ | ||
aadFields: [], | ||
loading: false, | ||
}); | ||
|
||
const mockDataPlugin = dataPluginMock.createStartContract(); | ||
const unifiedSearchBarMock = jest.fn().mockImplementation((props) => ( | ||
<button | ||
data-test-subj="querySubmitButton" | ||
onClick={() => props.onQuerySubmit({ dateRange: { from: 'now', to: 'now' } })} | ||
type="button" | ||
> | ||
{'Hello world'} | ||
</button> | ||
)); | ||
const httpMock = httpServiceMock.createStartContract(); | ||
const toastsMock = notificationServiceMock.createStartContract().toasts; | ||
|
||
describe('AlertsSearchBar', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders correctly', () => { | ||
render( | ||
<AlertsSearchBar | ||
rangeFrom="now/d" | ||
rangeTo="now/d" | ||
query="" | ||
onQuerySubmit={jest.fn()} | ||
initialFilters={[]} | ||
onFiltersUpdated={jest.fn()} | ||
appName={'test'} | ||
dataService={mockDataPlugin} | ||
featureIds={['observability', 'stackAlerts']} | ||
unifiedSearchBar={unifiedSearchBarMock} | ||
http={httpMock} | ||
toasts={toastsMock} | ||
/> | ||
); | ||
expect(screen.getByTestId('querySubmitButton')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders initial filters correctly', () => { | ||
const filters = [ | ||
{ | ||
meta: { | ||
negate: false, | ||
alias: null, | ||
disabled: false, | ||
type: 'custom', | ||
key: 'query', | ||
}, | ||
query: { match_phrase: { 'host.name': 'testValue' } }, | ||
$state: { store: 'appState' }, | ||
}, | ||
] as Filter[]; | ||
|
||
render( | ||
<AlertsSearchBar | ||
rangeFrom="now/d" | ||
rangeTo="now/d" | ||
query="" | ||
onQuerySubmit={jest.fn()} | ||
initialFilters={filters} | ||
onFiltersUpdated={jest.fn()} | ||
appName={'test'} | ||
dataService={mockDataPlugin} | ||
featureIds={['observability', 'stackAlerts']} | ||
unifiedSearchBar={unifiedSearchBarMock} | ||
http={httpMock} | ||
toasts={toastsMock} | ||
/> | ||
); | ||
|
||
expect(mockDataPlugin.query.filterManager.addFilters).toHaveBeenCalledWith(filters); | ||
}); | ||
}); |
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
136 changes: 136 additions & 0 deletions
136
...ggers_actions_ui/public/application/sections/alerts_search_bar/alerts_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,136 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { useAlertsDataView } from '@kbn/alerts-ui-shared/src/common/hooks/use_alerts_data_view'; | ||
import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; | ||
import { Filter } from '@kbn/es-query'; | ||
import { NotificationsStart } from '@kbn/core-notifications-browser'; | ||
import { useLoadRuleTypesQuery } from '../../hooks/use_load_rule_types_query'; | ||
import { useRuleAADFields } from '../../hooks/use_rule_aad_fields'; | ||
import { AlertsSearchBar } from './alerts_search_bar'; | ||
|
||
const mockDataPlugin = dataPluginMock.createStartContract(); | ||
jest.mock('../../hooks/use_load_rule_types_query'); | ||
jest.mock('../../hooks/use_rule_aad_fields'); | ||
jest.mock('@kbn/alerts-ui-shared/src/common/hooks/use_alerts_data_view'); | ||
jest.mock('@kbn/kibana-react-plugin/public', () => { | ||
const original = jest.requireActual('@kbn/kibana-react-plugin/public'); | ||
return { | ||
useKibana: () => ({ | ||
services: { | ||
...original.useKibana().services, | ||
data: mockDataPlugin, | ||
unifiedSearch: { | ||
ui: { | ||
SearchBar: jest.fn().mockImplementation((props) => ( | ||
<button | ||
data-test-subj="querySubmitButton" | ||
onClick={() => props.onQuerySubmit({ dateRange: { from: 'now', to: 'now' } })} | ||
type="button" | ||
> | ||
{'Hello world'} | ||
</button> | ||
)), | ||
}, | ||
}, | ||
notifications: { toasts: { addWarning: jest.fn() } } as unknown as NotificationsStart, | ||
}, | ||
}), | ||
}; | ||
}); | ||
jest.mocked(useAlertsDataView).mockReturnValue({ | ||
isLoading: false, | ||
dataView: { | ||
title: '.alerts-*', | ||
fields: [ | ||
{ | ||
name: 'event.action', | ||
type: 'string', | ||
aggregatable: true, | ||
searchable: true, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
jest.mocked(useLoadRuleTypesQuery).mockReturnValue({ | ||
ruleTypesState: { | ||
initialLoad: false, | ||
data: new Map(), | ||
isLoading: false, | ||
error: undefined, | ||
}, | ||
authorizedToReadAnyRules: false, | ||
hasAnyAuthorizedRuleType: false, | ||
authorizedRuleTypes: [], | ||
authorizedToCreateAnyRules: false, | ||
isSuccess: false, | ||
}); | ||
|
||
jest.mocked(useRuleAADFields).mockReturnValue({ | ||
aadFields: [], | ||
loading: false, | ||
}); | ||
|
||
describe('AlertsSearchBar', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders correctly', () => { | ||
render( | ||
<AlertsSearchBar | ||
rangeFrom="now/d" | ||
rangeTo="now/d" | ||
query="" | ||
onQuerySubmit={jest.fn()} | ||
initialFilters={[]} | ||
onFiltersUpdated={jest.fn()} | ||
onSavedQueryUpdated={jest.fn()} | ||
onClearSavedQuery={jest.fn()} | ||
appName={'test'} | ||
featureIds={['observability', 'stackAlerts']} | ||
/> | ||
); | ||
expect(screen.getByTestId('querySubmitButton')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders initial filters correctly', () => { | ||
const filters = [ | ||
{ | ||
meta: { | ||
negate: false, | ||
alias: null, | ||
disabled: false, | ||
type: 'custom', | ||
key: 'query', | ||
}, | ||
query: { match_phrase: { 'host.name': 'testValue' } }, | ||
$state: { store: 'appState' }, | ||
}, | ||
] as Filter[]; | ||
|
||
render( | ||
<AlertsSearchBar | ||
rangeFrom="now/d" | ||
rangeTo="now/d" | ||
query="" | ||
onQuerySubmit={jest.fn()} | ||
initialFilters={filters} | ||
onFiltersUpdated={jest.fn()} | ||
onSavedQueryUpdated={jest.fn()} | ||
onClearSavedQuery={jest.fn()} | ||
appName={'test'} | ||
featureIds={['observability', 'stackAlerts']} | ||
/> | ||
); | ||
|
||
expect(mockDataPlugin.query.filterManager.addFilters).toHaveBeenCalledWith(filters); | ||
}); | ||
}); |
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