|
| 1 | +import { http, HttpResponse } from "msw"; |
| 2 | +import { setupServer } from "msw/node"; |
| 3 | + |
| 4 | +import { apiBaseUrl } from "../utils/internal"; |
| 5 | +import { buildAuthorization } from "../utils/public"; |
| 6 | +import { getUserSetRequests } from "./getUserSetRequests"; |
| 7 | +import type { GetUserSetRequestsResponse, UserSetRequests } from "./models"; |
| 8 | +import { RequestListType } from "./models"; |
| 9 | + |
| 10 | +const server = setupServer(); |
| 11 | + |
| 12 | +describe("Function: getUserSetRequests", () => { |
| 13 | + // MSW Setup |
| 14 | + beforeAll(() => server.listen()); |
| 15 | + afterEach(() => server.resetHandlers()); |
| 16 | + afterAll(() => server.close()); |
| 17 | + |
| 18 | + it("is defined #sanity", () => { |
| 19 | + // ASSERT |
| 20 | + expect(getUserSetRequests).toBeDefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("using defaults, retrieves the list of set requests of the given user", async () => { |
| 24 | + // ARRANGE |
| 25 | + const authorization = buildAuthorization({ |
| 26 | + username: "mockUserName", |
| 27 | + webApiKey: "mockWebApiKey", |
| 28 | + }); |
| 29 | + |
| 30 | + const mockResponse = mockGetUserSetRequestsResponse; |
| 31 | + |
| 32 | + server.use( |
| 33 | + http.get(`${apiBaseUrl}/API_GetUserSetRequests.php`, (info) => { |
| 34 | + const url = new URL(info.request.url); |
| 35 | + expect(url.searchParams.get("u")).toEqual(mockOtherUsername); |
| 36 | + expect(url.searchParams.has("t")).toBeFalsy(); |
| 37 | + return HttpResponse.json(mockResponse); |
| 38 | + }) |
| 39 | + ); |
| 40 | + |
| 41 | + // ACT |
| 42 | + const response = await getUserSetRequests(authorization, { |
| 43 | + username: mockOtherUsername, |
| 44 | + }); |
| 45 | + expect(response).toEqual(mockUserSetRequestsValue); |
| 46 | + }); |
| 47 | + |
| 48 | + it.each([ |
| 49 | + { requestListType: RequestListType.ActiveRequests }, |
| 50 | + { requestListType: RequestListType.AllRequests }, |
| 51 | + ])( |
| 52 | + "calls the 'User Set Requests' endpoint with a given request list type ($requestListType)", |
| 53 | + async ({ requestListType: expectedRequestListType }) => { |
| 54 | + // ARRANGE |
| 55 | + const authorization = buildAuthorization({ |
| 56 | + username: "mockUserName", |
| 57 | + webApiKey: "mockWebApiKey", |
| 58 | + }); |
| 59 | + |
| 60 | + server.use( |
| 61 | + http.get(`${apiBaseUrl}/API_GetUserSetRequests.php`, (info) => { |
| 62 | + const url = new URL(info.request.url); |
| 63 | + expect(url.searchParams.get("u")).toEqual(mockOtherUsername); |
| 64 | + expect(url.searchParams.get("t")).toEqual( |
| 65 | + String(expectedRequestListType) |
| 66 | + ); |
| 67 | + return HttpResponse.json(mockGetUserSetRequestsResponse); |
| 68 | + }) |
| 69 | + ); |
| 70 | + |
| 71 | + // ACT |
| 72 | + await getUserSetRequests(authorization, { |
| 73 | + username: mockOtherUsername, |
| 74 | + requestListType: expectedRequestListType, |
| 75 | + }); |
| 76 | + } |
| 77 | + ); |
| 78 | + |
| 79 | + it.each([ |
| 80 | + { status: 503, statusText: "The API is currently down" }, |
| 81 | + { status: 422, statusText: "HTTP Error: Status 422 Unprocessable Entity" }, |
| 82 | + ])( |
| 83 | + "given the API returns a $status, throws an error", |
| 84 | + async ({ status, statusText }) => { |
| 85 | + // ARRANGE |
| 86 | + const authorization = buildAuthorization({ |
| 87 | + username: "mockUserName", |
| 88 | + webApiKey: "mockWebApiKey", |
| 89 | + }); |
| 90 | + |
| 91 | + const mockResponse = `<html><body>${statusText}</body></html>`; |
| 92 | + |
| 93 | + server.use( |
| 94 | + http.get(`${apiBaseUrl}/API_GetUserSetRequests.php`, () => |
| 95 | + HttpResponse.json(mockResponse, { status, statusText }) |
| 96 | + ) |
| 97 | + ); |
| 98 | + |
| 99 | + // ASSERT |
| 100 | + await expect( |
| 101 | + getUserSetRequests(authorization, { username: mockOtherUsername }) |
| 102 | + ).rejects.toThrow(); |
| 103 | + } |
| 104 | + ); |
| 105 | +}); |
| 106 | + |
| 107 | +const mockOtherUsername = "otherMockUser"; |
| 108 | + |
| 109 | +const mockGetUserSetRequestsResponse: GetUserSetRequestsResponse = { |
| 110 | + RequestedSets: [ |
| 111 | + { |
| 112 | + GameID: 8149, |
| 113 | + Title: "Example Set 1", |
| 114 | + ConsoleID: 0, |
| 115 | + ConsoleName: "Example Console", |
| 116 | + ImageIcon: "/Images/000001.png", |
| 117 | + }, |
| 118 | + { |
| 119 | + GameID: 9001, |
| 120 | + Title: "Example Set 2", |
| 121 | + ConsoleID: 2, |
| 122 | + ConsoleName: "Example Console 2", |
| 123 | + ImageIcon: "/Images/000002.png", |
| 124 | + }, |
| 125 | + ], |
| 126 | + TotalRequests: 5, |
| 127 | + PointsForNext: 5000, |
| 128 | +}; |
| 129 | + |
| 130 | +const mockUserSetRequestsValue: UserSetRequests = { |
| 131 | + requestedSets: [ |
| 132 | + { |
| 133 | + gameId: 8149, |
| 134 | + title: "Example Set 1", |
| 135 | + consoleId: 0, |
| 136 | + consoleName: "Example Console", |
| 137 | + imageIcon: "/Images/000001.png", |
| 138 | + }, |
| 139 | + { |
| 140 | + gameId: 9001, |
| 141 | + title: "Example Set 2", |
| 142 | + consoleId: 2, |
| 143 | + consoleName: "Example Console 2", |
| 144 | + imageIcon: "/Images/000002.png", |
| 145 | + }, |
| 146 | + ], |
| 147 | + totalRequests: 5, |
| 148 | + pointsForNext: 5000, |
| 149 | +}; |
0 commit comments