@@ -106,11 +106,12 @@ describe('useBackgroundJobStatus', () => {
106
106
} )
107
107
108
108
describe ( 'when job is failed' , ( ) => {
109
- it ( 'does not fetch job status' , ( ) => {
109
+ it ( 'does not fetch job status when completedAt is present ' , ( ) => {
110
110
const job = {
111
111
id : 'job-4' ,
112
112
status : 'failed' as const ,
113
113
createdAt : getTimestamp ( ) ,
114
+ completedAt : getTimestamp ( ) ,
114
115
}
115
116
116
117
const { result } = renderHook ( ( ) => useBackgroundJobStatus ( job ) , {
@@ -121,6 +122,37 @@ describe('useBackgroundJobStatus', () => {
121
122
expect ( result . current . data ) . toBeUndefined ( )
122
123
expect ( mockFetch ) . not . toHaveBeenCalled ( )
123
124
} )
125
+
126
+ it ( 'fetches job status when completedAt is missing' , async ( ) => {
127
+ const job = {
128
+ id : 'job-5' ,
129
+ status : 'failed' as const ,
130
+ createdAt : getTimestamp ( ) ,
131
+ }
132
+
133
+ mockFetch . mockResolvedValue ( {
134
+ ok : true ,
135
+ json : async ( ) => ( { status : 'failed' , error : 'Job failed' , completedAt : getTimestamp ( ) } ) ,
136
+ } )
137
+
138
+ const { result } = renderHook ( ( ) => useBackgroundJobStatus ( job ) , {
139
+ wrapper : createWrapper ( ) ,
140
+ } )
141
+
142
+ await waitFor ( ( ) => {
143
+ expect ( result . current . data ) . toEqual ( {
144
+ status : 'failed' ,
145
+ error : 'Job failed' ,
146
+ completedAt : expect . any ( String ) ,
147
+ } )
148
+ } )
149
+
150
+ expect ( mockFetch ) . toHaveBeenCalledWith ( '/api/background-jobs' , {
151
+ method : 'POST' ,
152
+ headers : { 'Content-Type' : 'application/json' } ,
153
+ body : JSON . stringify ( { id : 'job-5' } ) ,
154
+ } )
155
+ } )
124
156
} )
125
157
126
158
describe ( 'when job is undefined' , ( ) => {
@@ -154,7 +186,7 @@ describe('useBackgroundJobStatus', () => {
154
186
155
187
it ( 'handles HTTP 404 errors without retrying' , async ( ) => {
156
188
const job = {
157
- id : 'job-5 ' ,
189
+ id : 'job-6 ' ,
158
190
status : 'running' as const ,
159
191
createdAt : getTimestamp ( ) ,
160
192
}
@@ -180,7 +212,7 @@ describe('useBackgroundJobStatus', () => {
180
212
181
213
it ( 'calls fetch with correct parameters for error scenarios' , async ( ) => {
182
214
const job = {
183
- id : 'job-6 ' ,
215
+ id : 'job-7 ' ,
184
216
status : 'running' as const ,
185
217
createdAt : getTimestamp ( ) ,
186
218
}
@@ -195,14 +227,14 @@ describe('useBackgroundJobStatus', () => {
195
227
expect ( mockFetch ) . toHaveBeenCalledWith ( '/api/background-jobs' , {
196
228
method : 'POST' ,
197
229
headers : { 'Content-Type' : 'application/json' } ,
198
- body : JSON . stringify ( { id : 'job-6 ' } ) ,
230
+ body : JSON . stringify ( { id : 'job-7 ' } ) ,
199
231
} )
200
232
} )
201
233
} )
202
234
203
235
it ( 'calls fetch for HTTP error responses' , async ( ) => {
204
236
const job = {
205
- id : 'job-7 ' ,
237
+ id : 'job-8 ' ,
206
238
status : 'running' as const ,
207
239
createdAt : getTimestamp ( ) ,
208
240
}
@@ -220,7 +252,7 @@ describe('useBackgroundJobStatus', () => {
220
252
expect ( mockFetch ) . toHaveBeenCalledWith ( '/api/background-jobs' , {
221
253
method : 'POST' ,
222
254
headers : { 'Content-Type' : 'application/json' } ,
223
- body : JSON . stringify ( { id : 'job-7 ' } ) ,
255
+ body : JSON . stringify ( { id : 'job-8 ' } ) ,
224
256
} )
225
257
} )
226
258
} )
0 commit comments