44 * you may not use this file except in compliance with the Elastic License.
55 */
66import moment from 'moment-timezone' ;
7- import React from 'react' ;
8- import { Provider } from 'react-redux' ;
9- // axios has a $http like interface so using it to simulate $http
10- import axios from 'axios' ;
11- import axiosXhrAdapter from 'axios/lib/adapters/xhr' ;
12- import sinon from 'sinon' ;
7+ import React , { ReactElement } from 'react' ;
8+ import { ReactWrapper } from 'enzyme' ;
9+ import { mountWithIntl } from 'test_utils/enzyme_helpers' ;
1310import { findTestSubject , takeMountedSnapshot } from '@elastic/eui/lib/test' ;
1411
15- import { scopedHistoryMock } from '../../../../../src/core/public/mocks' ;
16- import { mountWithIntl } from '../../../../test_utils/enzyme_helpers' ;
17- import { fetchedPolicies } from '../../public/application/store/actions' ;
18- import { indexLifecycleManagementStore } from '../../public/application/store' ;
19- import { PolicyTable } from '../../public/application/sections/policy_table' ;
12+ import {
13+ fatalErrorsServiceMock ,
14+ injectedMetadataServiceMock ,
15+ scopedHistoryMock ,
16+ } from '../../../../../src/core/public/mocks' ;
17+ import { HttpService } from '../../../../../src/core/public/http' ;
18+ import { usageCollectionPluginMock } from '../../../../../src/plugins/usage_collection/public/mocks' ;
19+
20+ import { PolicyTable } from '../../public/application/sections/policy_table/policy_table' ;
2021import { init as initHttp } from '../../public/application/services/http' ;
2122import { init as initUiMetric } from '../../public/application/services/ui_metric' ;
23+ import { PolicyFromES } from '../../public/application/services/policies/types' ;
2224
23- initHttp ( axios . create ( { adapter : axiosXhrAdapter } ) , ( path ) => path ) ;
24- initUiMetric ( { reportUiStats : ( ) => { } } ) ;
25-
26- let server = null ;
25+ initHttp (
26+ new HttpService ( ) . setup ( {
27+ injectedMetadata : injectedMetadataServiceMock . createSetupContract ( ) ,
28+ fatalErrors : fatalErrorsServiceMock . createSetupContract ( ) ,
29+ } )
30+ ) ;
31+ initUiMetric ( usageCollectionPluginMock . createSetupContract ( ) ) ;
2732
28- let store = null ;
29- const policies = [ ] ;
33+ const policies : PolicyFromES [ ] = [ ] ;
3034for ( let i = 0 ; i < 105 ; i ++ ) {
3135 policies . push ( {
3236 version : i ,
33- modified_date : moment ( ) . subtract ( i , 'days' ) . valueOf ( ) ,
34- linkedIndices : i % 2 === 0 ? [ `index${ i } ` ] : null ,
37+ modified_date : moment ( ) . subtract ( i , 'days' ) . toISOString ( ) ,
38+ linkedIndices : i % 2 === 0 ? [ `index${ i } ` ] : undefined ,
3539 name : `testy${ i } ` ,
40+ policy : {
41+ name : `testy${ i } ` ,
42+ phases : { } ,
43+ } ,
3644 } ) ;
3745}
3846jest . mock ( '' ) ;
39- let component = null ;
47+ let component : ReactElement ;
4048
41- const snapshot = ( rendered ) => {
49+ const snapshot = ( rendered : string [ ] ) => {
4250 expect ( rendered ) . toMatchSnapshot ( ) ;
4351} ;
44- const mountedSnapshot = ( rendered ) => {
52+ const mountedSnapshot = ( rendered : ReactWrapper ) => {
4553 expect ( takeMountedSnapshot ( rendered ) ) . toMatchSnapshot ( ) ;
4654} ;
47- const names = ( rendered ) => {
55+ const names = ( rendered : ReactWrapper ) => {
4856 return findTestSubject ( rendered , 'policyTablePolicyNameLink' ) ;
4957} ;
50- const namesText = ( rendered ) => {
51- return names ( rendered ) . map ( ( button ) => button . text ( ) ) ;
58+ const namesText = ( rendered : ReactWrapper ) : string [ ] => {
59+ return ( names ( rendered ) as ReactWrapper ) . map ( ( button ) => button . text ( ) ) ;
5260} ;
5361
54- const testSort = ( headerName ) => {
62+ const testSort = ( headerName : string ) => {
5563 const rendered = mountWithIntl ( component ) ;
5664 const nameHeader = findTestSubject ( rendered , `policyTableHeaderCell-${ headerName } ` ) . find (
5765 'button'
@@ -63,7 +71,7 @@ const testSort = (headerName) => {
6371 rendered . update ( ) ;
6472 snapshot ( namesText ( rendered ) ) ;
6573} ;
66- const openContextMenu = ( buttonIndex ) => {
74+ const openContextMenu = ( buttonIndex : number ) => {
6775 const rendered = mountWithIntl ( component ) ;
6876 const actionsButton = findTestSubject ( rendered , 'policyActionsContextMenuButton' ) ;
6977 actionsButton . at ( buttonIndex ) . simulate ( 'click' ) ;
@@ -73,33 +81,26 @@ const openContextMenu = (buttonIndex) => {
7381
7482describe ( 'policy table' , ( ) => {
7583 beforeEach ( ( ) => {
76- store = indexLifecycleManagementStore ( ) ;
7784 component = (
78- < Provider store = { store } >
79- < PolicyTable history = { scopedHistoryMock . create ( ) } navigateToApp = { ( ) => { } } />
80- </ Provider >
85+ < PolicyTable
86+ policies = { policies }
87+ history = { scopedHistoryMock . create ( ) }
88+ navigateToApp = { jest . fn ( ) }
89+ updatePolicies = { jest . fn ( ) }
90+ />
8191 ) ;
82- store . dispatch ( fetchedPolicies ( policies ) ) ;
83- server = sinon . fakeServer . create ( ) ;
84- server . respondWith ( '/api/index_lifecycle_management/policies' , [
85- 200 ,
86- { 'Content-Type' : 'application/json' } ,
87- JSON . stringify ( policies ) ,
88- ] ) ;
8992 } ) ;
90- test ( 'should show spinner when policies are loading' , ( ) => {
91- store = indexLifecycleManagementStore ( ) ;
93+
94+ test ( 'should show empty state when there are not any policies' , ( ) => {
9295 component = (
93- < Provider store = { store } >
94- < PolicyTable history = { scopedHistoryMock . create ( ) } navigateToApp = { ( ) => { } } />
95- </ Provider >
96+ < PolicyTable
97+ policies = { [ ] }
98+ history = { scopedHistoryMock . create ( ) }
99+ navigateToApp = { jest . fn ( ) }
100+ updatePolicies = { jest . fn ( ) }
101+ />
96102 ) ;
97103 const rendered = mountWithIntl ( component ) ;
98- expect ( rendered . find ( '.euiLoadingSpinner' ) . exists ( ) ) . toBeTruthy ( ) ;
99- } ) ;
100- test ( 'should show empty state when there are not any policies' , ( ) => {
101- store . dispatch ( fetchedPolicies ( [ ] ) ) ;
102- const rendered = mountWithIntl ( component ) ;
103104 mountedSnapshot ( rendered ) ;
104105 } ) ;
105106 test ( 'should change pages when a pagination link is clicked on' , ( ) => {
@@ -123,7 +124,7 @@ describe('policy table', () => {
123124 test ( 'should filter based on content of search input' , ( ) => {
124125 const rendered = mountWithIntl ( component ) ;
125126 const searchInput = rendered . find ( '.euiFieldSearch' ) . first ( ) ;
126- searchInput . instance ( ) . value = 'testy0' ;
127+ ( ( searchInput . instance ( ) as unknown ) as HTMLInputElement ) . value = 'testy0' ;
127128 searchInput . simulate ( 'keyup' , { key : 'Enter' , keyCode : 13 , which : 13 } ) ;
128129 rendered . update ( ) ;
129130 snapshot ( namesText ( rendered ) ) ;
@@ -147,15 +148,15 @@ describe('policy table', () => {
147148 expect ( buttons . at ( 0 ) . text ( ) ) . toBe ( 'View indices linked to policy' ) ;
148149 expect ( buttons . at ( 1 ) . text ( ) ) . toBe ( 'Add policy to index template' ) ;
149150 expect ( buttons . at ( 2 ) . text ( ) ) . toBe ( 'Delete policy' ) ;
150- expect ( buttons . at ( 2 ) . getDOMNode ( ) . disabled ) . toBeTruthy ( ) ;
151+ expect ( ( buttons . at ( 2 ) . getDOMNode ( ) as HTMLButtonElement ) . disabled ) . toBeTruthy ( ) ;
151152 } ) ;
152153 test ( 'should have proper actions in context menu when there are not linked indices' , ( ) => {
153154 const rendered = openContextMenu ( 1 ) ;
154155 const buttons = rendered . find ( 'button.euiContextMenuItem' ) ;
155156 expect ( buttons . length ) . toBe ( 2 ) ;
156157 expect ( buttons . at ( 0 ) . text ( ) ) . toBe ( 'Add policy to index template' ) ;
157158 expect ( buttons . at ( 1 ) . text ( ) ) . toBe ( 'Delete policy' ) ;
158- expect ( buttons . at ( 1 ) . getDOMNode ( ) . disabled ) . toBeFalsy ( ) ;
159+ expect ( ( buttons . at ( 1 ) . getDOMNode ( ) as HTMLButtonElement ) . disabled ) . toBeFalsy ( ) ;
159160 } ) ;
160161 test ( 'confirmation modal should show when delete button is pressed' , ( ) => {
161162 const rendered = openContextMenu ( 1 ) ;
0 commit comments