5211b26903
* update deps * update patches * fix new arch optout * build from source, disable new arch * update to preview.14 * upgrade off of preview to release * fix lockfile lint * update patches * add back expo-location * downgrade reanimated * downgrade pagerview * use expo-file-system/legacy * try and upgrade types/react-dom * fix type of uint8array * rm @types/react resolution from bad rebase * more type errors * fix mock * bump rn-compressor * add patch of https://github.com/bluesky-social/react-native-paste-input/pull/5 * delete NativeDropdown & remove Zeego * `import React` in expo modules * bump cocoapods * rm unused picker lib * add speculative react-native-mmkv patch * revert patch, add temp debug info to error * bring back speculative patch, patch built JS code instead of source * revert patch, just do debugging part * dep update * update resolutions, dynamic-app-icon * bump node version in eas.json * update build instructions * rm atob from docs * Delete react-native-mmkv+2.12.2.patch * bump expo which might include fix * update expo patch version * bump expo patch version * wrong version oops * update expo to latest * update react-native-compressor, rm patch
128 lines
3.3 KiB
TypeScript
128 lines
3.3 KiB
TypeScript
import {createDownloadResumable, deleteAsync} from 'expo-file-system/legacy'
|
|
import {manipulateAsync, SaveFormat} from 'expo-image-manipulator'
|
|
|
|
import {
|
|
downloadAndResize,
|
|
type DownloadAndResizeOpts,
|
|
getResizedDimensions,
|
|
} from '../../src/lib/media/manip'
|
|
|
|
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',
|
|
width: 100,
|
|
height: 100,
|
|
maxSize: 500000,
|
|
mode: 'cover',
|
|
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), [], {})
|
|
expect(manipulateAsync).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
[{resize: {height: opts.height, width: opts.width}}],
|
|
{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',
|
|
width: 100,
|
|
height: 100,
|
|
maxSize: 500000,
|
|
mode: 'cover',
|
|
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)
|
|
|
|
const initialDimensionsTwo = {
|
|
width: 1000,
|
|
height: 1200,
|
|
}
|
|
const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo)
|
|
|
|
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)
|
|
|
|
const initialDimensionsTwo = {
|
|
width: 2000,
|
|
height: 4000,
|
|
}
|
|
const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo)
|
|
|
|
expect(resizedDimensionsOne).toEqual({
|
|
width: 2000,
|
|
height: 1000,
|
|
})
|
|
expect(resizedDimensionsTwo).toEqual({
|
|
width: 1000,
|
|
height: 2000,
|
|
})
|
|
})
|
|
})
|