120 lines
3.5 KiB
TypeScript
120 lines
3.5 KiB
TypeScript
/**
|
|
* hooks/useUploadToObjectStore.ts — presigned-URL upload.
|
|
* Asserts common.generateUploadUrls and the direct PUT to each presigned URL.
|
|
*
|
|
* httpBatchLink flushes on a timer, so each act() advances the fake clock.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { act, renderHook, waitFor } from '@testing-library/react-native'
|
|
|
|
import { useUploadToObjectStorage } from '@/hooks/useUploadToObjectStore'
|
|
import { resetStorage, setupApiMocks } from '../mocks/api'
|
|
import { lastProcedureInput } from '../mocks/procedures'
|
|
import { Providers } from '../helpers/render'
|
|
|
|
const wrapper = ({ children }: { children?: React.ReactNode }) => (
|
|
<Providers centralStores={false}>{children}</Providers>
|
|
)
|
|
|
|
type UploadResultLike = { keys: string[]; presignedUrls: string[] }
|
|
|
|
/** Run the upload; waitFor drives the tRPC batch timer to flush. */
|
|
const runUpload = async (
|
|
result: { current: ReturnType<typeof useUploadToObjectStorage> },
|
|
input: Parameters<ReturnType<typeof useUploadToObjectStorage>['upload']>[0]
|
|
) => {
|
|
let value: UploadResultLike | undefined
|
|
let failure: unknown
|
|
|
|
act(() => {
|
|
result.current.upload(input).then(
|
|
(settled) => {
|
|
value = settled as UploadResultLike
|
|
},
|
|
(error) => {
|
|
failure = error
|
|
}
|
|
)
|
|
})
|
|
|
|
await waitFor(() => {
|
|
if (!value && !failure) throw new Error('upload still pending')
|
|
})
|
|
|
|
return { value, failure }
|
|
}
|
|
|
|
describe('useUploadToObjectStorage', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
})
|
|
|
|
it('requests presigned urls and PUTs every image', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: {
|
|
'common.generateUploadUrls': {
|
|
uploadUrls: [
|
|
'https://bucket.test/complaint/k1',
|
|
'https://bucket.test/complaint/k2',
|
|
],
|
|
},
|
|
},
|
|
})
|
|
const { result } = renderHook(() => useUploadToObjectStorage(), { wrapper })
|
|
|
|
const { value } = await runUpload(result, {
|
|
images: [
|
|
{ blob: {} as Blob, mimeType: 'image/jpeg' },
|
|
{ blob: {} as Blob, mimeType: 'image/png' },
|
|
],
|
|
contextString: 'complaint',
|
|
})
|
|
|
|
expect(value).toEqual({
|
|
keys: ['k1', 'k2'],
|
|
presignedUrls: [
|
|
'https://bucket.test/complaint/k1',
|
|
'https://bucket.test/complaint/k2',
|
|
],
|
|
})
|
|
expect(lastProcedureInput(api.fetchMock, 'common.generateUploadUrls')).toEqual({
|
|
contextString: 'complaint',
|
|
mimeTypes: ['image/jpeg', 'image/png'],
|
|
})
|
|
|
|
const puts = api.fetchMock.mock.calls.filter(
|
|
([, options]) => (options as { method?: string })?.method === 'PUT'
|
|
)
|
|
expect(puts).toHaveLength(2)
|
|
})
|
|
|
|
it('short-circuits when there are no images', async () => {
|
|
const api = setupApiMocks()
|
|
const { result } = renderHook(() => useUploadToObjectStorage(), { wrapper })
|
|
|
|
const { value } = await runUpload(result, {
|
|
images: [],
|
|
contextString: 'complaint',
|
|
})
|
|
|
|
expect(value).toEqual({ keys: [], presignedUrls: [] })
|
|
expect(
|
|
api.fetchMock.mock.calls.some(([u]) => String(u).includes('generateUploadUrls'))
|
|
).toBe(false)
|
|
})
|
|
|
|
it('fails when the URL count does not match the image count', async () => {
|
|
setupApiMocks({
|
|
procedures: { 'common.generateUploadUrls': { uploadUrls: [] } },
|
|
})
|
|
const { result } = renderHook(() => useUploadToObjectStorage(), { wrapper })
|
|
|
|
const { failure } = await runUpload(result, {
|
|
images: [{ blob: {} as Blob, mimeType: 'image/jpeg' }],
|
|
contextString: 'complaint',
|
|
})
|
|
|
|
expect((failure as Error)?.message).toMatch(/Expected 1 URLs, got 0/)
|
|
})
|
|
})
|