Skip to content
Snippets Groups Projects
games.test.ts 2.02 KiB
Newer Older
Yusuf Akgül's avatar
Yusuf Akgül committed
import { GET } from "@/app/api/games/route"
import { NextRequest } from "next/server"

describe("games api", () => {
    const mockRequest = {
        nextUrl: {
            searchParams: new URLSearchParams(),
        },
Yusuf Akgül's avatar
Yusuf Akgül committed

    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(() => {
        global.fetch = jest.fn()
            .mockResolvedValueOnce({
                ok: true,
                json: () => Promise.resolve(mockAuthData),
            })
            .mockResolvedValueOnce({
                ok: true,
                json: () => Promise.resolve(mockGamesData),
            })
    })

    test("GET method returns games", async () => {
        const expectedGames = mockGamesData

        // Mock the implementation of getGames
        const getGamesMock = jest.fn().mockResolvedValue(expectedGames)
        jest.mock("@/lib/igdb", () => ({
            getGames: getGamesMock,
        }))

        // Call the GET function with the mock request
        const response = await GET(mockRequest as NextRequest)

        // Verify that getGames was called with the expected arguments
        expect(getGamesMock).toHaveBeenCalledWith(
            expect.any(Number),
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        )

        // Verify that the response matches the expected response
        expect(response).toEqual(expect.objectContaining({
            status: 200,
            body: expectedGames,
        }))
    })
})