Files
portfolio/app/__tests__/api/fetchImage.test.tsx
Denshooter 180b9aa9f8 full upgrade (#31)
*  chore: update CI workflow to include testing and multi-arch build (#29)

*  chore: remove unused dependencies from package-lock.json and updated to a better local dev environment (#30)

*  test: add unit tests

*  test: add unit tests for whole project

*  feat: add whatwg-fetch for improved fetch support

*  chore: update Node.js version to 22 in workflow

*  refactor: update types and improve email handling tests

*  refactor: remove unused imports

*  fix: normalize image name to lowercase in workflows

*  fix: ensure Docker image names are consistently lowercase

*  chore: update

*  chore: update base URL to use secret variable

*  chore: update to login to ghcr

*  fix: add missing 'fi' to close if statement in workflow
2025-02-16 16:36:21 +01:00

53 lines
1.5 KiB
TypeScript

import { GET } from '@/app/api/fetchImage/route';
import { NextRequest } from 'next/server';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch-img';
jest.mock('next/server', () => {
class NextResponseClass {
body: unknown;
init: unknown;
constructor(body: unknown, init?: unknown) {
this.body = body;
this.init = init;
}
static json(body: unknown, init?: unknown) {
return new NextResponseClass(body, init);
}
static from(body: unknown, init?: unknown) {
return new NextResponseClass(body, init);
}
}
return { NextResponse: NextResponseClass };
});
global.fetch = mockFetch({
ok: true,
headers: {
get: jest.fn().mockReturnValue('image/jpeg'),
},
arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(8)),
});
describe('GET /api/fetchImage', () => {
it('should return an error if no image URL is provided', async () => {
const mockRequest = {
url: 'http://localhost/api/fetchImage',
} as unknown as NextRequest;
const response = await GET(mockRequest);
expect(response.body).toEqual({ error: 'Missing URL parameter' });
expect(response.init.status).toBe(400);
});
it('should fetch an image if URL is provided', async () => {
const mockRequest = {
url: 'http://localhost/api/fetchImage?url=https://example.com/image.jpg',
} as unknown as NextRequest;
const response = await GET(mockRequest);
expect(response.body).toBeDefined();
expect(response.init.headers['Content-Type']).toBe('image/jpeg');
});
});