import React from 'react' ; import { mountWithIntl , shallowWithIntl } from '@kbn/test/jest' ; import TelemetryManagementSection from './telemetry_management_section' ; import { TelemetryService } from '../../../telemetry/public/services' ; import { coreMock } from '../../../../core/public/mocks' ; import { render } from '@testing-library/react' ; import type { DocLinksStart } from 'src/core/public' ; describe ( 'TelemetryManagementSectionComponent' , ( ) => { const coreStart = coreMock . createStart ( ) ; const docLinks = { } as DocLinksStart [ 'links' ] ; const coreSetup = coreMock . createSetup ( ) ; it ( 'renders as expected' , ( ) => { const onQueryMatchChange = jest . fn ( ) ; const telemetryService = new TelemetryService ( { config : { sendUsageTo : 'staging' , enabled : true , banner : true , allowChangingOptInStatus : true , optIn : true , sendUsageFrom : 'browser' , } , isScreenshotMode : false , reportOptInStatusChange : false , currentKibanaVersion : 'mock_kibana_version' , notifications : coreStart . notifications , http : coreSetup . http , } ) ; expect ( shallowWithIntl ( < TelemetryManagementSection telemetryService = { telemetryService } onQueryMatchChange = { onQueryMatchChange } showAppliesSettingMessage = { true } enableSaving = { true } toasts = { coreStart . notifications . toasts } docLinks = { docLinks } / > ) ) . toMatchSnapshot ( ) ; } ) ; it ( 'renders null because query does not match the SEARCH_TERMS' , ( ) => { const onQueryMatchChange = jest . fn ( ) ; const telemetryService = new TelemetryService ( { config : { enabled : true , banner : true , allowChangingOptInStatus : true , optIn : false , sendUsageFrom : 'browser' , sendUsageTo : 'staging' , } , isScreenshotMode : false , reportOptInStatusChange : false , notifications : coreStart . notifications , currentKibanaVersion : 'mock_kibana_version' , http : coreSetup . http , } ) ; const component = render ( < React . Suspense fallback = { < span > Fallback < /span>}> < TelemetryManagementSection telemetryService = { telemetryService } onQueryMatchChange = { onQueryMatchChange } showAppliesSettingMessage = { false } enableSaving = { true } toasts = { coreStart . notifications . toasts } docLinks = { docLinks } / > < /React.Suspense>); try { component . rerender ( < React . Suspense fallback = { < span > Fallback < /span>}> < TelemetryManagementSection query = { { text : 'asdasdasd' } } telemetryService = { telemetryService } onQueryMatchChange = { onQueryMatchChange } showAppliesSettingMessage = { false } enableSaving = { true } toasts = { coreStart . notifications . toasts } docLinks = { docLinks } / > < /React.Suspense>); expect ( onQueryMatchChange ) . toHaveBeenCalledWith ( false ) ; expect ( onQueryMatchChange ) . toHaveBeenCalledTimes ( 1 ) ; } finally { component . unmount ( ) ; } } ) ; it ( 'renders because query matches the SEARCH_TERMS' , ( ) => { const onQueryMatchChange = jest . fn ( ) ; const telemetryService = new TelemetryService ( { config : { enabled : true , banner : true , allowChangingOptInStatus : true , optIn : false , sendUsageTo : 'staging' , sendUsageFrom : 'browser' , } , isScreenshotMode : false , reportOptInStatusChange : false , notifications : coreStart . notifications , currentKibanaVersion : 'mock_kibana_version' , http : coreSetup . http , } ) ; const component = mountWithIntl ( < TelemetryManagementSection telemetryService = { telemetryService } onQueryMatchChange = { onQueryMatchChange } showAppliesSettingMessage = { false } enableSaving = { true } toasts = { coreStart . notifications . toasts } docLinks = { docLinks } / > ) ; try { expect ( component . setProps ( { ... component . props ( ) , query : { text : 'TeLEMetry' } } ) . html ( ) ) . not . toBe ( '' ) ; expect ( component . setProps ( { ... component . props ( ) , query : { } } ) . html ( ) ) . not . toBe ( '' ) ; expect ( onQueryMatchChange ) . toHaveBeenCalledWith ( true ) ; expect ( onQueryMatchChange ) . toHaveBeenCalledTimes ( 1 ) ; } finally { component . unmount ( ) ; } } ) ; it ( 'renders null because allowChangingOptInStatus is false' , ( ) => { const onQueryMatchChange = jest . fn ( ) ; const telemetryService = new TelemetryService ( { config : { enabled : true , banner : true , allowChangingOptInStatus : false , optIn : true , sendUsageTo : 'staging' , sendUsageFrom : 'browser' , } , isScreenshotMode : false , reportOptInStatusChange : false , notifications : coreStart . notifications , currentKibanaVersion : 'mock_kibana_version' , http : coreSetup . http , } ) ; const component = mountWithIntl ( < TelemetryManagementSection telemetryService = { telemetryService } onQueryMatchChange = { onQueryMatchChange } showAppliesSettingMessage = { true } enableSaving = { true } toasts = { coreStart . notifications . toasts } docLinks = { docLinks } / > ) ; try { expect ( component ) . toMatchSnapshot ( ) ; component . setProps ( { ... component . props ( ) , query : { text : 'TeLEMetry' } } ) ; expect ( onQueryMatchChange ) . toHaveBeenCalledWith ( false ) ; } finally { component . unmount ( ) ; } } ) ; it ( 'shows the OptInExampleFlyout' , ( ) => { const onQueryMatchChange = jest . fn ( ) ; const telemetryService = new TelemetryService ( { config : { enabled : true , banner : true , allowChangingOptInStatus : true , optIn : false , sendUsageTo : 'staging' , sendUsageFrom : 'browser' , } , isScreenshotMode : false , reportOptInStatusChange : false , notifications : coreStart . notifications , currentKibanaVersion : 'mock_kibana_version' , http : coreSetup . http , } ) ; const component = mountWithIntl ( < TelemetryManagementSection telemetryService = { telemetryService } onQueryMatchChange = { onQueryMatchChange } showAppliesSettingMessage = { false } enableSaving = { true } toasts = { coreStart . notifications . toasts } docLinks = { docLinks } / > ) ; try { const toggleExampleComponent = component . find ( 'FormattedMessage > EuiLink[onClick]' ) . at ( 0 ) ; const updatedView = toggleExampleComponent . simulate ( 'click' ) ; updatedView . find ( 'OptInExampleFlyout' ) ; updatedView . simulate ( 'close' ) ; } finally { component . unmount ( ) ; } } ) ; it ( 'toggles the OptIn button' , async ( ) => { const onQueryMatchChange = jest . fn ( ) ; const telemetryService = new TelemetryService ( { config : { enabled : true , banner : true , allowChangingOptInStatus : true , optIn : false , sendUsageTo : 'staging' , sendUsageFrom : 'browser' , } , isScreenshotMode : false , reportOptInStatusChange : false , notifications : coreStart . notifications , currentKibanaVersion : 'mock_kibana_version' , http : coreSetup . http , } ) ; const component = mountWithIntl ( < TelemetryManagementSection telemetryService = { telemetryService } onQueryMatchChange = { onQueryMatchChange } showAppliesSettingMessage = { false } enableSaving = { true } toasts = { coreStart . notifications . toasts } docLinks = { docLinks } / > ) ; try { const toggleOptInComponent = component . find ( 'Field' ) ; await expect ( toggleOptInComponent . prop < TelemetryManagementSection [ 'toggleOptIn' ] > ( 'handleChange' ) ( ) ) . resolves . toBe ( true ) ; expect ( ( component . state ( ) as { enabled : boolean ; } ) . enabled ) . toBe ( true ) ; await expect ( toggleOptInComponent . prop < TelemetryManagementSection [ 'toggleOptIn' ] > ( 'handleChange' ) ( ) ) . resolves . toBe ( true ) ; expect ( ( component . state ( ) as { enabled : boolean ; } ) . enabled ) . toBe ( false ) ; telemetryService . setOptIn = jest . fn ( ) . mockRejectedValue ( Error ( 'test-error' ) ) ; await expect ( toggleOptInComponent . prop < TelemetryManagementSection [ 'toggleOptIn' ] > ( 'handleChange' ) ( ) ) . rejects . toStrictEqual ( Error ( 'test-error' ) ) ; } finally { component . unmount ( ) ; } } ) ; it ( 'test the wrapper (for coverage purposes)' , ( ) => { const onQueryMatchChange = jest . fn ( ) ; const telemetryService = new TelemetryService ( { config : { enabled : true , banner : true , allowChangingOptInStatus : false , optIn : false , sendUsageTo : 'staging' , sendUsageFrom : 'browser' , } , isScreenshotMode : false , reportOptInStatusChange : false , notifications : coreStart . notifications , currentKibanaVersion : 'mock_kibana_version' , http : coreSetup . http , } ) ; expect ( shallowWithIntl ( < TelemetryManagementSection showAppliesSettingMessage = { true } telemetryService = { telemetryService } onQueryMatchChange = { onQueryMatchChange } enableSaving = { true } toasts = { coreStart . notifications . toasts } docLinks = { docLinks } / > ) . html ( ) ) . toMatchSnapshot ( ) ; } ) ; } ) ;