|
1 | 1 | /* eslint-disable @typescript-eslint/dot-notation */ |
2 | 2 | import { ComponentType, type ComponentDef } from '@defra/forms-model' |
| 3 | +import Boom from '@hapi/boom' |
3 | 4 | import { type ValidationErrorItem, type ValidationResult } from 'joi' |
4 | 5 |
|
5 | 6 | import { |
@@ -195,6 +196,89 @@ describe('FileUploadPageController', () => { |
195 | 196 | ) |
196 | 197 | }) |
197 | 198 |
|
| 199 | + it('initiates new upload when getUploadStatus throws a 404 error', async () => { |
| 200 | + const state = { |
| 201 | + upload: { |
| 202 | + [controller.path]: { |
| 203 | + upload: { |
| 204 | + uploadId: 'some-id', |
| 205 | + uploadUrl: 'some-url', |
| 206 | + statusUrl: 'some-status-url' |
| 207 | + }, |
| 208 | + files: [] |
| 209 | + } |
| 210 | + } |
| 211 | + } as unknown as FormSubmissionState |
| 212 | + |
| 213 | + const notFoundError = Boom.notFound('Upload not found') |
| 214 | + |
| 215 | + jest |
| 216 | + .spyOn(uploadService, 'getUploadStatus') |
| 217 | + .mockRejectedValue(notFoundError) |
| 218 | + |
| 219 | + const testController = controller as TestableFileUploadPageController |
| 220 | + const initiateSpy = jest.spyOn( |
| 221 | + testController, |
| 222 | + 'initiateAndStoreNewUpload' |
| 223 | + ) |
| 224 | + initiateSpy.mockResolvedValue(state as never) |
| 225 | + |
| 226 | + const result = await controller['checkUploadStatus'](request, state, 1) |
| 227 | + |
| 228 | + expect(initiateSpy).toHaveBeenCalledWith(request, state) |
| 229 | + expect(result).toBe(state) |
| 230 | + }) |
| 231 | + |
| 232 | + it('re-throws non-404 Boom errors from getUploadStatus', async () => { |
| 233 | + const state = { |
| 234 | + upload: { |
| 235 | + [controller.path]: { |
| 236 | + upload: { |
| 237 | + uploadId: 'some-id', |
| 238 | + uploadUrl: 'some-url', |
| 239 | + statusUrl: 'some-status-url' |
| 240 | + }, |
| 241 | + files: [] |
| 242 | + } |
| 243 | + } |
| 244 | + } as unknown as FormSubmissionState |
| 245 | + |
| 246 | + const serverError = Boom.internal('Server error') |
| 247 | + |
| 248 | + jest |
| 249 | + .spyOn(uploadService, 'getUploadStatus') |
| 250 | + .mockRejectedValue(serverError) |
| 251 | + |
| 252 | + await expect( |
| 253 | + controller['checkUploadStatus'](request, state, 1) |
| 254 | + ).rejects.toThrow('Server error') |
| 255 | + }) |
| 256 | + |
| 257 | + it('re-throws non-Boom errors from getUploadStatus', async () => { |
| 258 | + const state = { |
| 259 | + upload: { |
| 260 | + [controller.path]: { |
| 261 | + upload: { |
| 262 | + uploadId: 'some-id', |
| 263 | + uploadUrl: 'some-url', |
| 264 | + statusUrl: 'some-status-url' |
| 265 | + }, |
| 266 | + files: [] |
| 267 | + } |
| 268 | + } |
| 269 | + } as unknown as FormSubmissionState |
| 270 | + |
| 271 | + const networkError = new Error('Network failure') |
| 272 | + |
| 273 | + jest |
| 274 | + .spyOn(uploadService, 'getUploadStatus') |
| 275 | + .mockRejectedValue(networkError) |
| 276 | + |
| 277 | + await expect( |
| 278 | + controller['checkUploadStatus'](request, state, 1) |
| 279 | + ).rejects.toThrow('Network failure') |
| 280 | + }) |
| 281 | + |
198 | 282 | it('handles pending upload with backoff and retries', async () => { |
199 | 283 | const state = { |
200 | 284 | upload: { |
|
0 commit comments