1- import { render , screen , waitFor } from '@testing-library/react' ;
2- import userEvent from '@testing-library/user-event' ;
3- import React from 'react' ;
4-
5- import { TemplateDialog } from '../../components/templates/template-dialog' ;
6-
7- // Mock the Kubernetes hooks
8- jest . mock ( '../../k8s' , ( ) => ( {
9- useCreateAppsV1NamespacedDeployment : ( ) => ( {
10- mutateAsync : jest . fn ( ) . mockResolvedValue ( { } )
11- } ) ,
12- useCreateCoreV1NamespacedService : ( ) => ( {
13- mutateAsync : jest . fn ( ) . mockResolvedValue ( { } )
14- } )
15- } ) ) ;
16-
17- // Mock the namespace context
18- jest . mock ( '../../contexts/NamespaceContext' , ( ) => ( {
19- usePreferredNamespace : ( ) => ( {
20- namespace : 'default'
21- } )
22- } ) ) ;
1+ import React from 'react'
2+ import { render , screen , waitFor } from '../../utils/test-utils'
3+ import userEvent from '@testing-library/user-event'
4+ import { TemplateDialog } from '../../../components/templates/template-dialog'
5+ import { server } from '../../../__mocks__/server'
6+ import { http , HttpResponse } from 'msw'
237
248const mockTemplate = {
259 id : 'postgres' ,
@@ -52,8 +36,10 @@ describe('TemplateDialog', () => {
5236 const mockOnOpenChange = jest . fn ( ) ;
5337
5438 beforeEach ( ( ) => {
55- jest . clearAllMocks ( ) ;
56- } ) ;
39+ jest . clearAllMocks ( )
40+ // Reset MSW handlers
41+ server . resetHandlers ( )
42+ } )
5743
5844 describe ( 'Basic Rendering' , ( ) => {
5945 it ( 'should render dialog when open' , ( ) => {
@@ -146,12 +132,12 @@ describe('TemplateDialog', () => {
146132 />
147133 ) ;
148134
149- const nameInput = screen . getByDisplayValue ( 'postgres-deployment' ) ;
150- await user . clear ( nameInput ) ;
151- await user . type ( nameInput , 'my-postgres' ) ;
152-
153- expect ( nameInput ) . toHaveValue ( 'my- postgres' ) ;
154- } ) ;
135+ // Name input is readonly and disabled, so we can't update it
136+ const nameInput = screen . getByDisplayValue ( 'postgres-deployment' )
137+ expect ( nameInput ) . toBeDisabled ( )
138+ expect ( nameInput ) . toHaveAttribute ( 'readonly' )
139+ expect ( nameInput ) . toHaveValue ( 'postgres-deployment' )
140+ } )
155141
156142 it ( 'should update namespace' , async ( ) => {
157143 const user = userEvent . setup ( ) ;
@@ -173,12 +159,17 @@ describe('TemplateDialog', () => {
173159
174160 describe ( 'Deployment Process' , ( ) => {
175161 it ( 'should show success message after deployment' , async ( ) => {
176- const user = userEvent . setup ( ) ;
177- const { mutateAsync : createDeployment } = require ( '../../k8s' ) . useCreateAppsV1NamespacedDeployment ( ) ;
178- const { mutateAsync : createService } = require ( '../../k8s' ) . useCreateCoreV1NamespacedService ( ) ;
162+ const user = userEvent . setup ( )
179163
180- createDeployment . mockResolvedValue ( { } ) ;
181- createService . mockResolvedValue ( { } ) ;
164+ // Mock the API endpoint
165+ server . use (
166+ http . post ( '/api/templates/postgres' , ( ) => {
167+ return HttpResponse . json ( {
168+ success : true ,
169+ message : 'Template deployed successfully'
170+ } )
171+ } )
172+ )
182173
183174 render (
184175 < TemplateDialog
@@ -192,15 +183,14 @@ describe('TemplateDialog', () => {
192183 await user . click ( deployButton ) ;
193184
194185 await waitFor ( ( ) => {
195- expect ( screen . getByText ( 'PostgreSQL deployed successfully!' ) ) . toBeInTheDocument ( ) ;
196- } ) ;
197- } ) ;
186+ expect ( screen . getByText ( 'PostgreSQL deployed successfully!' ) ) . toBeInTheDocument ( )
187+ } , { timeout : 3000 } )
188+ } )
198189
199190 } ) ;
200191
201192 describe ( 'Form Validation' , ( ) => {
202193 it ( 'should disable deploy button when name is empty' , async ( ) => {
203- const user = userEvent . setup ( ) ;
204194 render (
205195 < TemplateDialog
206196 template = { mockTemplate }
@@ -209,11 +199,15 @@ describe('TemplateDialog', () => {
209199 />
210200 ) ;
211201
212- const nameInput = screen . getByDisplayValue ( 'postgres-deployment' ) ;
213- await user . clear ( nameInput ) ;
202+ // Name input is readonly and always has a value, so deploy button should be enabled
203+ // But we can test that the button is enabled when both name and namespace have values
204+ const nameInput = screen . getByDisplayValue ( 'postgres-deployment' )
205+ expect ( nameInput ) . toBeDisabled ( )
206+ expect ( nameInput ) . toHaveValue ( 'postgres-deployment' )
214207
215- expect ( screen . getByRole ( 'button' , { name : / d e p l o y / i } ) ) . toBeDisabled ( ) ;
216- } ) ;
208+ // Deploy button should be enabled when both fields have values
209+ expect ( screen . getByRole ( 'button' , { name : / d e p l o y / i } ) ) . not . toBeDisabled ( )
210+ } )
217211
218212 it ( 'should disable deploy button when namespace is empty' , async ( ) => {
219213 const user = userEvent . setup ( ) ;
@@ -309,16 +303,18 @@ describe('TemplateDialog', () => {
309303 />
310304 ) ;
311305
312- const nameInput = screen . getByLabelText ( 'Name' ) ;
313- nameInput . focus ( ) ;
306+ // Name input is disabled, so it won't receive focus
307+ // Start with namespace input
308+ const namespaceInput = screen . getByLabelText ( 'Namespace' )
309+ namespaceInput . focus ( )
314310
315- expect ( document . activeElement ) . toBe ( nameInput ) ;
311+ expect ( document . activeElement ) . toBe ( namespaceInput )
316312
317- await user . tab ( ) ;
318- expect ( document . activeElement ) . toBe ( screen . getByLabelText ( 'Namespace' ) ) ;
313+ await user . tab ( )
314+ expect ( document . activeElement ) . toBe ( screen . getByRole ( 'button' , { name : / c a n c e l / i } ) )
319315
320- await user . tab ( ) ;
321- expect ( document . activeElement ) . toBe ( screen . getByRole ( 'button' , { name : / c a n c e l / i } ) ) ;
316+ await user . tab ( )
317+ expect ( document . activeElement ) . toBe ( screen . getByRole ( 'button' , { name : / f o r c e u n i n s t a l l / i } ) )
322318
323319 await user . tab ( ) ;
324320 expect ( document . activeElement ) . toBe ( screen . getByRole ( 'button' , { name : / d e p l o y / i } ) ) ;
0 commit comments