77 * License v3.0 only", or the "Server Side Public License, v 1".
88 */
99
10+ import { render , screen , act as rtlAct } from '@testing-library/react' ;
11+ import userEvent from '@testing-library/user-event' ;
12+ import { __IntlProvider as IntlProvider } from '@kbn/i18n-react' ;
1013import { BehaviorSubject } from 'rxjs' ;
1114import type { ReactWrapper } from 'enzyme' ;
1215import { findTestSubject } from '@elastic/eui/lib/test' ;
@@ -34,6 +37,9 @@ import type { DiscoverCustomizationId } from '../../../../customizations/customi
3437import type { FieldListCustomization , SearchBarCustomization } from '../../../../customizations' ;
3538import { RuntimeStateProvider } from '../../state_management/redux' ;
3639import { DiscoverMainProvider } from '../../state_management/discover_state_provider' ;
40+ import type { DataView } from '@kbn/data-views-plugin/common' ;
41+
42+ type TestWrapperProps = DiscoverSidebarResponsiveProps & { selectedDataView : DataView } ;
3743
3844const mockSearchBarCustomization : SearchBarCustomization = {
3945 id : 'search_bar' ,
@@ -143,7 +149,7 @@ jest.mock('@kbn/discover-utils/src/utils/calc_field_counts', () => ({
143149
144150jest . spyOn ( ExistingFieldsServiceApi , 'loadFieldExisting' ) ;
145151
146- function getCompProps ( options ?: { hits ?: DataTableRecord [ ] } ) : DiscoverSidebarResponsiveProps {
152+ function getCompProps ( options ?: { hits ?: DataTableRecord [ ] } ) : TestWrapperProps {
147153 const dataView = stubLogstashDataView ;
148154 dataView . toSpec = jest . fn ( ( ) => ( { } ) ) ;
149155
@@ -186,12 +192,16 @@ function getStateContainer({ query }: { query?: Query | AggregateQuery }) {
186192 return stateContainer ;
187193}
188194
189- async function mountComponent (
190- props : DiscoverSidebarResponsiveProps ,
195+ type EnzymeReturnType = ReactWrapper < TestWrapperProps > ;
196+ type MountReturn < WithRTL extends boolean > = WithRTL extends true ? undefined : EnzymeReturnType ;
197+
198+ async function mountComponent < WithReactTestingLibrary extends boolean = false > (
199+ props : TestWrapperProps ,
191200 appStateParams : { query ?: Query | AggregateQuery } = { } ,
192- services ?: DiscoverServices
193- ) : Promise < ReactWrapper < DiscoverSidebarResponsiveProps > > {
194- let comp : ReactWrapper < DiscoverSidebarResponsiveProps > ;
201+ services ?: DiscoverServices ,
202+ withReactTestingLibrary ?: WithReactTestingLibrary
203+ ) : Promise < MountReturn < WithReactTestingLibrary > > {
204+ let comp : ReactWrapper < TestWrapperProps > ;
195205 const stateContainer = getStateContainer ( appStateParams ) ;
196206 const mockedServices = services ?? createMockServices ( ) ;
197207 mockedServices . data . dataViews . getIdsWithTitle = jest . fn ( async ( ) =>
@@ -206,30 +216,37 @@ async function mountComponent(
206216 . fn ( )
207217 . mockImplementation ( ( ) => stateContainer . appState . getState ( ) ) ;
208218
219+ const component = (
220+ < EuiProvider >
221+ < KibanaContextProvider services = { mockedServices } >
222+ < DiscoverMainProvider value = { stateContainer } >
223+ < RuntimeStateProvider currentDataView = { props . selectedDataView } adHocDataViews = { [ ] } >
224+ < DiscoverSidebarResponsive { ...props } />
225+ </ RuntimeStateProvider >
226+ </ DiscoverMainProvider >
227+ </ KibanaContextProvider >
228+ </ EuiProvider >
229+ ) ;
230+
231+ if ( withReactTestingLibrary ) {
232+ await rtlAct ( ( ) => render ( < IntlProvider locale = "en" > { component } </ IntlProvider > ) ) ;
233+ return undefined as MountReturn < WithReactTestingLibrary > ;
234+ }
235+
209236 await act ( async ( ) => {
210- comp = mountWithIntl (
211- < EuiProvider >
212- < KibanaContextProvider services = { mockedServices } >
213- < DiscoverMainProvider value = { stateContainer } >
214- < RuntimeStateProvider currentDataView = { props . selectedDataView ! } adHocDataViews = { [ ] } >
215- < DiscoverSidebarResponsive { ...props } /> { ' ' }
216- </ RuntimeStateProvider >
217- </ DiscoverMainProvider >
218- </ KibanaContextProvider >
219- </ EuiProvider >
220- ) ;
237+ comp = mountWithIntl ( component ) ;
221238 // wait for lazy modules
222239 await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
223240 comp . update ( ) ;
224241 } ) ;
225242
226243 comp ! . update ( ) ;
227244
228- return comp ! ;
245+ return comp ! as unknown as MountReturn < WithReactTestingLibrary > ;
229246}
230247
231248describe ( 'discover responsive sidebar' , function ( ) {
232- let props : DiscoverSidebarResponsiveProps ;
249+ let props : TestWrapperProps ;
233250
234251 beforeEach ( async ( ) => {
235252 ( ExistingFieldsServiceApi . loadFieldExisting as jest . Mock ) . mockImplementation ( async ( ) => ( {
@@ -328,17 +345,42 @@ describe('discover responsive sidebar', function () {
328345 expect ( ExistingFieldsServiceApi . loadFieldExisting ) . toHaveBeenCalledTimes ( 1 ) ;
329346 } ) ;
330347
331- it ( 'should set a11y attributes for the search input in the field list' , async function ( ) {
332- const comp = await mountComponent ( props ) ;
348+ describe ( 'when the input is not focused' , ( ) => {
349+ it ( 'should set a11y attributes for the search input in the field list' , async function ( ) {
350+ // When
351+ await mountComponent ( props , undefined , undefined , true ) ;
333352
334- const a11yDescription = findTestSubject ( comp , 'fieldListGrouped__ariaDescription' ) ;
335- expect ( a11yDescription . prop ( 'aria-live' ) ) . toBe ( 'polite' ) ;
336- expect ( a11yDescription . text ( ) ) . toBe (
337- '1 selected field. 4 popular fields. 3 available fields. 20 empty fields. 2 meta fields.'
338- ) ;
353+ // Then
354+ const a11yDescription = screen . getByTestId ( 'fieldListGrouped__ariaDescription' ) ;
355+ expect ( a11yDescription ) . toHaveAttribute ( 'aria-live' , 'off' ) ;
356+ expect ( a11yDescription ) . toHaveTextContent (
357+ '1 selected field. 4 popular fields. 3 available fields. 20 empty fields. 2 meta fields.'
358+ ) ;
359+
360+ const searchInput = screen . getByTestId ( 'fieldListFiltersFieldSearch' ) ;
361+ expect ( searchInput ) . toHaveAttribute ( 'aria-describedby' , a11yDescription . id ) ;
362+ } ) ;
363+ } ) ;
339364
340- const searchInput = findTestSubject ( comp , 'fieldListFiltersFieldSearch' ) ;
341- expect ( searchInput . first ( ) . prop ( 'aria-describedby' ) ) . toBe ( a11yDescription . prop ( 'id' ) ) ;
365+ describe ( 'when the input is focused' , ( ) => {
366+ it ( 'should set a11y attributes for the search input in the field list' , async function ( ) {
367+ // Given
368+ const user = userEvent . setup ( ) ;
369+
370+ // When
371+ await mountComponent ( props , undefined , undefined , true ) ;
372+
373+ // Then
374+ const searchInput = screen . getByTestId ( 'fieldListFiltersFieldSearch' ) ;
375+ const a11yDescription = screen . getByTestId ( 'fieldListGrouped__ariaDescription' ) ;
376+ await user . click ( searchInput ) ;
377+ expect ( searchInput ) . toHaveAttribute ( 'aria-describedby' , a11yDescription . id ) ;
378+
379+ expect ( a11yDescription ) . toHaveAttribute ( 'aria-live' , 'polite' ) ;
380+ expect ( a11yDescription ) . toHaveTextContent (
381+ '1 selected field. 4 popular fields. 3 available fields. 20 empty fields. 2 meta fields.'
382+ ) ;
383+ } ) ;
342384 } ) ;
343385
344386 it ( 'should not have selected fields if no columns selected' , async function ( ) {
@@ -385,7 +427,7 @@ describe('discover responsive sidebar', function () {
385427 } ) ;
386428
387429 it ( 'should not calculate counts if documents are not fetched yet' , async function ( ) {
388- const propsWithoutDocuments : DiscoverSidebarResponsiveProps = {
430+ const propsWithoutDocuments : TestWrapperProps = {
389431 ...props ,
390432 documents$ : new BehaviorSubject ( {
391433 fetchStatus : FetchStatus . UNINITIALIZED ,
@@ -662,7 +704,7 @@ describe('discover responsive sidebar', function () {
662704
663705 it ( 'should render buttons in data view picker correctly' , async ( ) => {
664706 const services = createMockServices ( ) ;
665- const propsWithPicker : DiscoverSidebarResponsiveProps = {
707+ const propsWithPicker : TestWrapperProps = {
666708 ...props ,
667709 fieldListVariant : 'button-and-flyout-always' ,
668710 } ;
@@ -694,7 +736,7 @@ describe('discover responsive sidebar', function () {
694736 const services = createMockServices ( ) ;
695737 services . dataViewEditor . userPermissions . editDataView = jest . fn ( ( ) => false ) ;
696738 services . dataViewFieldEditor . userPermissions . editIndexPattern = jest . fn ( ( ) => false ) ;
697- const propsWithPicker : DiscoverSidebarResponsiveProps = {
739+ const propsWithPicker : TestWrapperProps = {
698740 ...props ,
699741 fieldListVariant : 'button-and-flyout-always' ,
700742 } ;
0 commit comments