1515 */
1616
1717import * as React from 'react' ;
18- import { act , fireEvent , render , screen , waitFor } from '@testing-library/react' ;
18+ import { fireEvent , render , screen , waitFor } from '@testing-library/react' ;
19+ import userEvent from '@testing-library/user-event' ;
1920import { vi } from 'vitest' ;
2021import { NewExperiment } from './NewExperiment' ;
2122import TestUtils from 'src/TestUtils' ;
@@ -26,7 +27,6 @@ import { logger } from 'src/lib/Utils';
2627
2728describe ( 'NewExperiment' , ( ) => {
2829 let renderResult : ReturnType < typeof render > | null = null ;
29- let newExperimentRef : React . RefObject < NewExperiment > | null = null ;
3030
3131 const createExperimentSpy = vi . spyOn ( Apis . experimentServiceApiV2 , 'createExperiment' ) ;
3232 const listPipelineVersionsSpy = vi . spyOn ( Apis . pipelineServiceApiV2 , 'listPipelineVersions' ) ;
@@ -49,19 +49,11 @@ describe('NewExperiment', () => {
4949 } as PageProps ;
5050 }
5151
52- function getInstance ( ) : NewExperiment {
53- if ( ! newExperimentRef ?. current ) {
54- throw new Error ( 'NewExperiment instance not available' ) ;
55- }
56- return newExperimentRef . current ;
57- }
58-
5952 async function renderNewExperiment (
6053 propsPatch : Partial < PageProps & { namespace ?: string } > = { } ,
6154 ) : Promise < void > {
62- newExperimentRef = React . createRef < NewExperiment > ( ) ;
6355 const props = { ...generateProps ( ) , ...propsPatch } as PageProps ;
64- renderResult = render ( < NewExperiment ref = { newExperimentRef } { ...props } /> ) ;
56+ renderResult = render ( < NewExperiment { ...props } /> ) ;
6557 await TestUtils . flushPromises ( ) ;
6658 }
6759
@@ -98,16 +90,15 @@ describe('NewExperiment', () => {
9890 afterEach ( ( ) => {
9991 renderResult ?. unmount ( ) ;
10092 renderResult = null ;
101- newExperimentRef = null ;
10293 vi . clearAllMocks ( ) ;
10394 } ) ;
10495
10596 it ( 'renders the new experiment page' , async ( ) => {
10697 await renderNewExperiment ( ) ;
107- await waitFor ( ( ) =>
108- expect ( screen . getByText ( 'Experiment name is required' ) ) . toBeInTheDocument ( ) ,
109- ) ;
110- expect ( renderResult ! . asFragment ( ) ) . toMatchSnapshot ( ) ;
98+ expect ( screen . getByText ( 'Experiment name is required' ) ) . toBeInTheDocument ( ) ;
99+ expect ( getNextButton ( ) ) . toBeDisabled ( ) ;
100+ expect ( screen . getByLabelText ( / E x p e r i m e n t n a m e / i ) ) . toBeInTheDocument ( ) ;
101+ expect ( screen . getByLabelText ( / D e s c r i p t i o n / i ) ) . toBeInTheDocument ( ) ;
111102 } ) ;
112103
113104 it ( 'does not include any action buttons in the toolbar' , async ( ) => {
@@ -126,7 +117,7 @@ describe('NewExperiment', () => {
126117 fillExperimentName ( 'experiment name' ) ;
127118
128119 await waitFor ( ( ) => expect ( getNextButton ( ) ) . not . toBeDisabled ( ) ) ;
129- expect ( renderResult ! . asFragment ( ) ) . toMatchSnapshot ( ) ;
120+ expect ( screen . getByLabelText ( / E x p e r i m e n t n a m e / i ) ) . toHaveValue ( 'experiment name' ) ;
130121 } ) ;
131122
132123 it ( "re-disables the 'Next' button when an experiment name is cleared after having been entered" , async ( ) => {
@@ -138,35 +129,27 @@ describe('NewExperiment', () => {
138129
139130 fillExperimentName ( '' ) ;
140131 await waitFor ( ( ) => expect ( getNextButton ( ) ) . toBeDisabled ( ) ) ;
141- expect ( renderResult ! . asFragment ( ) ) . toMatchSnapshot ( ) ;
132+ expect ( screen . getByText ( 'Experiment name is required' ) ) . toBeInTheDocument ( ) ;
142133 } ) ;
143134
144135 it ( 'updates the experiment name' , async ( ) => {
145136 await renderNewExperiment ( ) ;
146137 fillExperimentName ( 'experiment name' ) ;
147138
148- await waitFor ( ( ) => {
149- expect ( getInstance ( ) . state ) . toEqual ( {
150- description : '' ,
151- experimentName : 'experiment name' ,
152- isbeingCreated : false ,
153- validationError : '' ,
154- } ) ;
155- } ) ;
139+ await waitFor ( ( ) =>
140+ expect ( screen . getByLabelText ( / E x p e r i m e n t n a m e / i) ) . toHaveValue ( 'experiment name' ) ,
141+ ) ;
142+ expect ( getNextButton ( ) ) . not . toBeDisabled ( ) ;
156143 } ) ;
157144
158145 it ( 'updates the experiment description' , async ( ) => {
159146 await renderNewExperiment ( ) ;
160147 fillDescription ( 'a description!' ) ;
161148
162- await waitFor ( ( ) => {
163- expect ( getInstance ( ) . state ) . toEqual ( {
164- description : 'a description!' ,
165- experimentName : '' ,
166- isbeingCreated : false ,
167- validationError : 'Experiment name is required' ,
168- } ) ;
169- } ) ;
149+ await waitFor ( ( ) =>
150+ expect ( screen . getByLabelText ( / D e s c r i p t i o n / i) ) . toHaveValue ( 'a description!' ) ,
151+ ) ;
152+ expect ( screen . getByText ( 'Experiment name is required' ) ) . toBeInTheDocument ( ) ;
170153 } ) ;
171154
172155 it ( "sets the page to a busy state upon clicking 'Next'" , async ( ) => {
@@ -175,10 +158,9 @@ describe('NewExperiment', () => {
175158 fillExperimentName ( 'experiment-name' ) ;
176159 await waitFor ( ( ) => expect ( getNextButton ( ) ) . not . toBeDisabled ( ) ) ;
177160
178- fireEvent . click ( getNextButton ( ) ) ;
161+ await userEvent . click ( getNextButton ( ) ) ;
179162 await TestUtils . flushPromises ( ) ;
180163
181- expect ( getInstance ( ) . state ) . toHaveProperty ( 'isbeingCreated' , true ) ;
182164 expect ( getNextButton ( ) ) . toBeDisabled ( ) ;
183165 } ) ;
184166
@@ -188,7 +170,7 @@ describe('NewExperiment', () => {
188170 fillExperimentName ( 'experiment name' ) ;
189171 fillDescription ( 'experiment description' ) ;
190172
191- fireEvent . click ( getNextButton ( ) ) ;
173+ await userEvent . click ( getNextButton ( ) ) ;
192174 await TestUtils . flushPromises ( ) ;
193175
194176 expect ( createExperimentSpy ) . toHaveBeenCalledWith (
@@ -203,7 +185,7 @@ describe('NewExperiment', () => {
203185 await renderNewExperiment ( { namespace : 'test-ns' } ) ;
204186
205187 fillExperimentName ( 'a-random-experiment-name-DO-NOT-VERIFY-THIS' ) ;
206- fireEvent . click ( getNextButton ( ) ) ;
188+ await userEvent . click ( getNextButton ( ) ) ;
207189 await TestUtils . flushPromises ( ) ;
208190
209191 expect ( createExperimentSpy ) . toHaveBeenCalledWith (
@@ -219,7 +201,7 @@ describe('NewExperiment', () => {
219201 await renderNewExperiment ( ) ;
220202
221203 fillExperimentName ( 'experiment-name' ) ;
222- fireEvent . click ( getNextButton ( ) ) ;
204+ await userEvent . click ( getNextButton ( ) ) ;
223205 await TestUtils . flushPromises ( ) ;
224206
225207 expect ( historyPushSpy ) . toHaveBeenCalledWith (
@@ -241,10 +223,10 @@ describe('NewExperiment', () => {
241223 props . location . search = `?${ QUERY_PARAMS . pipelineId } =${ pipelineId } ` ;
242224 await renderNewExperiment ( props as any ) ;
243225
244- await waitFor ( ( ) => expect ( getInstance ( ) . state . pipelineId ) . toBe ( pipelineId ) ) ;
226+ await TestUtils . flushPromises ( ) ;
245227
246228 fillExperimentName ( 'experiment-name' ) ;
247- fireEvent . click ( getNextButton ( ) ) ;
229+ await userEvent . click ( getNextButton ( ) ) ;
248230 await TestUtils . flushPromises ( ) ;
249231
250232 expect ( historyPushSpy ) . toHaveBeenCalledWith (
@@ -260,7 +242,7 @@ describe('NewExperiment', () => {
260242 await renderNewExperiment ( ) ;
261243
262244 fillExperimentName ( 'experiment-name' ) ;
263- fireEvent . click ( getNextButton ( ) ) ;
245+ await userEvent . click ( getNextButton ( ) ) ;
264246 await TestUtils . flushPromises ( ) ;
265247
266248 expect ( updateSnackbarSpy ) . toHaveBeenLastCalledWith ( {
@@ -277,10 +259,10 @@ describe('NewExperiment', () => {
277259 fillExperimentName ( 'experiment-name' ) ;
278260
279261 TestUtils . makeErrorResponseOnce ( createExperimentSpy as any , 'test error!' ) ;
280- fireEvent . click ( getNextButton ( ) ) ;
262+ await userEvent . click ( getNextButton ( ) ) ;
281263 await TestUtils . flushPromises ( ) ;
282264
283- await waitFor ( ( ) => expect ( getInstance ( ) . state ) . toHaveProperty ( 'isbeingCreated' , false ) ) ;
265+ await waitFor ( ( ) => expect ( getNextButton ( ) ) . not . toBeDisabled ( ) ) ;
284266 expect ( loggerErrorSpy ) . toHaveBeenCalled ( ) ;
285267 } ) ;
286268
@@ -291,7 +273,7 @@ describe('NewExperiment', () => {
291273 fillExperimentName ( 'experiment-name' ) ;
292274
293275 TestUtils . makeErrorResponseOnce ( createExperimentSpy as any , 'test error!' ) ;
294- fireEvent . click ( getNextButton ( ) ) ;
276+ await userEvent . click ( getNextButton ( ) ) ;
295277 await TestUtils . flushPromises ( ) ;
296278
297279 const call = updateDialogSpy . mock . calls [ 0 ] [ 0 ] ;
@@ -302,7 +284,7 @@ describe('NewExperiment', () => {
302284
303285 it ( 'navigates to experiment list page upon cancellation' , async ( ) => {
304286 await renderNewExperiment ( ) ;
305- fireEvent . click ( getCancelButton ( ) ) ;
287+ await userEvent . click ( getCancelButton ( ) ) ;
306288 await TestUtils . flushPromises ( ) ;
307289
308290 expect ( historyPushSpy ) . toHaveBeenCalledWith ( RoutePage . EXPERIMENTS ) ;
0 commit comments