import { render , waitFor } from '@testing-library/react' ; import { mount , MountRendererProps , ReactWrapper } from 'enzyme' ; import enzymeToJson from 'enzyme-to-json' ; import { Location } from 'history' ; import moment from 'moment' ; import { Moment } from 'moment-timezone' ; import React from 'react' ; import { MemoryRouter } from 'react-router-dom' ; import { EuiThemeProvider } from '../../../../../src/plugins/kibana_react/common' ; import { ESSearchRequest , ESSearchResponse , } from '../../../../../src/core/types/elasticsearch' ; import { APMConfig } from '../../server' ; import { UxUIFilters } from '../../typings/ui_filters' ; import { MockApmPluginContextWrapper } from '../context/apm_plugin/mock_apm_plugin_context' ; import { UrlParamsProvider } from '../context/url_params_context/url_params_context' ; const originalConsoleWarn = console . warn ; /**
 *  A dependency we're using is using deprecated react methods. Override the
 * console to hide the warnings. These should go away when we switch to
 * Elastic Charts
 */ export function disableConsoleWarning ( messageToDisable ) { return jest . spyOn ( console , 'warn' ) . mockImplementation ( ( message ) => { if ( ! message . startsWith ( messageToDisable ) ) { originalConsoleWarn ( message ) ; } } ) ; } export function toJson ( wrapper ) { return enzymeToJson ( wrapper , { noKey : true , mode : 'deep' , } ) ; } export function mockMoment ( ) { jest . spyOn ( moment . prototype , 'format' ) . mockImplementation ( function ( this ) { return `1st of January (mocking ${ this . unix ( ) })` ; } ) ; jest . spyOn ( moment . prototype , 'fromNow' ) . mockImplementation ( function ( this ) { return `1337 minutes ago (mocking ${ this . unix ( ) })` ; } ) ; } export async function getRenderedHref ( Component , location ) { const el = render ( < MemoryRouter initialEntries = { [ location ] } > < MockApmPluginContextWrapper > < UrlParamsProvider > < Component /> < /UrlParamsProvider> < /MockApmPluginContextWrapper> < /MemoryRouter>); const a = el . container . querySelector ( 'a' ) ; await waitFor ( ( ) => { } , { container : a ! } ) ; return a ? a . getAttribute ( 'href' ) : '' ; } export function mockNow ( date ) { const fakeNow = new Date ( date ) . getTime ( ) ; return jest . spyOn ( Date , 'now' ) . mockReturnValue ( fakeNow ) ; } export function delay ( ms ) { return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } export function expectTextsNotInDocument ( output , texts ) { texts . forEach ( ( text ) => { try { output . getByText ( text ) ; } catch ( err ) { if ( err . message . startsWith ( 'Unable to find an element with the text:' ) ) { return ; } else { throw err ; } } throw new Error ( `Unexpected text found: ${ text }` ) ; } ) ; } export function expectTextsInDocument ( output , texts ) { texts . forEach ( ( text ) => { expect ( output . getByText ( text ) ) . toBeInTheDocument ( ) ; } ) ; } interface MockSetup { apmEventClient : any ; internalClient : any ; config : APMConfig ; uiFilters : UxUIFilters ; indices : { sourcemap : string ; error : string ; onboarding : string ; span : string ; transaction : string ; metric : string ; apmAgentConfigurationIndex : string ; apmCustomLinkIndex : string ; } ; } interface Options { mockResponse ? : ( request : ESSearchRequest ) => ESSearchResponse < unknown , ESSearchRequest > ; } export async function inspectSearchParams ( fn , options = { } ) { const spy = jest . fn ( ) . mockImplementation ( async ( request ) => { return options . mockResponse ? options . mockResponse ( request ) : { hits : { hits : { total : { value : 0 , } , } , } , } ; } ) ; let response ; let error ; const mockSetup = { apmEventClient : { search : spy } as any , internalClient : { search : spy } as any , config : new Proxy ( { } , { get : ( _ , key ) => { switch ( key ) { default : return 'myIndex' ; case 'xpack.apm.metricsInterval' : return 30 ; } } , } ) as APMConfig , uiFilters : { } , indices : { sourcemap : 'myIndex' , error : 'myIndex' , onboarding : 'myIndex' , span : 'myIndex' , transaction : 'myIndex' , metric : 'myIndex' , apmAgentConfigurationIndex : 'myIndex' , apmCustomLinkIndex : 'myIndex' , } , } ; try { response = await fn ( mockSetup ) ; } catch ( err ) { error = err ; } return { params : spy . mock . calls [ 0 ] [ 0 ] , response , error , spy , teardown : ( ) => spy . mockClear ( ) , } ; } export type SearchParamsMock = Awaited < ReturnType < typeof inspectSearchParams > > ; export function renderWithTheme ( component , params ? , { darkMode = false } = { } ) { return render ( < EuiThemeProvider darkMode = { darkMode } > { component } < /EuiThemeProvider>, params); } export function mountWithTheme ( tree , { darkMode = false } = { } ) { function WrappingThemeProvider ( props ) { return ( < EuiThemeProvider darkMode = { darkMode } > { props . children } < /EuiThemeProvider>); } return mount ( tree , { wrappingComponent : WrappingThemeProvider , } as MountRendererProps ) ; }