Skip to content

Commit dd7cb74

Browse files
committed
feat: fixed polling in useBackgroundJobStatus hook
1 parent abcdeea commit dd7cb74

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/hooks/useBackgroundJobStatus.test.tsx

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ describe('useBackgroundJobStatus', () => {
106106
})
107107

108108
describe('when job is failed', () => {
109-
it('does not fetch job status', () => {
109+
it('does not fetch job status when completedAt is present', () => {
110110
const job = {
111111
id: 'job-4',
112112
status: 'failed' as const,
113113
createdAt: getTimestamp(),
114+
completedAt: getTimestamp(),
114115
}
115116

116117
const { result } = renderHook(() => useBackgroundJobStatus(job), {
@@ -121,6 +122,37 @@ describe('useBackgroundJobStatus', () => {
121122
expect(result.current.data).toBeUndefined()
122123
expect(mockFetch).not.toHaveBeenCalled()
123124
})
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+
})
124156
})
125157

126158
describe('when job is undefined', () => {
@@ -154,7 +186,7 @@ describe('useBackgroundJobStatus', () => {
154186

155187
it('handles HTTP 404 errors without retrying', async () => {
156188
const job = {
157-
id: 'job-5',
189+
id: 'job-6',
158190
status: 'running' as const,
159191
createdAt: getTimestamp(),
160192
}
@@ -180,7 +212,7 @@ describe('useBackgroundJobStatus', () => {
180212

181213
it('calls fetch with correct parameters for error scenarios', async () => {
182214
const job = {
183-
id: 'job-6',
215+
id: 'job-7',
184216
status: 'running' as const,
185217
createdAt: getTimestamp(),
186218
}
@@ -195,14 +227,14 @@ describe('useBackgroundJobStatus', () => {
195227
expect(mockFetch).toHaveBeenCalledWith('/api/background-jobs', {
196228
method: 'POST',
197229
headers: { 'Content-Type': 'application/json' },
198-
body: JSON.stringify({ id: 'job-6' }),
230+
body: JSON.stringify({ id: 'job-7' }),
199231
})
200232
})
201233
})
202234

203235
it('calls fetch for HTTP error responses', async () => {
204236
const job = {
205-
id: 'job-7',
237+
id: 'job-8',
206238
status: 'running' as const,
207239
createdAt: getTimestamp(),
208240
}
@@ -220,7 +252,7 @@ describe('useBackgroundJobStatus', () => {
220252
expect(mockFetch).toHaveBeenCalledWith('/api/background-jobs', {
221253
method: 'POST',
222254
headers: { 'Content-Type': 'application/json' },
223-
body: JSON.stringify({ id: 'job-7' }),
255+
body: JSON.stringify({ id: 'job-8' }),
224256
})
225257
})
226258
})

src/hooks/useBackgroundJobStatus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const useBackgroundJobStatus = (
4949
}
5050
return fetchJobStatus(job.id)
5151
},
52-
enabled: !!job?.id && job.status === 'running',
52+
enabled: !!job?.id && (job.status === 'running' || (job.status === 'failed' && !job.completedAt)),
5353
refetchInterval,
5454
refetchIntervalInBackground: true,
5555
retry: (failureCount, error) => {

0 commit comments

Comments
 (0)