From 838fc601c1f89f028862212d169aebe4163c8672 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Mon, 26 Dec 2022 12:01:40 -0600 Subject: Start with highest quality compression and find a suitable size (#33) --- __tests__/lib/download.test.ts | 93 ---------------------------------------- __tests__/lib/images.test.ts | 97 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 93 deletions(-) delete mode 100644 __tests__/lib/download.test.ts create mode 100644 __tests__/lib/images.test.ts (limited to '__tests__') diff --git a/__tests__/lib/download.test.ts b/__tests__/lib/download.test.ts deleted file mode 100644 index d90e8c895..000000000 --- a/__tests__/lib/download.test.ts +++ /dev/null @@ -1,93 +0,0 @@ -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() - }) -}) diff --git a/__tests__/lib/images.test.ts b/__tests__/lib/images.test.ts new file mode 100644 index 000000000..461bd04cc --- /dev/null +++ b/__tests__/lib/images.test.ts @@ -0,0 +1,97 @@ +import {downloadAndResize, DownloadAndResizeOpts} from '../../src/lib/images' +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'), + size: 100, + } + + 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, + maxSize: 500000, + 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', + 1, + undefined, + undefined, + undefined, + {mode: 'cover'}, + ) + }) + + 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 return undefined for unsupported file type', async () => { + const opts: DownloadAndResizeOpts = { + uri: 'https://example.com/image.bmp', + width: 100, + height: 100, + maxSize: 500000, + mode: 'cover', + timeout: 10000, + } + + const result = await downloadAndResize(opts) + expect(result).toBeUndefined() + }) +}) -- cgit 1.4.1