2022-12-26 19:01:40 +01:00
|
|
|
import {downloadAndResize, DownloadAndResizeOpts} from '../../src/lib/images'
|
2022-12-22 16:32:39 +01:00
|
|
|
import ImageResizer from '@bam.tech/react-native-image-resizer'
|
|
|
|
import RNFetchBlob from 'rn-fetch-blob'
|
|
|
|
|
|
|
|
describe('downloadAndResize', () => {
|
|
|
|
const errorSpy = jest.spyOn(global.console, 'error')
|
|
|
|
|
|
|
|
const mockResizedImage = {
|
|
|
|
path: jest.fn().mockReturnValue('file://resized-image.jpg'),
|
2022-12-26 19:01:40 +01:00
|
|
|
size: 100,
|
2023-01-17 17:06:00 +01:00
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
mime: 'image/jpeg',
|
2022-12-22 16:32:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
const mockedCreateResizedImage =
|
|
|
|
ImageResizer.createResizedImage as jest.Mock
|
|
|
|
mockedCreateResizedImage.mockResolvedValue(mockResizedImage)
|
|
|
|
})
|
|
|
|
|
2023-01-17 17:06:00 +01:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
2022-12-22 16:32:39 +01:00
|
|
|
it('should return resized image for valid URI and options', async () => {
|
|
|
|
const mockedFetch = RNFetchBlob.fetch as jest.Mock
|
|
|
|
mockedFetch.mockResolvedValueOnce({
|
|
|
|
path: jest.fn().mockReturnValue('file://downloaded-image.jpg'),
|
|
|
|
flush: jest.fn(),
|
|
|
|
})
|
|
|
|
|
|
|
|
const opts: DownloadAndResizeOpts = {
|
|
|
|
uri: 'https://example.com/image.jpg',
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
2022-12-26 19:01:40 +01:00
|
|
|
maxSize: 500000,
|
2022-12-22 16:32:39 +01:00
|
|
|
mode: 'cover',
|
|
|
|
timeout: 10000,
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await downloadAndResize(opts)
|
|
|
|
expect(result).toEqual(mockResizedImage)
|
|
|
|
expect(RNFetchBlob.config).toHaveBeenCalledWith({
|
|
|
|
fileCache: true,
|
|
|
|
appendExt: 'jpeg',
|
|
|
|
})
|
|
|
|
expect(RNFetchBlob.fetch).toHaveBeenCalledWith(
|
|
|
|
'GET',
|
|
|
|
'https://example.com/image.jpg',
|
|
|
|
)
|
|
|
|
expect(ImageResizer.createResizedImage).toHaveBeenCalledWith(
|
|
|
|
'file://downloaded-image.jpg',
|
|
|
|
100,
|
|
|
|
100,
|
|
|
|
'JPEG',
|
2023-01-19 01:14:46 +01:00
|
|
|
100,
|
2022-12-22 16:32:39 +01:00
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
{mode: 'cover'},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return undefined for invalid URI', async () => {
|
|
|
|
const opts: DownloadAndResizeOpts = {
|
|
|
|
uri: 'invalid-uri',
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
2022-12-26 19:01:40 +01:00
|
|
|
maxSize: 500000,
|
2022-12-22 16:32:39 +01:00
|
|
|
mode: 'cover',
|
|
|
|
timeout: 10000,
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await downloadAndResize(opts)
|
|
|
|
expect(errorSpy).toHaveBeenCalled()
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return undefined for unsupported file type', async () => {
|
2023-02-22 21:23:57 +01:00
|
|
|
const mockedFetch = RNFetchBlob.fetch as jest.Mock
|
|
|
|
mockedFetch.mockResolvedValueOnce({
|
|
|
|
path: jest.fn().mockReturnValue('file://downloaded-image'),
|
|
|
|
flush: jest.fn(),
|
|
|
|
})
|
|
|
|
|
2022-12-22 16:32:39 +01:00
|
|
|
const opts: DownloadAndResizeOpts = {
|
2023-02-22 21:23:57 +01:00
|
|
|
uri: 'https://example.com/image',
|
2022-12-22 16:32:39 +01:00
|
|
|
width: 100,
|
|
|
|
height: 100,
|
2022-12-26 19:01:40 +01:00
|
|
|
maxSize: 500000,
|
2022-12-22 16:32:39 +01:00
|
|
|
mode: 'cover',
|
|
|
|
timeout: 10000,
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await downloadAndResize(opts)
|
2023-02-22 21:23:57 +01:00
|
|
|
expect(result).toEqual(mockResizedImage)
|
|
|
|
expect(RNFetchBlob.config).toHaveBeenCalledWith({
|
|
|
|
fileCache: true,
|
|
|
|
appendExt: 'jpeg',
|
|
|
|
})
|
|
|
|
expect(RNFetchBlob.fetch).toHaveBeenCalledWith(
|
|
|
|
'GET',
|
|
|
|
'https://example.com/image',
|
|
|
|
)
|
|
|
|
expect(ImageResizer.createResizedImage).toHaveBeenCalledWith(
|
|
|
|
'file://downloaded-image',
|
|
|
|
100,
|
|
|
|
100,
|
|
|
|
'JPEG',
|
|
|
|
100,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
{mode: 'cover'},
|
|
|
|
)
|
2022-12-22 16:32:39 +01:00
|
|
|
})
|
|
|
|
})
|