Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139import {createDownloadResumable, deleteAsync} from 'expo-file-system/legacy'import {manipulateAsync, SaveFormat} from 'expo-image-manipulator'
import {IMAGE_SIZE_CONFIG_2K_1MB} from '../../src/lib/constants'import { downloadAndResize, type DownloadAndResizeOpts,} from '../../src/lib/media/manip'import {getResizedDimensions} from '../../src/lib/media/util'
const mockResizedImage = { path: 'file://resized-image.jpg', size: 100, width: 100, height: 100, mime: 'image/jpeg',}
describe('downloadAndResize', () => { const errorSpy = jest.spyOn(global.console, 'error')
beforeEach(() => { const mockedCreateResizedImage = manipulateAsync as jest.Mock mockedCreateResizedImage.mockResolvedValue({ uri: 'file://resized-image.jpg', ...mockResizedImage, }) })
afterEach(() => { jest.clearAllMocks() })
it('should return resized image for valid URI and options', async () => { const mockedFetch = createDownloadResumable as jest.Mock mockedFetch.mockReturnValue({ cancelAsync: jest.fn(), downloadAsync: jest .fn() .mockResolvedValue({uri: 'file://resized-image.jpg'}), })
const opts: DownloadAndResizeOpts = { uri: 'https://example.com/image.jpg', maxDimension: 2000, maxSize: 500000, timeout: 10000, }
const result = await downloadAndResize(opts) expect(result).toEqual(mockResizedImage) expect(createDownloadResumable).toHaveBeenCalledWith( opts.uri, expect.anything(), { cache: true, }, )
// First time it gets called is to get dimensions expect(manipulateAsync).toHaveBeenCalledWith(expect.any(String), [], {}) // The mocked source image is 100x100, below maxDimension, so it is not // downsized. expect(manipulateAsync).toHaveBeenCalledWith( expect.any(String), [{resize: {height: 100, width: 100}}], {format: SaveFormat.JPEG, compress: 1.0}, ) expect(deleteAsync).toHaveBeenCalledWith(expect.any(String), { idempotent: true, }) })
it('should return undefined for invalid URI', async () => { const opts: DownloadAndResizeOpts = { uri: 'invalid-uri', maxDimension: 2000, maxSize: 500000, timeout: 10000, }
const result = await downloadAndResize(opts) expect(errorSpy).toHaveBeenCalled() expect(result).toBeUndefined() })
it('should not downsize whenever dimensions are below the max dimensions', () => { const initialDimensionsOne = { width: 1200, height: 1000, } const resizedDimensionsOne = getResizedDimensions( initialDimensionsOne, IMAGE_SIZE_CONFIG_2K_1MB.maxDimension, )
const initialDimensionsTwo = { width: 1000, height: 1200, } const resizedDimensionsTwo = getResizedDimensions( initialDimensionsTwo, IMAGE_SIZE_CONFIG_2K_1MB.maxDimension, )
expect(resizedDimensionsOne).toEqual(initialDimensionsOne) expect(resizedDimensionsTwo).toEqual(initialDimensionsTwo) })
it('should resize dimensions and maintain aspect ratio if they are above the max dimensons', () => { const initialDimensionsOne = { width: 3000, height: 1500, } const resizedDimensionsOne = getResizedDimensions( initialDimensionsOne, IMAGE_SIZE_CONFIG_2K_1MB.maxDimension, )
const initialDimensionsTwo = { width: 2000, height: 4000, } const resizedDimensionsTwo = getResizedDimensions( initialDimensionsTwo, IMAGE_SIZE_CONFIG_2K_1MB.maxDimension, )
expect(resizedDimensionsOne).toEqual({ width: 2000, height: 1000, }) expect(resizedDimensionsTwo).toEqual({ width: 1000, height: 2000, }) })})