From 8b3259bf90cc70b7ddf3117f1b9ce9eb0d06c828 Mon Sep 17 00:00:00 2001
From: Serdar D <serdar-dorak@hotmail.de>
Date: Wed, 28 Jun 2023 18:42:15 +0200
Subject: [PATCH] favGamesList test noch fehlerhaft

---
 __test__/api/authRoute.test.tsx         | 29 ------------
 __test__/api/commentsRoute.test.tsx     |  0
 __test__/api/favGamesListRoute.test.tsx | 61 +++++++++++++++++++++++++
 package-lock.json                       |  1 +
 package.json                            |  3 +-
 5 files changed, 64 insertions(+), 30 deletions(-)
 delete mode 100644 __test__/api/authRoute.test.tsx
 delete mode 100644 __test__/api/commentsRoute.test.tsx
 create mode 100644 __test__/api/favGamesListRoute.test.tsx

diff --git a/__test__/api/authRoute.test.tsx b/__test__/api/authRoute.test.tsx
deleted file mode 100644
index 62adcb9..0000000
--- 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 e69de29..0000000
diff --git a/__test__/api/favGamesListRoute.test.tsx b/__test__/api/favGamesListRoute.test.tsx
new file mode 100644
index 0000000..f692f74
--- /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 d5ad2ee..93e0e4d 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 15c7b74..9948a5e 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
+}
-- 
GitLab