Bluesky app fork with some witchin' additions 馃挮
at main 127 lines 3.4 kB view raw
1import {createDownloadResumable, deleteAsync} from 'expo-file-system/legacy' 2import {manipulateAsync, SaveFormat} from 'expo-image-manipulator' 3 4import { 5 downloadAndResize, 6 type DownloadAndResizeOpts, 7 getResizedDimensions, 8} from '../../src/lib/media/manip' 9 10const mockResizedImage = { 11 path: 'file://resized-image.jpg', 12 size: 100, 13 width: 100, 14 height: 100, 15 mime: 'image/jpeg', 16} 17 18describe('downloadAndResize', () => { 19 const errorSpy = jest.spyOn(global.console, 'error') 20 21 beforeEach(() => { 22 const mockedCreateResizedImage = manipulateAsync as jest.Mock 23 mockedCreateResizedImage.mockResolvedValue({ 24 uri: 'file://resized-image.jpg', 25 ...mockResizedImage, 26 }) 27 }) 28 29 afterEach(() => { 30 jest.clearAllMocks() 31 }) 32 33 it('should return resized image for valid URI and options', async () => { 34 const mockedFetch = createDownloadResumable as jest.Mock 35 mockedFetch.mockReturnValue({ 36 cancelAsync: jest.fn(), 37 downloadAsync: jest 38 .fn() 39 .mockResolvedValue({uri: 'file://resized-image.jpg'}), 40 }) 41 42 const opts: DownloadAndResizeOpts = { 43 uri: 'https://example.com/image.jpg', 44 width: 100, 45 height: 100, 46 maxSize: 500000, 47 mode: 'cover', 48 timeout: 10000, 49 } 50 51 const result = await downloadAndResize(opts) 52 expect(result).toEqual(mockResizedImage) 53 expect(createDownloadResumable).toHaveBeenCalledWith( 54 opts.uri, 55 expect.anything(), 56 { 57 cache: true, 58 }, 59 ) 60 61 // First time it gets called is to get dimensions 62 expect(manipulateAsync).toHaveBeenCalledWith(expect.any(String), [], {}) 63 expect(manipulateAsync).toHaveBeenCalledWith( 64 expect.any(String), 65 [{resize: {height: opts.height, width: opts.width}}], 66 {format: SaveFormat.JPEG, compress: 1.0}, 67 ) 68 expect(deleteAsync).toHaveBeenCalledWith(expect.any(String), { 69 idempotent: true, 70 }) 71 }) 72 73 it('should return undefined for invalid URI', async () => { 74 const opts: DownloadAndResizeOpts = { 75 uri: 'invalid-uri', 76 width: 100, 77 height: 100, 78 maxSize: 500000, 79 mode: 'cover', 80 timeout: 10000, 81 } 82 83 const result = await downloadAndResize(opts) 84 expect(errorSpy).toHaveBeenCalled() 85 expect(result).toBeUndefined() 86 }) 87 88 it('should not downsize whenever dimensions are below the max dimensions', () => { 89 const initialDimensionsOne = { 90 width: 1200, 91 height: 1000, 92 } 93 const resizedDimensionsOne = getResizedDimensions(initialDimensionsOne) 94 95 const initialDimensionsTwo = { 96 width: 1000, 97 height: 1200, 98 } 99 const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo) 100 101 expect(resizedDimensionsOne).toEqual(initialDimensionsOne) 102 expect(resizedDimensionsTwo).toEqual(initialDimensionsTwo) 103 }) 104 105 it('should resize dimensions and maintain aspect ratio if they are above the max dimensons', () => { 106 const initialDimensionsOne = { 107 width: 3000, 108 height: 1500, 109 } 110 const resizedDimensionsOne = getResizedDimensions(initialDimensionsOne) 111 112 const initialDimensionsTwo = { 113 width: 2000, 114 height: 4000, 115 } 116 const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo) 117 118 expect(resizedDimensionsOne).toEqual({ 119 width: 2000, 120 height: 1000, 121 }) 122 expect(resizedDimensionsTwo).toEqual({ 123 width: 1000, 124 height: 2000, 125 }) 126 }) 127})