1
- import { HttpResponse } from 'msw' ;
2
- import { ZodError } from 'zod' ;
3
-
4
- import { render , screen , userEvent , within } from '@/test-utils/rtl' ;
1
+ import { render , screen , userEvent } from '@/test-utils/rtl' ;
5
2
6
3
import ErrorBoundary from '@/components/error-boundary/error-boundary' ;
7
4
import { mockWorkflowDiagnosticsResult } from '@/route-handlers/diagnose-workflow/__fixtures__/mock-workflow-diagnostics-result' ;
8
- import { type DiagnoseWorkflowResponse } from '@/route-handlers/diagnose-workflow/diagnose-workflow.types' ;
5
+ import { type WorkflowDiagnosticsResult } from '@/route-handlers/diagnose-workflow/diagnose-workflow.types' ;
9
6
10
7
import WorkflowDiagnosticsContent from '../workflow-diagnostics-content' ;
11
8
12
- jest . mock (
13
- '@/components/section-loading-indicator/section-loading-indicator' ,
14
- ( ) => jest . fn ( ( ) => < div data-testid = "loading-indicator" > Loading...</ div > )
15
- ) ;
16
-
17
9
jest . mock ( '../../workflow-diagnostics-list/workflow-diagnostics-list' , ( ) =>
18
10
jest . fn ( ( ) => < div > Diagnostics List Component</ div > )
19
11
) ;
@@ -22,27 +14,26 @@ jest.mock('../../workflow-diagnostics-json/workflow-diagnostics-json', () =>
22
14
jest . fn ( ( ) => < div > Diagnostics JSON Component</ div > )
23
15
) ;
24
16
25
- describe ( 'WorkflowDiagnosticsContent' , ( ) => {
17
+ jest . mock (
18
+ '../../workflow-diagnostics-view-toggle/workflow-diagnostics-view-toggle' ,
19
+ ( ) =>
20
+ jest . fn ( ( { listEnabled, activeView, setActiveView } ) => (
21
+ < div data-testid = "view-toggle" >
22
+ < div > Is list view enabled: { listEnabled . toString ( ) } </ div >
23
+ < div > Active View: { activeView } </ div >
24
+ < button onClick = { ( ) => setActiveView ( 'list' ) } > Switch to List</ button >
25
+ < button onClick = { ( ) => setActiveView ( 'json' ) } > Switch to JSON</ button >
26
+ </ div >
27
+ ) )
28
+ ) ;
29
+
30
+ describe ( WorkflowDiagnosticsContent . name , ( ) => {
26
31
beforeEach ( ( ) => {
27
32
jest . clearAllMocks ( ) ;
28
33
} ) ;
29
34
30
- it ( 'should render loading indicator when status is pending' , async ( ) => {
31
- setup ( { responseType : 'pending' } ) ;
32
-
33
- expect ( await screen . findByTestId ( 'loading-indicator' ) ) . toBeInTheDocument ( ) ;
34
- } ) ;
35
-
36
- it ( 'should throw error when status is error' , async ( ) => {
37
- setup ( { responseType : 'error' } ) ;
38
-
39
- expect (
40
- await screen . findByText ( 'Failed to fetch diagnostics' )
41
- ) . toBeInTheDocument ( ) ;
42
- } ) ;
43
-
44
35
it ( 'should render diagnostics list view by default' , async ( ) => {
45
- setup ( { responseType : 'success' } ) ;
36
+ setup ( { diagnosticsResult : mockWorkflowDiagnosticsResult } ) ;
46
37
47
38
expect (
48
39
await screen . findByText ( 'Diagnostics List Component' )
@@ -53,7 +44,9 @@ describe('WorkflowDiagnosticsContent', () => {
53
44
} ) ;
54
45
55
46
it ( 'should allow switching between list and JSON views' , async ( ) => {
56
- const { user } = setup ( { responseType : 'success' } ) ;
47
+ const { user } = setup ( {
48
+ diagnosticsResult : mockWorkflowDiagnosticsResult ,
49
+ } ) ;
57
50
58
51
// Initially shows list view
59
52
expect (
@@ -64,8 +57,7 @@ describe('WorkflowDiagnosticsContent', () => {
64
57
) . not . toBeInTheDocument ( ) ;
65
58
66
59
// Switch to JSON view
67
- const listbox = screen . getByRole ( 'listbox' ) ;
68
- const jsonButton = within ( listbox ) . getByRole ( 'option' , { name : / j s o n / i } ) ;
60
+ const jsonButton = screen . getByText ( 'Switch to JSON' ) ;
69
61
await user . click ( jsonButton ) ;
70
62
71
63
expect ( screen . getByText ( 'Diagnostics JSON Component' ) ) . toBeInTheDocument ( ) ;
@@ -74,7 +66,7 @@ describe('WorkflowDiagnosticsContent', () => {
74
66
) . not . toBeInTheDocument ( ) ;
75
67
76
68
// Switch back to list view
77
- const listButton = within ( listbox ) . getByRole ( 'option' , { name : / l i s t / i } ) ;
69
+ const listButton = screen . getByText ( 'Switch to List' ) ;
78
70
await user . click ( listButton ) ;
79
71
80
72
expect ( screen . getByText ( 'Diagnostics List Component' ) ) . toBeInTheDocument ( ) ;
@@ -83,32 +75,20 @@ describe('WorkflowDiagnosticsContent', () => {
83
75
) . not . toBeInTheDocument ( ) ;
84
76
} ) ;
85
77
86
- it ( 'should switch to JSON view when parsing error exists' , async ( ) => {
87
- setup ( { responseType : 'parsing-error' } ) ;
88
-
89
- expect (
90
- await screen . findByText ( 'Diagnostics JSON Component' )
91
- ) . toBeInTheDocument ( ) ;
92
- expect (
93
- screen . queryByText ( 'Diagnostics List Component' )
94
- ) . not . toBeInTheDocument ( ) ;
95
- } ) ;
96
-
97
- it ( 'should disable list view when parsing error exists' , async ( ) => {
98
- const { debug } = setup ( { responseType : 'parsing-error' } ) ;
78
+ it ( 'should render view toggle with correct props' , ( ) => {
79
+ setup ( { diagnosticsResult : mockWorkflowDiagnosticsResult } ) ;
99
80
100
- const listbox = await screen . findByRole ( 'listbox' ) ;
101
- const listButton = within ( listbox ) . getByRole ( 'option' , { name : / l i s t / i } ) ;
102
- debug ( ) ;
103
- expect ( listButton ) . toBeDisabled ( ) ;
81
+ expect ( screen . getByTestId ( 'view-toggle' ) ) . toBeInTheDocument ( ) ;
82
+ expect ( screen . getByText ( 'Is list view enabled: true' ) ) . toBeInTheDocument ( ) ;
83
+ expect ( screen . getByText ( 'Active View: list' ) ) . toBeInTheDocument ( ) ;
104
84
} ) ;
105
85
} ) ;
106
86
107
87
function setup ( {
108
- responseType ,
88
+ diagnosticsResult ,
109
89
} : {
110
- responseType ?: 'success' | 'error' | 'parsing-error' | 'pending' ;
111
- } = { } ) {
90
+ diagnosticsResult : WorkflowDiagnosticsResult ;
91
+ } ) {
112
92
const user = userEvent . setup ( ) ;
113
93
114
94
const renderResult = render (
@@ -118,39 +98,9 @@ function setup({
118
98
cluster = "test-cluster"
119
99
workflowId = "test-workflow-id"
120
100
runId = "test-run-id"
101
+ diagnosticsResult = { diagnosticsResult }
121
102
/>
122
- </ ErrorBoundary > ,
123
- {
124
- endpointsMocks : [
125
- {
126
- path : '/api/domains/:domain/:cluster/workflows/:workflowId/:runId/diagnose' ,
127
- httpMethod : 'GET' ,
128
- mockOnce : false ,
129
- httpResolver : async ( ) => {
130
- switch ( responseType ) {
131
- case 'error' :
132
- return HttpResponse . json (
133
- { message : 'Failed to fetch diagnostics' } ,
134
- { status : 500 }
135
- ) ;
136
- case 'parsing-error' :
137
- return HttpResponse . json ( {
138
- result : { raw : 'invalid data' } ,
139
- parsingError : new ZodError ( [ ] ) ,
140
- } satisfies DiagnoseWorkflowResponse ) ;
141
- case 'pending' :
142
- return new Promise ( ( ) => { } ) ; // Never resolves to simulate pending
143
- case 'success' :
144
- default :
145
- return HttpResponse . json ( {
146
- result : mockWorkflowDiagnosticsResult ,
147
- parsingError : null ,
148
- } satisfies DiagnoseWorkflowResponse ) ;
149
- }
150
- } ,
151
- } ,
152
- ] ,
153
- }
103
+ </ ErrorBoundary >
154
104
) ;
155
105
156
106
return { user, ...renderResult } ;
0 commit comments