From 354978f5e81bdda9299cac68ddb34930f13bd4d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Akg=C3=BCl?= <s86116@bht-berlin.de> Date: Thu, 29 Jun 2023 06:01:26 +0200 Subject: [PATCH] tried testing --- .gitignore | 1 + __test__/api/favGamesListRoute.test.tsx | 57 ------------------------- __test__/api/gamesRoute.test.tsx | 57 ------------------------- __tests__/api/games.tsx | 49 +++++++++++++++++++++ __tests__/api/gameslist.tsx | 45 +++++++++++++++++++ app/api/gamelists/route.ts | 1 - jest.config.js | 19 --------- jest.config.mjs | 28 ++++++++++++ jest.setup.js | 5 +++ 9 files changed, 128 insertions(+), 134 deletions(-) delete mode 100644 __test__/api/favGamesListRoute.test.tsx delete mode 100644 __test__/api/gamesRoute.test.tsx create mode 100644 __tests__/api/games.tsx create mode 100644 __tests__/api/gameslist.tsx delete mode 100644 jest.config.js create mode 100644 jest.config.mjs diff --git a/.gitignore b/.gitignore index 53ff3c9..1a46f3a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ # testing /coverage +.swc # next.js /.next/ diff --git a/__test__/api/favGamesListRoute.test.tsx b/__test__/api/favGamesListRoute.test.tsx deleted file mode 100644 index 7e4f171..0000000 --- a/__test__/api/favGamesListRoute.test.tsx +++ /dev/null @@ -1,57 +0,0 @@ -// import { getFavoriteGames, getGame, getGames } from "@/lib/igdb"; - -// // Mocken der Fetch-Funktion für die API-Aufrufe -// jest.mock('node-fetch'); - -// describe('Game Service', () => { -// test('fetches games from the server', async () => { -// // Mock-Daten der Spiele -// const mockGamesData = [ -// { -// id: 1, -// name: 'Game 1', -// cover: { -// image_id: 'cover_image_id_1', -// url: 'cover_image_url_1' -// }, -// // Weitere Eigenschaften der IGame-Schnittstelle -// }, -// { -// id: 2, -// name: 'Game 2', -// cover: { -// image_id: 'cover_image_id_2', -// url: 'cover_image_url_2' -// }, -// // Weitere Eigenschaften der IGame-Schnittstelle -// }, -// ]; - -// // Mock-Funktion für die Fetch-Funktion -// const mockFetch = jest.fn().mockResolvedValueOnce({ -// ok: true, -// json: async () => mockGamesData, -// }); - -// // Mocken der globalen fetch-Funktion -// // @ts-ignore -// global.fetch = mockFetch; - -// // Setzen der Umgebungsvariable IGDB_BASE_URL -// process.env.IGDB_BASE_URL = 'https://api.igdb.com/v4'; - -// // Aufruf der Funktion, die Spiele vom Server abruft -// const games = await getGames(); - -// // Überprüfen, ob die Fetch-Funktion mit den erwarteten Parametern aufgerufen wurde -// expect(mockFetch).toHaveBeenCalledWith('https://api.igdb.com/v4/games', { -// method: 'GET', -// // Weitere erwartete Optionen für den Fetch-Aufruf -// }); - -// // Überprüfen, ob die zurückgegebenen Spiele den erwarteten Daten entsprechen -// expect(games).toEqual(mockGamesData); -// }); - -// // Weitere Tests für getGame() und getFavoriteGames() hier... -// }); diff --git a/__test__/api/gamesRoute.test.tsx b/__test__/api/gamesRoute.test.tsx deleted file mode 100644 index 80aa671..0000000 --- a/__test__/api/gamesRoute.test.tsx +++ /dev/null @@ -1,57 +0,0 @@ -import { getFavoriteGames, getGame, getGames } from "@/lib/igdb"; - -// Mocken der Fetch-Funktion für die API-Aufrufe -jest.mock('node-fetch'); - -describe('Game Service', () => { - test('fetches games from the server', async () => { - // Mock-Daten der Spiele - const mockGamesData = [ - { - id: 1, - name: 'Game 1', - cover: { - image_id: 'cover_image_id_1', - url: 'cover_image_url_1' - }, - // Weitere Eigenschaften der IGame-Schnittstelle - }, - { - id: 2, - name: 'Game 2', - cover: { - image_id: 'cover_image_id_2', - url: 'cover_image_url_2' - }, - // Weitere Eigenschaften der IGame-Schnittstelle - }, - ]; - - // Mock-Funktion für die Fetch-Funktion - const mockFetch = jest.fn().mockResolvedValueOnce({ - ok: true, - json: async () => mockGamesData, - }); - - // Mocken der globalen fetch-Funktion - // @ts-ignore - global.fetch = mockFetch; - - // Setzen der Umgebungsvariable IGDB_BASE_URL - process.env.IGDB_BASE_URL = 'https://api.igdb.com/v4'; - - // Aufruf der Funktion, die Spiele vom Server abruft - const games = await getGames(); - - // Überprüfen, ob die Fetch-Funktion mit den erwarteten Parametern aufgerufen wurde - expect(mockFetch).toHaveBeenCalledWith('https://api.igdb.com/v4/games', { - method: 'GET', - // Weitere erwartete Optionen für den Fetch-Aufruf - }); - - // Überprüfen, ob die zurückgegebenen Spiele den erwarteten Daten entsprechen - expect(games).toEqual(mockGamesData); - }); - - // Weitere Tests für getGame() und getFavoriteGames() hier... -}); \ No newline at end of file diff --git a/__tests__/api/games.tsx b/__tests__/api/games.tsx new file mode 100644 index 0000000..c05b398 --- /dev/null +++ b/__tests__/api/games.tsx @@ -0,0 +1,49 @@ +import { getGames } from "@/lib/igdb"; + +describe('games api', () => { + + const mockGamesData = [ + { + id: 1, + name: 'Game 1', + cover: { + image_id: 'cover_image_id_1', + url: 'cover_image_url_1' + }, + }, + { + id: 2, + name: 'Game 2', + cover: { + image_id: 'cover_image_id_2', + url: 'cover_image_url_2' + }, + }, + ]; + + const mockAuthData = [ + { + access_token: "1", + expires_in: 420, + token_type: 'bearer', + } + ] + + beforeAll(async () => { + global.fetch = jest.fn() + .mockResolvedValueOnce(() => Promise.resolve({ + ok: true, + json: () => Promise.resolve(mockAuthData), + })) + .mockResolvedValueOnce(() => Promise.resolve({ + ok: true, + json: () => Promise.resolve(mockGamesData), + })) + }); + + test('get games', async () => { + const games = await getGames(); + + expect(games).toEqual("Game 1"); + }); +}); \ No newline at end of file diff --git a/__tests__/api/gameslist.tsx b/__tests__/api/gameslist.tsx new file mode 100644 index 0000000..2bdbf5e --- /dev/null +++ b/__tests__/api/gameslist.tsx @@ -0,0 +1,45 @@ +describe('games list api', () => { + + const mockGamesData = [ + { + id: 1, + name: 'Game 1', + cover: { + image_id: 'cover_image_id_1', + url: 'cover_image_url_1' + }, + }, + { + id: 2, + name: 'Game 2', + cover: { + image_id: 'cover_image_id_2', + url: 'cover_image_url_2' + }, + }, + ]; + + const mockAuthData = [ + { + access_token: "1", + expires_in: 420, + token_type: 'bearer', + } + ] + + beforeAll(async () => { + global.fetch = jest.fn() + .mockResolvedValueOnce(() => Promise.resolve({ + ok: true, + json: () => Promise.resolve(mockAuthData), + })) + .mockResolvedValueOnce(() => Promise.resolve({ + ok: true, + json: () => Promise.resolve(mockGamesData), + })) + }); + + test('get gameslist', async () => { + expect("test").toEqual("test"); + }); +}); \ No newline at end of file diff --git a/app/api/gamelists/route.ts b/app/api/gamelists/route.ts index fa9640e..a107c73 100644 --- a/app/api/gamelists/route.ts +++ b/app/api/gamelists/route.ts @@ -4,7 +4,6 @@ import { User } from "@prisma/client"; import { revalidatePath } from "next/cache"; import { NextRequest, NextResponse } from "next/server"; - export async function PUT(req: NextRequest) { const sessionUser = await getCurrentUser(); diff --git a/jest.config.js b/jest.config.js deleted file mode 100644 index e794dff..0000000 --- a/jest.config.js +++ /dev/null @@ -1,19 +0,0 @@ -const nextJest = require('next/jest') - -const createJestConfig = nextJest({ - //Provide the path to your Next.js app to load next.config.js and .env in your test environment. - dir: '.', -}) - -// Add your custom config to be passed to Jest. -const customJestConfig = { - setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], - moduleNameMapper: { - //Handle module aliases (this will be automatically configured for you soon) - '^@app/api/(.*)$': '<rootDir>/app/api/$1', - }, - testEnvironment: 'jest-environment-jsdom', -} - -// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async. -module.exports = createJestConfig(customJestConfig) \ No newline at end of file diff --git a/jest.config.mjs b/jest.config.mjs new file mode 100644 index 0000000..cd633c9 --- /dev/null +++ b/jest.config.mjs @@ -0,0 +1,28 @@ +import nextJest from 'next/jest.js'; + +// Provide the path to your Next.js app to load next.config.js and .env files in your test environment +const createJestConfig = nextJest({ dir: './' }) + +// Custom client config to be passed to Jest. +const clientTestConfig = { + displayName: "client", + testPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/__tests__/api/"], + setupFilesAfterEnv: ["<rootDir>/jest.setup.js"], + testEnvironment: "jest-environment-jsdom", +}; + +// Custom server config to be passed to Jest. +const serverTestConfig = { + displayName: "server", + testMatch: ["**/__tests__/api/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"], + testEnvironment: "jest-environment-node", +}; + +// Add any custom config to be passed to Jest +/** @type {import('jest').Config} */ +const config = { + // Add more setup options before each test is run + projects: [await createJestConfig(clientTestConfig)(), await createJestConfig(serverTestConfig)()], +}; + +export default config; \ No newline at end of file diff --git a/jest.setup.js b/jest.setup.js index 666127a..306452c 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -1 +1,6 @@ +// Optional: configure or set up a testing framework before each test. +// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js` + +// Used for __tests__/testing-library.js +// Learn more: https://github.com/testing-library/jest-dom import '@testing-library/jest-dom/extend-expect'; -- GitLab