11import type { FC , ReactNode } from 'react' ;
2- import { render , waitFor } from '@testing-library/react' ;
2+ import { screen , waitFor } from '@testing-library/react' ;
33import userEvent from '@testing-library/user-event' ;
4- import type { FormikConfig } from 'formik' ;
54import { Formik } from 'formik' ;
5+ import { useK8sWatchResources } from '@console/internal/components/utils/k8s-watch-hook' ;
6+ import { renderWithProviders } from '@console/shared/src/test-utils/unit-test-utils' ;
67import type { TriggersSectionFormData } from '../TriggersSection' ;
78import TriggersSection from '../TriggersSection' ;
89
9- interface WrapperProps extends FormikConfig < TriggersSectionFormData > {
10- children ?: ReactNode ;
10+ // Mock PatternFly topology to prevent console warnings during tests
11+ jest . mock ( '@patternfly/react-topology' , ( ) => ( { } ) ) ;
12+
13+ jest . mock ( '@console/internal/components/utils/k8s-watch-hook' , ( ) => ( {
14+ useK8sWatchResources : jest . fn ( ) ,
15+ } ) ) ;
16+
17+ const mockUseK8sWatchResources = useK8sWatchResources as jest . Mock ;
18+
19+ const initialValues : TriggersSectionFormData = {
20+ formData : {
21+ triggers : {
22+ configChange : false ,
23+ imageChange : false ,
24+ otherTriggers : [ ] ,
25+ } ,
26+ } ,
27+ } ;
28+
29+ interface FormikWrapperProps {
30+ children : ReactNode ;
31+ onSubmit : jest . Mock ;
1132}
1233
13- const Wrapper : FC < WrapperProps > = ( { children, ... formikConfig } ) => (
14- < Formik { ... formikConfig } >
34+ const FormikWrapper : FC < FormikWrapperProps > = ( { children, onSubmit } ) => (
35+ < Formik initialValues = { initialValues } onSubmit = { onSubmit } >
1536 { ( formikProps ) => (
1637 < form onSubmit = { formikProps . handleSubmit } >
1738 { children }
@@ -21,100 +42,93 @@ const Wrapper: FC<WrapperProps> = ({ children, ...formikConfig }) => (
2142 </ Formik >
2243) ;
2344
24- const initialValues : TriggersSectionFormData = {
25- formData : {
26- triggers : {
27- configChange : false ,
28- imageChange : false ,
29- otherTriggers : [ ] ,
30- } ,
31- } ,
45+ const renderTriggersSection = ( onSubmit : jest . Mock ) => {
46+ renderWithProviders (
47+ < FormikWrapper onSubmit = { onSubmit } >
48+ < TriggersSection namespace = "a-namespace" />
49+ </ FormikWrapper > ,
50+ ) ;
3251} ;
3352
3453describe ( 'TriggersSection' , ( ) => {
35- it ( 'should render empty form' , ( ) => {
54+ beforeEach ( ( ) => {
55+ mockUseK8sWatchResources . mockReturnValue ( {
56+ secrets : { data : [ ] , loaded : true , loadError : null } ,
57+ } ) ;
58+ } ) ;
59+
60+ afterEach ( ( ) => {
61+ jest . restoreAllMocks ( ) ;
62+ } ) ;
63+
64+ it ( 'should render all trigger configuration options' , ( ) => {
3665 const onSubmit = jest . fn ( ) ;
3766
38- const renderResult = render (
39- < Wrapper initialValues = { initialValues } onSubmit = { onSubmit } >
40- < TriggersSection namespace = "a-namespace" />
41- </ Wrapper > ,
42- ) ;
67+ renderTriggersSection ( onSubmit ) ;
4368
44- renderResult . getByTestId ( 'section triggers' ) ;
45- renderResult . getByText ( 'Triggers' ) ;
46- renderResult . getByText ( 'Automatically build a new image when config changes' ) ;
47- renderResult . getByText ( 'Automatically build a new image when image changes' ) ;
48- renderResult . getByText ( 'Add trigger' ) ;
69+ expect ( screen . getByTestId ( 'section triggers' ) ) . toBeInTheDocument ( ) ;
70+ expect ( screen . getByText ( 'Triggers' ) ) . toBeVisible ( ) ;
71+ expect (
72+ screen . getByText ( 'Automatically build a new image when config changes' ) ,
73+ ) . toBeInTheDocument ( ) ;
74+ expect ( screen . getByText ( 'Automatically build a new image when image changes' ) ) . toBeVisible ( ) ;
75+ expect ( screen . getByRole ( 'button' , { name : 'Add trigger' } ) ) . toBeVisible ( ) ;
4976
50- expect ( ( renderResult . getByTestId ( 'image-change checkbox' ) as HTMLInputElement ) . checked ) . toBe (
51- false ,
52- ) ;
77+ const imageChangeCheckbox = screen . getByTestId ( 'image-change checkbox' ) as HTMLInputElement ;
78+ expect ( imageChangeCheckbox ) . not . toBeChecked ( ) ;
5379
54- expect ( onSubmit ) . toHaveBeenCalledTimes ( 0 ) ;
80+ expect ( onSubmit ) . not . toHaveBeenCalled ( ) ;
5581 } ) ;
5682
57- it ( 'should allow user to change config change checkbox trigger and save this data ' , async ( ) => {
83+ it ( 'should toggle config change checkbox and submit form with updated value ' , async ( ) => {
5884 const user = userEvent . setup ( ) ;
5985 const onSubmit = jest . fn ( ) ;
86+ renderTriggersSection ( onSubmit ) ;
6087
61- const renderResult = render (
62- < Wrapper initialValues = { initialValues } onSubmit = { onSubmit } >
63- < TriggersSection namespace = "a-namespace" />
64- </ Wrapper > ,
65- ) ;
66-
67- // Change form
68- await user . click ( renderResult . getByTestId ( 'config-change checkbox' ) ) ;
88+ const configChangeCheckbox = screen . getByTestId ( 'config-change checkbox' ) ;
89+ await user . click ( configChangeCheckbox ) ;
6990
70- // Submit
71- const submitButton = renderResult . getByRole ( 'button' , { name : 'Submit' } ) ;
91+ const submitButton = screen . getByRole ( 'button' , { name : 'Submit' } ) ;
7292 await user . click ( submitButton ) ;
93+
7394 await waitFor ( ( ) => {
7495 expect ( onSubmit ) . toHaveBeenCalledTimes ( 1 ) ;
7596 } ) ;
7697
77- const expectedFormData : TriggersSectionFormData = {
78- formData : {
79- triggers : {
80- configChange : true ,
81- imageChange : false ,
82- otherTriggers : [ ] ,
83- } ,
84- } ,
85- } ;
86- expect ( onSubmit ) . toHaveBeenLastCalledWith ( expectedFormData , expect . anything ( ) ) ;
98+ expect ( onSubmit . mock . calls [ 0 ] [ 0 ] ) . toEqual (
99+ expect . objectContaining ( {
100+ formData : expect . objectContaining ( {
101+ triggers : expect . objectContaining ( {
102+ configChange : true ,
103+ } ) ,
104+ } ) ,
105+ } ) ,
106+ ) ;
87107 } ) ;
88108
89- it ( 'should allow user to change image change checkbox trigger and save this data ' , async ( ) => {
109+ it ( 'should toggle image change checkbox and submit form with updated value ' , async ( ) => {
90110 const user = userEvent . setup ( ) ;
91111 const onSubmit = jest . fn ( ) ;
112+ renderTriggersSection ( onSubmit ) ;
92113
93- const renderResult = render (
94- < Wrapper initialValues = { initialValues } onSubmit = { onSubmit } >
95- < TriggersSection namespace = "a-namespace" />
96- </ Wrapper > ,
97- ) ;
98-
99- // Change form
100- await user . click ( renderResult . getByTestId ( 'image-change checkbox' ) ) ;
114+ const imageChangeCheckbox = screen . getByTestId ( 'image-change checkbox' ) ;
115+ await user . click ( imageChangeCheckbox ) ;
101116
102- // Submit
103- const submitButton = renderResult . getByRole ( 'button' , { name : 'Submit' } ) ;
117+ const submitButton = screen . getByRole ( 'button' , { name : 'Submit' } ) ;
104118 await user . click ( submitButton ) ;
119+
105120 await waitFor ( ( ) => {
106121 expect ( onSubmit ) . toHaveBeenCalledTimes ( 1 ) ;
107122 } ) ;
108123
109- const expectedFormData : TriggersSectionFormData = {
110- formData : {
111- triggers : {
112- configChange : false ,
113- imageChange : true ,
114- otherTriggers : [ ] ,
115- } ,
116- } ,
117- } ;
118- expect ( onSubmit ) . toHaveBeenLastCalledWith ( expectedFormData , expect . anything ( ) ) ;
124+ expect ( onSubmit . mock . calls [ 0 ] [ 0 ] ) . toEqual (
125+ expect . objectContaining ( {
126+ formData : expect . objectContaining ( {
127+ triggers : expect . objectContaining ( {
128+ imageChange : true ,
129+ } ) ,
130+ } ) ,
131+ } ) ,
132+ ) ;
119133 } ) ;
120134} ) ;
0 commit comments