From cdf5c6e537e89b2a68fc2bc01f9dcbccedd2ca83 Mon Sep 17 00:00:00 2001 From: Aster Fialla Date: Tue, 24 Feb 2026 18:03:55 -0500 Subject: [PATCH] tested utils checkImageFormatValidity --- src/helpers/utils.js | 9 ++++--- tests/helpers/utils.test.js | 54 ++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/helpers/utils.js b/src/helpers/utils.js index c5ff1ac..f1a1083 100644 --- a/src/helpers/utils.js +++ b/src/helpers/utils.js @@ -15,6 +15,7 @@ u.debounce = function(func, delay) { * * @async * @param {string} imageUrl - The url of the image + * @returns {bool} - Whether the image is in a valid format * @throws {Error} When loading the profile picture from a URL doesn't work, or it fails requirements. */ u.checkImageFormatValidity = async function (imageUrl) { @@ -24,15 +25,17 @@ u.checkImageFormatValidity = async function (imageUrl) { response = await fetch(imageUrl); } catch(e) { - throw new Error(`${enums.err.CANNOT_FETCH_RESOURCE}: {error.message}`); + throw new Error(`${enums.err.PROPIC_CANNOT_LOAD}: ${e.message}`); } try { - blobFile = response.blob(); + blobFile = await response.blob(); if (blobFile.size > 1000000 || !acceptableImages.includes(blobFile.type)) throw new Error(enums.err.PROPIC_FAILS_REQUIREMENTS); + + return true; } catch(error) { - throw new Error(`${enums.err.PROPIC_CANNOT_LOAD}: ${error.message}`); + throw new Error(error.message); } } diff --git a/tests/helpers/utils.test.js b/tests/helpers/utils.test.js index 5414193..e6f5b7c 100644 --- a/tests/helpers/utils.test.js +++ b/tests/helpers/utils.test.js @@ -1,15 +1,63 @@ const {enums} = require("../../src/enums"); -const fetchMock = require('jest-fetch-mock'); -fetchMock.enableMocks(); - const {utils} = require("../../src/helpers/utils.js"); describe('utils', () => { + const attachmentUrl = 'oya.png'; + let blob; + beforeEach(() => { jest.resetModules(); jest.clearAllMocks(); + blob = new Blob([JSON.stringify({attachmentUrl: attachmentUrl})], {type: 'image/png'}); + global.fetch = jest.fn(() => + Promise.resolve({ + blob: () => Promise.resolve(blob), + }) + ); + + }) + + describe('checkImageFormatValidity', () => { + + test('calls fetch with imageUrl and returns true if no errors', async() => { + // Act + const res = await utils.checkImageFormatValidity(attachmentUrl); + // Assert + expect(res).toBe(true); + expect(fetch).toHaveBeenCalledTimes(1); + expect(fetch).toHaveBeenCalledWith(attachmentUrl); + }) + + test('throws error if fetch returns error', async() => { + // Arrange + global.fetch = jest.fn().mockImplementation(() =>{throw Error('error');}); + // Act & Assert + await expect(utils.checkImageFormatValidity(attachmentUrl)).rejects.toThrow(`${enums.err.PROPIC_CANNOT_LOAD}: error`); + }) + + test('throws error if blob returns error', async() => { + // Arrange + global.fetch = jest.fn(() => + Promise.resolve({ + blob: () => Promise.reject(new Error('error')) + })) + // Act & Assert + await expect(utils.checkImageFormatValidity(attachmentUrl)).rejects.toThrow('error'); + }) + + test('throws error if blob in wrong format', async() => { + // Arrange + blob = new Blob([JSON.stringify({attachmentUrl})], {type: 'text/html'}); + global.fetch = jest.fn(() => + Promise.resolve({ + blob: () => Promise.resolve(blob), + }) + ); + // Act & Assert + await expect(utils.checkImageFormatValidity(attachmentUrl)).rejects.toThrow(enums.err.PROPIC_FAILS_REQUIREMENTS); + }) }) afterEach(() => {