import fs from 'fs/promises'; import path from 'path'; import axios from 'axios'; import { checkImageUrl, allowedFormats, requestWaitingTimeInMs } from '..'; jest.useFakeTimers(); describe('checkImageUrl', () => { it('should return a correct result for jpeg', async () => { const isAllowed = allowedFormats.includes('jpeg'); const jpegImageBuffer = await fs.readFile( path.join(__dirname, './images/jpeg-test.jpeg') ); const mockGet = jest.spyOn(axios, 'get'); mockGet.mockImplementation(() => Promise.resolve({ data: jpegImageBuffer }) ); const result = await checkImageUrl('https://tsthisisforyoudear.com/'); expect(result).toBe(isAllowed); }); it('should return a correct result for webp', async () => { const isAllowed = allowedFormats.includes('webp'); const webpImageBuffer = await fs.readFile( path.join(__dirname, './images/webp-test.webp') ); const mockGet = jest.spyOn(axios, 'get'); mockGet.mockImplementation(() => Promise.resolve({ data: webpImageBuffer }) ); const result = await checkImageUrl('https://tsthisisforyoudear.com/'); expect(result).toBe(isAllowed); }); it('should return null when server not responding', () => { const mockGet = jest.spyOn(axios, 'get'); mockGet.mockImplementation(() => new Promise(() => {})); const pendingPromise = checkImageUrl( 'https://tsthisisforyoudear.com/' ).then((result) => { expect(result).toBe(null); }); jest.advanceTimersByTime(requestWaitingTimeInMs); return pendingPromise; }); });