about summary refs log tree commit diff
path: root/__tests__
diff options
context:
space:
mode:
authorHailey <me@haileyok.com>2024-09-24 10:15:33 -0700
committerGitHub <noreply@github.com>2024-09-24 10:15:33 -0700
commitea43d20c61547523e34ae864ca4ddffdedd8dfb1 (patch)
tree876b0f207f19df040bd15281477eb90584ab5610 /__tests__
parentd2fae81b33ae0a73d0b9f87700365d60bc51f094 (diff)
downloadvoidsky-ea43d20c61547523e34ae864ca4ddffdedd8dfb1.tar.zst
Remove image resizer (#5464)
Diffstat (limited to '__tests__')
-rw-r--r--__tests__/lib/images.test.ts126
1 files changed, 65 insertions, 61 deletions
diff --git a/__tests__/lib/images.test.ts b/__tests__/lib/images.test.ts
index 595f566c4..a5acad25f 100644
--- a/__tests__/lib/images.test.ts
+++ b/__tests__/lib/images.test.ts
@@ -1,26 +1,30 @@
-import ImageResizer from '@bam.tech/react-native-image-resizer'
+import {deleteAsync} from 'expo-file-system'
+import {manipulateAsync, SaveFormat} from 'expo-image-manipulator'
 import RNFetchBlob from 'rn-fetch-blob'
 
 import {
   downloadAndResize,
   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')
 
-  const mockResizedImage = {
-    path: jest.fn().mockReturnValue('file://resized-image.jpg'),
-    size: 100,
-    width: 50,
-    height: 50,
-    mime: 'image/jpeg',
-  }
-
   beforeEach(() => {
-    const mockedCreateResizedImage =
-      ImageResizer.createResizedImage as jest.Mock
-    mockedCreateResizedImage.mockResolvedValue(mockResizedImage)
+    const mockedCreateResizedImage = manipulateAsync as jest.Mock
+    mockedCreateResizedImage.mockResolvedValue({
+      uri: 'file://resized-image.jpg',
+      ...mockResizedImage,
+    })
   })
 
   afterEach(() => {
@@ -54,17 +58,17 @@ describe('downloadAndResize', () => {
       'GET',
       'https://example.com/image.jpg',
     )
-    expect(ImageResizer.createResizedImage).toHaveBeenCalledWith(
-      'file://downloaded-image.jpg',
-      100,
-      100,
-      'JPEG',
-      100,
-      undefined,
-      undefined,
-      undefined,
-      {mode: 'cover'},
+
+    // 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 () => {
@@ -82,11 +86,11 @@ describe('downloadAndResize', () => {
     expect(result).toBeUndefined()
   })
 
-  it('should return undefined for unsupported file type', async () => {
+  it('should return undefined for non-200 response', async () => {
     const mockedFetch = RNFetchBlob.fetch as jest.Mock
     mockedFetch.mockResolvedValueOnce({
       path: jest.fn().mockReturnValue('file://downloaded-image'),
-      info: jest.fn().mockReturnValue({status: 200}),
+      info: jest.fn().mockReturnValue({status: 400}),
       flush: jest.fn(),
     })
 
@@ -100,47 +104,47 @@ describe('downloadAndResize', () => {
     }
 
     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',
-    )
-    expect(ImageResizer.createResizedImage).toHaveBeenCalledWith(
-      'file://downloaded-image',
-      100,
-      100,
-      'JPEG',
-      100,
-      undefined,
-      undefined,
-      undefined,
-      {mode: 'cover'},
-    )
+    expect(errorSpy).not.toHaveBeenCalled()
+    expect(result).toBeUndefined()
   })
 
-  it('should return undefined for non-200 response', async () => {
-    const mockedFetch = RNFetchBlob.fetch as jest.Mock
-    mockedFetch.mockResolvedValueOnce({
-      path: jest.fn().mockReturnValue('file://downloaded-image'),
-      info: jest.fn().mockReturnValue({status: 400}),
-      flush: jest.fn(),
-    })
+  it('should not downsize whenever dimensions are below the max dimensions', () => {
+    const initialDimensionsOne = {
+      width: 1200,
+      height: 1000,
+    }
+    const resizedDimensionsOne = getResizedDimensions(initialDimensionsOne)
 
-    const opts: DownloadAndResizeOpts = {
-      uri: 'https://example.com/image',
-      width: 100,
-      height: 100,
-      maxSize: 500000,
-      mode: 'cover',
-      timeout: 10000,
+    const initialDimensionsTwo = {
+      width: 1000,
+      height: 1200,
     }
+    const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo)
 
-    const result = await downloadAndResize(opts)
-    expect(errorSpy).not.toHaveBeenCalled()
-    expect(result).toBeUndefined()
+    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,
+    })
   })
 })