@@ -3,15 +3,27 @@ import { Suspense } from 'react';
3
3
import { HttpResponse } from 'msw' ;
4
4
import { VirtuosoMockContext } from 'react-virtuoso' ;
5
5
6
- import { act , render , screen , userEvent } from '@/test-utils/rtl' ;
6
+ import {
7
+ act ,
8
+ render ,
9
+ screen ,
10
+ userEvent ,
11
+ waitForElementToBeRemoved ,
12
+ } from '@/test-utils/rtl' ;
7
13
14
+ import * as usePageFiltersModule from '@/components/page-filters/hooks/use-page-filters' ;
8
15
import { type Props as PageFiltersToggleProps } from '@/components/page-filters/page-filters-toggle/page-filters-toggle.types' ;
9
16
import { type GetWorkflowHistoryResponse } from '@/route-handlers/get-workflow-history/get-workflow-history.types' ;
10
17
import { describeWorkflowResponse } from '@/views/workflow-page/__fixtures__/describe-workflow-response' ;
11
18
12
19
import { completedActivityTaskEvents } from '../__fixtures__/workflow-history-activity-events' ;
20
+ import { completedDecisionTaskEvents } from '../__fixtures__/workflow-history-decision-events' ;
13
21
import WorkflowHistory from '../workflow-history' ;
14
22
23
+ jest . mock ( '@/hooks/use-page-query-params/use-page-query-params' , ( ) =>
24
+ jest . fn ( ( ) => [ { historySelectedEventId : '1' } , jest . fn ( ) ] )
25
+ ) ;
26
+
15
27
jest . mock (
16
28
'../workflow-history-compact-event-card/workflow-history-compact-event-card' ,
17
29
( ) => jest . fn ( ( ) => < div > Compact group Card</ div > )
@@ -51,6 +63,11 @@ jest.mock('@/components/page-filters/hooks/use-page-filters', () =>
51
63
52
64
jest . mock ( '../config/workflow-history-filters.config' , ( ) => [ ] ) ;
53
65
66
+ jest . mock (
67
+ '@/components/section-loading-indicator/section-loading-indicator' ,
68
+ ( ) => jest . fn ( ( ) => < div > keep loading events</ div > )
69
+ ) ;
70
+
54
71
describe ( 'WorkflowHistory' , ( ) => {
55
72
it ( 'renders page correctly' , async ( ) => {
56
73
setup ( { } ) ;
@@ -74,7 +91,7 @@ describe('WorkflowHistory', () => {
74
91
75
92
it ( 'throws an error if the request fails' , async ( ) => {
76
93
try {
77
- await act ( ( ) => setup ( { error : true } ) ) ;
94
+ await act ( async ( ) => await setup ( { error : true } ) ) ;
78
95
} catch ( error ) {
79
96
expect ( ( error as Error ) ?. message ) . toBe (
80
97
'Failed to fetch workflow history'
@@ -84,7 +101,7 @@ describe('WorkflowHistory', () => {
84
101
85
102
it ( 'throws an error if the workflow summary request fails' , async ( ) => {
86
103
try {
87
- await act ( ( ) => setup ( { summaryError : true } ) ) ;
104
+ await act ( async ( ) => await setup ( { summaryError : true } ) ) ;
88
105
} catch ( error ) {
89
106
expect ( ( error as Error ) ?. message ) . toBe (
90
107
'Failed to fetch workflow summary'
@@ -98,7 +115,7 @@ describe('WorkflowHistory', () => {
98
115
} ) ;
99
116
100
117
it ( 'should hide filters on executing toggle button onClick' , async ( ) => {
101
- const { user } = setup ( { } ) ;
118
+ const { user } = await setup ( { } ) ;
102
119
const toggleButton = await screen . findByText ( 'Filter Toggle' ) ;
103
120
104
121
await user . click ( toggleButton ) ;
@@ -107,25 +124,83 @@ describe('WorkflowHistory', () => {
107
124
} ) ;
108
125
109
126
it ( 'should show timeline when the Timeline button is clicked' , async ( ) => {
110
- const { user } = setup ( { } ) ;
127
+ const { user } = await setup ( { } ) ;
111
128
const timelineButton = await screen . findByText ( 'Timeline' ) ;
112
129
113
130
await user . click ( timelineButton ) ;
114
131
115
132
expect ( screen . queryByText ( 'Timeline chart' ) ) . toBeInTheDocument ( ) ;
116
133
} ) ;
134
+
135
+ it ( 'should show loading while searching for initial selectedEventId' , async ( ) => {
136
+ const { getRequestResolver } = await setup ( {
137
+ resolveLoadMoreManually : true ,
138
+ pageQueryParamsValues : { historySelectedEventId : '3' } ,
139
+ hasNextPage : true ,
140
+ } ) ;
141
+
142
+ await act ( ( ) => {
143
+ const resolver = getRequestResolver ( ) ;
144
+ resolver ( {
145
+ history : {
146
+ events : [ completedDecisionTaskEvents [ 0 ] ] ,
147
+ } ,
148
+ archived : false ,
149
+ nextPageToken : 'mock-next-page-token' ,
150
+ rawHistory : [ ] ,
151
+ } ) ;
152
+ } ) ;
153
+
154
+ expect ( await screen . findByText ( 'keep loading events' ) ) . toBeInTheDocument ( ) ;
155
+
156
+ const secondPageResolver = getRequestResolver ( ) ;
157
+ secondPageResolver ( {
158
+ history : {
159
+ events : [ completedDecisionTaskEvents [ 1 ] ] ,
160
+ } ,
161
+ archived : false ,
162
+ nextPageToken : 'mock-next-page-token' ,
163
+ rawHistory : [ ] ,
164
+ } ) ;
165
+
166
+ expect (
167
+ await screen . findByText ( 'keep loading events' )
168
+ ) . not . toBeInTheDocument ( ) ;
169
+ } ) ;
117
170
} ) ;
118
171
119
- function setup ( {
172
+ async function setup ( {
120
173
error,
121
174
summaryError,
175
+ resolveLoadMoreManually,
176
+ pageQueryParamsValues = { } ,
177
+ hasNextPage,
122
178
} : {
123
179
error ?: boolean ;
124
180
summaryError ?: boolean ;
181
+ resolveLoadMoreManually ?: boolean ;
182
+ pageQueryParamsValues ?: Record < string , string > ;
183
+ hasNextPage ?: boolean ;
125
184
} ) {
126
185
const user = userEvent . setup ( ) ;
186
+
187
+ if ( pageQueryParamsValues ) {
188
+ jest . spyOn ( usePageFiltersModule , 'default' ) . mockReturnValue ( {
189
+ queryParams : pageQueryParamsValues ,
190
+ setQueryParams : jest . fn ( ) ,
191
+ activeFiltersCount : 0 ,
192
+ resetAllFilters : jest . fn ( ) ,
193
+ } ) ;
194
+ }
195
+
196
+ type ReqResolver = ( r : GetWorkflowHistoryResponse ) => void ;
197
+ let requestResolver : ReqResolver = ( ) => { } ;
198
+ let requestRejector = ( ) => { } ;
199
+ const getRequestResolver = ( ) => requestResolver ;
200
+ const getRequestRejector = ( ) => requestRejector ;
201
+ let requestIndex = - 1 ;
127
202
const renderResult = render (
128
- < Suspense >
203
+ < Suspense fallback = { 'Suspense placeholder' } >
129
204
< WorkflowHistory
130
205
params = { {
131
206
domain : 'test-domain' ,
@@ -141,25 +216,41 @@ function setup({
141
216
{
142
217
path : '/api/domains/:domain/:cluster/workflows/:workflowId/:runId/history' ,
143
218
httpMethod : 'GET' ,
144
- ...( error
145
- ? {
146
- httpResolver : ( ) => {
147
- return HttpResponse . json (
148
- { message : 'Failed to fetch workflow history' } ,
149
- { status : 500 }
219
+ mockOnce : false ,
220
+ httpResolver : async ( ) => {
221
+ requestIndex = requestIndex + 1 ;
222
+ if ( requestIndex > 0 && resolveLoadMoreManually ) {
223
+ await new Promise ( ( resolve , reject ) => {
224
+ requestResolver = ( result : GetWorkflowHistoryResponse ) =>
225
+ resolve ( HttpResponse . json ( result , { status : 200 } ) ) ;
226
+ requestRejector = ( ) =>
227
+ reject (
228
+ HttpResponse . json (
229
+ { message : 'Failed to fetch workflow history' } ,
230
+ { status : 500 }
231
+ )
150
232
) ;
151
- } ,
152
- }
153
- : {
154
- jsonResponse : {
233
+ } ) ;
234
+ } else {
235
+ if ( error )
236
+ return HttpResponse . json (
237
+ { message : 'Failed to fetch workflow history' } ,
238
+ { status : 500 }
239
+ ) ;
240
+
241
+ return HttpResponse . json (
242
+ {
155
243
history : {
156
244
events : completedActivityTaskEvents ,
157
245
} ,
158
246
archived : false ,
159
- nextPageToken : '' ,
247
+ nextPageToken : hasNextPage ? 'mock-next-page-token' : '' ,
160
248
rawHistory : [ ] ,
161
249
} satisfies GetWorkflowHistoryResponse ,
162
- } ) ,
250
+ { status : 200 }
251
+ ) ;
252
+ }
253
+ } ,
163
254
} ,
164
255
{
165
256
path : '/api/domains/:domain/:cluster/workflows/:workflowId/:runId' ,
@@ -182,13 +273,17 @@ function setup({
182
273
{
183
274
wrapper : ( { children } ) => (
184
275
< VirtuosoMockContext . Provider
185
- value = { { viewportHeight : 1000 , itemHeight : 100 } }
276
+ value = { { viewportHeight : 300 , itemHeight : 100 } }
186
277
>
187
278
{ children }
188
279
</ VirtuosoMockContext . Provider >
189
280
) ,
190
281
}
191
282
) ;
283
+ if ( ! error && ! summaryError )
284
+ await waitForElementToBeRemoved ( ( ) =>
285
+ screen . queryAllByText ( 'Suspense placeholder' )
286
+ ) ;
192
287
193
- return { user, ...renderResult } ;
288
+ return { user, getRequestResolver , getRequestRejector , ...renderResult } ;
194
289
}
0 commit comments