import { act } from 'react-dom/test-utils' ; import { API_BASE_PATH } from '../../../common/constants' ; import type { MlAction } from '../../../common/types' ; import { setupEnvironment } from '../helpers' ; import { ElasticsearchTestBed , setupElasticsearchPage } from './es_deprecations.helpers' ; import { esDeprecationsMockResponse , MOCK_SNAPSHOT_ID , MOCK_JOB_ID , createEsDeprecationsMockResponse , } from './mocked_responses' ; describe ( 'ES deprecations table' , ( ) => { let testBed ; const { server , httpRequestsMockHelpers } = setupEnvironment ( ) ; afterAll ( ( ) => { server . restore ( ) ; } ) ; beforeEach ( async ( ) => { httpRequestsMockHelpers . setLoadEsDeprecationsResponse ( esDeprecationsMockResponse ) ; httpRequestsMockHelpers . setUpgradeMlSnapshotStatusResponse ( { nodeId : 'my_node' , snapshotId : MOCK_SNAPSHOT_ID , jobId : MOCK_JOB_ID , status : 'idle' , } ) ; httpRequestsMockHelpers . setReindexStatusResponse ( { reindexOp : null , warnings : [ ] , hasRequiredPrivileges : true , meta : { indexName : 'foo' , reindexName : 'reindexed-foo' , aliases : [ ] , } , } ) ; await act ( async ( ) => { testBed = await setupElasticsearchPage ( { isReadOnlyMode : false } ) ; } ) ; testBed . component . update ( ) ; } ) ; it ( 'renders deprecations' , ( ) => { const { exists , find } = testBed ; expect ( exists ( 'esDeprecationsContent' ) ) . toBe ( true ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( esDeprecationsMockResponse . deprecations . length ) ; } ) ; it ( 'refreshes deprecation data' , async ( ) => { const { actions } = testBed ; const totalRequests = server . requests . length ; await actions . table . clickRefreshButton ( ) ; const mlDeprecation = esDeprecationsMockResponse . deprecations [ 0 ] ; const reindexDeprecation = esDeprecationsMockResponse . deprecations [ 3 ] ; expect ( server . requests . length ) . toBe ( totalRequests + 4 ) ; expect ( server . requests [ server . requests . length - 4 ] . url ) . toBe ( `${ API_BASE_PATH }/es_deprecations` ) ; expect ( server . requests [ server . requests . length - 3 ] . url ) . toBe ( `${ API_BASE_PATH }/ml_snapshots/${ ( mlDeprecation . correctiveAction as MlAction ) . jobId }/${ ( mlDeprecation . correctiveAction as MlAction ) . snapshotId }` ) ; expect ( server . requests [ server . requests . length - 2 ] . url ) . toBe ( `${ API_BASE_PATH }/reindex/${ reindexDeprecation . index }` ) ; expect ( server . requests [ server . requests . length - 1 ] . url ) . toBe ( `${ API_BASE_PATH }/ml_upgrade_mode` ) ; } ) ; it ( 'shows critical and warning deprecations count' , ( ) => { const { find } = testBed ; const criticalDeprecations = esDeprecationsMockResponse . deprecations . filter ( ( deprecation ) => deprecation . isCritical ) ; const warningDeprecations = esDeprecationsMockResponse . deprecations . filter ( ( deprecation ) => deprecation . isCritical === false ) ; expect ( find ( 'criticalDeprecationsCount' ) . text ( ) ) . toContain ( criticalDeprecations . length ) ; expect ( find ( 'warningDeprecationsCount' ) . text ( ) ) . toContain ( warningDeprecations . length ) ; } ) ; describe ( 'search bar' , ( ) => { it ( 'filters results by "critical" status' , async ( ) => { const { find , actions } = testBed ; await actions . searchBar . clickCriticalFilterButton ( ) ; const criticalDeprecations = esDeprecationsMockResponse . deprecations . filter ( ( deprecation ) => deprecation . isCritical ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( criticalDeprecations . length ) ; await actions . searchBar . clickCriticalFilterButton ( ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( esDeprecationsMockResponse . deprecations . length ) ; } ) ; it ( 'filters results by type' , async ( ) => { const { component , find , actions } = testBed ; await actions . searchBar . clickTypeFilterDropdownAt ( 0 ) ; const clusterTypeFilterButton = document . body . querySelector ( '.euiFilterSelect__items .euiFilterSelectItem' ) ; expect ( clusterTypeFilterButton ) . not . toBeNull ( ) ; await act ( async ( ) => { clusterTypeFilterButton ! . click ( ) ; } ) ; component . update ( ) ; const clusterDeprecations = esDeprecationsMockResponse . deprecations . filter ( ( deprecation ) => deprecation . type === 'cluster_settings' ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( clusterDeprecations . length ) ; } ) ; it ( 'filters results by query string' , async ( ) => { const { find , actions } = testBed ; const multiFieldsDeprecation = esDeprecationsMockResponse . deprecations [ 2 ] ; await actions . searchBar . setSearchInputValue ( multiFieldsDeprecation . message ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( 1 ) ; expect ( find ( 'deprecationTableRow' ) . at ( 0 ) . text ( ) ) . toContain ( multiFieldsDeprecation . message ) ; } ) ; it ( 'shows error for invalid search queries' , async ( ) => { const { find , exists , actions } = testBed ; await actions . searchBar . setSearchInputValue ( '%' ) ; expect ( exists ( 'invalidSearchQueryMessage' ) ) . toBe ( true ) ; expect ( find ( 'invalidSearchQueryMessage' ) . text ( ) ) . toContain ( 'Invalid search' ) ; } ) ; it ( 'shows message when search query does not return results' , async ( ) => { const { find , actions , exists } = testBed ; await actions . searchBar . setSearchInputValue ( 'foobarbaz' ) ; expect ( exists ( 'noDeprecationsRow' ) ) . toBe ( true ) ; expect ( find ( 'noDeprecationsRow' ) . text ( ) ) . toContain ( 'No Elasticsearch deprecation issues found' ) ; } ) ; } ) ; describe ( 'pagination' , ( ) => { const esDeprecationsMockResponseWithManyDeprecations = createEsDeprecationsMockResponse ( 20 ) ; const { deprecations } = esDeprecationsMockResponseWithManyDeprecations ; beforeEach ( async ( ) => { httpRequestsMockHelpers . setLoadEsDeprecationsResponse ( esDeprecationsMockResponseWithManyDeprecations ) ; httpRequestsMockHelpers . setUpgradeMlSnapshotStatusResponse ( { nodeId : 'my_node' , snapshotId : MOCK_SNAPSHOT_ID , jobId : MOCK_JOB_ID , status : 'idle' , } ) ; await act ( async ( ) => { testBed = await setupElasticsearchPage ( { isReadOnlyMode : false } ) ; } ) ; testBed . component . update ( ) ; } ) ; it ( 'shows the correct number of pages and deprecations per page' , async ( ) => { const { find , actions } = testBed ; expect ( find ( 'esDeprecationsPagination' ) . find ( '.euiPagination__item' ) . length ) . toEqual ( Math . round ( deprecations . length / 50 ) ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( 50 ) ; await actions . pagination . clickPaginationAt ( 1 ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( deprecations . length - 50 ) ; } ) ; it ( 'allows the number of viewable rows to change' , async ( ) => { const { find , actions , component } = testBed ; await actions . pagination . clickRowsPerPageDropdown ( ) ; const rowsPerPageButton = document . body . querySelector ( '[data-test-subj="tablePagination-100-rows"]' ) ; expect ( rowsPerPageButton ) . not . toBeNull ( ) ; await act ( async ( ) => { rowsPerPageButton ! . click ( ) ; } ) ; component . update ( ) ; expect ( find ( 'esDeprecationsPagination' ) . find ( '.euiPagination__item' ) . length ) . toEqual ( Math . round ( deprecations . length / 100 ) ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( deprecations . length ) ; } ) ; it ( 'updates pagination when filters change' , async ( ) => { const { actions , find } = testBed ; const criticalDeprecations = deprecations . filter ( ( deprecation ) => deprecation . isCritical ) ; await actions . searchBar . clickCriticalFilterButton ( ) ; expect ( find ( 'esDeprecationsPagination' ) . find ( '.euiPagination__item' ) . length ) . toEqual ( 1 ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( criticalDeprecations . length ) ; } ) ; it ( 'updates pagination on search' , async ( ) => { const { actions , find } = testBed ; const reindexDeprecations = deprecations . filter ( ( deprecation ) => deprecation . correctiveAction ?. type === 'reindex' ) ; await actions . searchBar . setSearchInputValue ( 'Index created before 7.0' ) ; expect ( find ( 'esDeprecationsPagination' ) . find ( '.euiPagination__item' ) . length ) . toEqual ( 1 ) ; expect ( find ( 'deprecationTableRow' ) . length ) . toEqual ( reindexDeprecations . length ) ; } ) ; } ) ; describe ( 'no deprecations' , ( ) => { beforeEach ( async ( ) => { const noDeprecationsResponse = { totalCriticalDeprecations : 0 , deprecations : [ ] , } ; httpRequestsMockHelpers . setLoadEsDeprecationsResponse ( noDeprecationsResponse ) ; await act ( async ( ) => { testBed = await setupElasticsearchPage ( { isReadOnlyMode : false } ) ; } ) ; testBed . component . update ( ) ; } ) ; test ( 'renders prompt' , ( ) => { const { exists , find } = testBed ; expect ( exists ( 'noDeprecationsPrompt' ) ) . toBe ( true ) ; expect ( find ( 'noDeprecationsPrompt' ) . text ( ) ) . toContain ( 'Your Elasticsearch configuration is up to date' ) ; } ) ; } ) ; } ) ;