@@ -26,32 +26,40 @@ import {
2626 INITIAL_DETECTOR_DEFINITION_VALUES ,
2727 testDetectorDefinitionValues ,
2828} from '../../utils/constants' ;
29+ import { getRandomDetector } from '../../../../redux/reducers/__tests__/utils' ;
30+ import { useFetchDetectorInfo } from '../../../CreateDetectorSteps/hooks/useFetchDetectorInfo' ;
2931
3032jest . mock ( '../../../../services' , ( ) => {
3133 const originalModule = jest . requireActual ( '../../../../services' ) ;
3234
3335 return {
3436 ...originalModule ,
3537 getDataSourceEnabled : ( ) => ( {
36- enabled : false
38+ enabled : false ,
3739 } ) ,
3840 getUISettings : ( ) => ( {
3941 get : ( flag ) => {
4042 if ( flag === 'home:useNewHomePage' ) {
41- return false ;
43+ return false ;
4244 }
4345 return originalModule . getUISettings ( ) . get ( flag ) ;
44- }
46+ } ,
4547 } ) ,
4648 getNavigationUI : ( ) => ( {
47- HeaderControl : null
49+ HeaderControl : null ,
4850 } ) ,
4951 getApplication : ( ) => ( {
50- setAppRightControls : null ,
51- } )
52+ setAppRightControls : null ,
53+ } ) ,
5254 } ;
5355} ) ;
5456
57+ jest . mock ( '../../../CreateDetectorSteps/hooks/useFetchDetectorInfo' , ( ) => ( {
58+ // Within each test, the mock implementation for useFetchDetectorInfo is set using jest.Mock.
59+ // This ensures that the hook returns the desired values for each test case.
60+ useFetchDetectorInfo : jest . fn ( ) ,
61+ } ) ) ;
62+
5563const renderWithRouterEmpty = ( isEdit : boolean = false ) => ( {
5664 ...render (
5765 < Provider store = { configureStore ( httpClientMock ) } >
@@ -98,6 +106,28 @@ const renderWithRouterFull = (isEdit: boolean = false) => ({
98106 ) ,
99107} ) ;
100108
109+ const renderWithRouterFullAndEdit = ( isEdit : boolean = true ) => ( {
110+ ...render (
111+ < Provider store = { configureStore ( httpClientMock ) } >
112+ < Router >
113+ < Switch >
114+ < Route
115+ render = { ( props : RouteComponentProps ) => (
116+ < CoreServicesContext . Provider value = { coreServicesMock } >
117+ < DefineDetector
118+ setActionMenu = { jest . fn ( ) }
119+ isEdit = { isEdit }
120+ { ...props }
121+ />
122+ </ CoreServicesContext . Provider >
123+ ) }
124+ />
125+ </ Switch >
126+ </ Router >
127+ </ Provider >
128+ ) ,
129+ } ) ;
130+
101131describe ( '<DefineDetector /> Full' , ( ) => {
102132 beforeEach ( ( ) => {
103133 jest . clearAllMocks ( ) ;
@@ -106,12 +136,32 @@ describe('<DefineDetector /> Full', () => {
106136 } ) ;
107137 describe ( 'creating detector definition' , ( ) => {
108138 test ( 'renders the component' , ( ) => {
139+ const detectorInfo = {
140+ detector : getRandomDetector ( true ) ,
141+ hasError : false ,
142+ isLoadingDetector : true ,
143+ errorMessage : undefined ,
144+ } ;
145+
146+ ( useFetchDetectorInfo as jest . Mock ) . mockImplementation (
147+ ( ) => detectorInfo
148+ ) ;
109149 const { container, getByText } = renderWithRouterFull ( false ) ;
110150 getByText ( 'Define detector' ) ;
111151 expect ( container . firstChild ) . toMatchSnapshot ( ) ;
112152 } ) ;
113153
114154 test ( 'duplicate name' , async ( ) => {
155+ const detectorInfo = {
156+ detector : getRandomDetector ( true ) ,
157+ hasError : false ,
158+ isLoadingDetector : true ,
159+ errorMessage : undefined ,
160+ } ;
161+
162+ ( useFetchDetectorInfo as jest . Mock ) . mockImplementation (
163+ ( ) => detectorInfo
164+ ) ;
115165 httpClientMock . get = jest . fn ( ) . mockResolvedValue ( {
116166 ok : true ,
117167 response : {
@@ -130,6 +180,31 @@ describe('<DefineDetector /> Full', () => {
130180 } ) ;
131181} ) ;
132182
183+ describe ( '<DefineDetector /> FullEdit' , ( ) => {
184+ beforeEach ( ( ) => {
185+ console . error = jest . fn ( ) ;
186+ console . warn = jest . fn ( ) ;
187+ } ) ;
188+ describe ( 'editing detector page' , ( ) => {
189+ test ( 'renders the component' , ( ) => {
190+ const detectorInfo = {
191+ detector : getRandomDetector ( true , '' , [ 'opensearch:index-1' ] ) ,
192+ hasError : false ,
193+ isLoadingDetector : true ,
194+ errorMessage : undefined ,
195+ } ;
196+
197+ ( useFetchDetectorInfo as jest . Mock ) . mockImplementation (
198+ ( ) => detectorInfo
199+ ) ;
200+ const { container, getByText } = renderWithRouterFullAndEdit ( true ) ;
201+ getByText ( 'Edit detector settings' ) ;
202+ getByText ( 'opensearch (Remote)' ) ;
203+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
204+ } ) ;
205+ } ) ;
206+ } ) ;
207+
133208describe ( '<DefineDetector /> empty' , ( ) => {
134209 beforeEach ( ( ) => {
135210 jest . clearAllMocks ( ) ;
0 commit comments