about summary refs log tree commit diff
path: root/__tests__/lib/download.test.ts
diff options
context:
space:
mode:
authorJoão Ferreiro <ferreiro@pinkroom.dev>2022-12-22 15:32:39 +0000
committerGitHub <noreply@github.com>2022-12-22 09:32:39 -0600
commit7517b65dcd676f36d38f31c991929c32168b3e12 (patch)
tree65793d2575b205365c2997b4bbddc1ba6424d2ba /__tests__/lib/download.test.ts
parent4913a07e3365d2004e67e9131dd4b4c15094dd33 (diff)
downloadvoidsky-7517b65dcd676f36d38f31c991929c32168b3e12.tar.zst
Unit testing (#32)
* add testing lib

* remove coverage folder from git

* finished basic test setup

* fix tests typescript and import paths

* add first snapshot

* testing utils

* rename test files; update script flags; ++tests

* testing utils functions

* testing downloadAndResize wip

* remove download test

* specify unwanted coverage paths;
remove update snapshots flag

* fix strings tests

* testing downloadAndResize method

* increasing testing

* fixing snapshots wip

* fixed shell mobile snapshot

* adding snapshots for the screens

* fix onboard snapshot

* fix typescript issues

* fix TabsSelector snapshot

* Account for testing device's locale in ago() tests

* Remove platform detection on regex

Co-authored-by: Paul Frazee <pfrazee@gmail.com>
Diffstat (limited to '__tests__/lib/download.test.ts')
-rw-r--r--__tests__/lib/download.test.ts93
1 files changed, 93 insertions, 0 deletions
diff --git a/__tests__/lib/download.test.ts b/__tests__/lib/download.test.ts
new file mode 100644
index 000000000..d90e8c895
--- /dev/null
+++ b/__tests__/lib/download.test.ts
@@ -0,0 +1,93 @@
+import {downloadAndResize, DownloadAndResizeOpts} from '../../src/lib/download'
+import ImageResizer from '@bam.tech/react-native-image-resizer'
+import RNFetchBlob from 'rn-fetch-blob'
+
+jest.mock('rn-fetch-blob', () => ({
+  config: jest.fn().mockReturnThis(),
+  cancel: jest.fn(),
+  fetch: jest.fn(),
+}))
+jest.mock('@bam.tech/react-native-image-resizer', () => ({
+  createResizedImage: jest.fn(),
+}))
+
+describe('downloadAndResize', () => {
+  const errorSpy = jest.spyOn(global.console, 'error')
+
+  const mockResizedImage = {
+    path: jest.fn().mockReturnValue('file://resized-image.jpg'),
+  }
+
+  beforeEach(() => {
+    jest.clearAllMocks()
+
+    const mockedCreateResizedImage =
+      ImageResizer.createResizedImage as jest.Mock
+    mockedCreateResizedImage.mockResolvedValue(mockResizedImage)
+  })
+
+  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,
+      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',
+      0.7,
+      undefined,
+      undefined,
+      undefined,
+      {mode: 'cover'},
+    )
+  })
+
+  it('should return undefined for invalid URI', async () => {
+    const opts: DownloadAndResizeOpts = {
+      uri: 'invalid-uri',
+      width: 100,
+      height: 100,
+      mode: 'cover',
+      timeout: 10000,
+    }
+
+    const result = await downloadAndResize(opts)
+    expect(errorSpy).toHaveBeenCalled()
+    expect(result).toBeUndefined()
+  })
+
+  it('should return undefined for unsupported file type', async () => {
+    const opts: DownloadAndResizeOpts = {
+      uri: 'https://example.com/image.bmp',
+      width: 100,
+      height: 100,
+      mode: 'cover',
+      timeout: 10000,
+    }
+
+    const result = await downloadAndResize(opts)
+    expect(result).toBeUndefined()
+  })
+})