Newer
Older
import { GET } from "@/app/api/games/route"
import { NextRequest } from "next/server"
describe("games api", () => {
const mockRequest = {
nextUrl: {
searchParams: new URLSearchParams(),
},
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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,
}))
})
})