diff --git a/__test__/api/authRoute.test.tsx b/__test__/api/authRoute.test.tsx deleted file mode 100644 index 62adcb90d8e64f32349ddb32001abc6a4a5b49fd..0000000000000000000000000000000000000000 --- a/__test__/api/authRoute.test.tsx +++ /dev/null @@ -1,29 +0,0 @@ -/* eslint-disable testing-library/no-unnecessary-act */ -import { render, screen, waitFor } from '@testing-library/react'; -import { ErrorBoundary } from 'react-error-boundary'; -import ErrorFallback from '../ErrorFallback'; -import Shopper from '../Shopper'; -import { shopper } from './data'; - -test("Shopper Test mit fetch und mockup", async () => { - - jest.spyOn(global, "fetch").mockImplementation(() => - Promise.resolve({ - ok: true, - json: () => Promise.resolve(shopper) - } as Response) - ); - //ErrorBoundary ist notwendig, da sonst der Fehler nicht abgefangen wird.(in FallbackCompontent wird der Fehler behandelt) - render( - <ErrorBoundary - FallbackComponent={ErrorFallback}> - <Shopper /> - </ErrorBoundary> - ); - await waitFor(() => { - const titleTest = screen.getAllByText(shopper.shopLists[0].store); - expect(titleTest.length).toBeGreaterThan(0); - }, { timeout: 1000 }); - - expect(global.fetch).toHaveBeenCalledTimes(1); -}); \ No newline at end of file diff --git a/__test__/api/commentsRoute.test.tsx b/__test__/api/commentsRoute.test.tsx deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/__test__/api/favGamesListRoute.test.tsx b/__test__/api/favGamesListRoute.test.tsx new file mode 100644 index 0000000000000000000000000000000000000000..f692f744ad0aa46124bdf95bda260b922be01711 --- /dev/null +++ b/__test__/api/favGamesListRoute.test.tsx @@ -0,0 +1,61 @@ +import { env } from "@/env.mjs"; +import { getFavoriteGames, getGames } from '@/lib/igdb'; + +describe('Integration Tests', () => { + // Vor jedem Test den IGDB_BASE_URL-Wert speichern und Mock für fetch setzen + beforeEach(() => { + process.env.IGDB_BASE_URL = env.IGDB_BASE_URL; + global.fetch = jest.fn(); + }); + + // Nach jedem Test den IGDB_BASE_URL-Wert wiederherstellen + afterEach(() => { + process.env.IGDB_BASE_URL = undefined; + jest.resetAllMocks(); + }); + + test('getGames should fetch games from IGDB API', async () => { + const mockGames = [{ id: 1, name: 'Game 1' }, { id: 2, name: 'Game 2' }]; + const mockResponse = { ok: true, json: jest.fn().mockResolvedValue(mockGames) }; + (fetch as jest.Mock).mockResolvedValue(mockResponse); + + const games = await getGames(); + + expect(fetch).toHaveBeenCalledWith( + `${env.IGDB_BASE_URL}/games`, + expect.objectContaining({ + method: 'POST', + headers: expect.objectContaining({ + 'Client-ID': expect.any(String), + Authorization: expect.stringMatching(/^Bearer /), + }), + body: expect.stringContaining('fields name, cover.image_id;'), + }) + ); + + expect(games).toEqual(mockGames); + }); + + test('getFavoriteGames should fetch favorite games from IGDB API', async () => { + const mockGameList = [1, 2, 3]; + const mockGames = [{ id: 1, name: 'Game 1' }, { id: 2, name: 'Game 2' }]; + const mockResponse = { ok: true, json: jest.fn().mockResolvedValue(mockGames) }; + (fetch as jest.Mock).mockResolvedValue(mockResponse); + + const games = await getFavoriteGames(mockGameList); + + expect(fetch).toHaveBeenCalledWith( + `${env.IGDB_BASE_URL}/games`, + expect.objectContaining({ + method: 'POST', + headers: expect.objectContaining({ + 'Client-ID': expect.any(String), + Authorization: expect.stringMatching(/^Bearer /), + }), + body: expect.stringContaining(`where id = (${mockGameList.toString()})`), + }) + ); + + expect(games).toEqual(mockGames); + }); +}); diff --git a/package-lock.json b/package-lock.json index d5ad2ee6261e631bbca472fa041d853165c2ab84..93e0e4d4942afdc09414c1758a32ccee91dd551c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,6 +43,7 @@ "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^14.0.0", "@types/bcrypt": "^5.0.0", + "@types/jest": "^29.5.2", "@types/node": "^20.3.1", "@types/react": "^18.2.13", "@types/react-dom": "^18.2.6", diff --git a/package.json b/package.json index 15c7b740ab430d26aee33414400ceace08c0b08a..9948a5e8e19ac18de2f84ab3dfca63c44df3b76d 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^14.0.0", "@types/bcrypt": "^5.0.0", + "@types/jest": "^29.5.2", "@types/node": "^20.3.1", "@types/react": "^18.2.13", "@types/react-dom": "^18.2.6", @@ -62,4 +63,4 @@ "tailwindcss": "3.3.2", "typescript": "^5.1.3" } -} \ No newline at end of file +}