|
| 1 | +/* eslint-disable sonarjs/no-duplicate-string */ |
| 2 | + |
| 3 | +import { http, HttpResponse } from "msw"; |
| 4 | +import { setupServer } from "msw/node"; |
| 5 | + |
| 6 | +import { apiBaseUrl } from "../utils/internal"; |
| 7 | +import { buildAuthorization } from "../utils/public"; |
| 8 | +import { getComments } from "./getComments"; |
| 9 | +import type { CommentsResponse, GetCommentsResponse } from "./models"; |
| 10 | + |
| 11 | +const server = setupServer(); |
| 12 | + |
| 13 | +describe("Function: getComments", () => { |
| 14 | + // MSW Setup |
| 15 | + beforeAll(() => server.listen()); |
| 16 | + afterEach(() => server.resetHandlers()); |
| 17 | + afterAll(() => server.close()); |
| 18 | + |
| 19 | + it("is defined #sanity", () => { |
| 20 | + // ASSERT |
| 21 | + expect(getComments).toBeDefined(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("retrieves the comments on a user's wall", async () => { |
| 25 | + // ARRANGE |
| 26 | + const authorization = buildAuthorization({ |
| 27 | + username: "mockUserName", |
| 28 | + webApiKey: "mockWebApiKey", |
| 29 | + }); |
| 30 | + |
| 31 | + const mockResponse: GetCommentsResponse = { |
| 32 | + Count: 2, |
| 33 | + Total: 2, |
| 34 | + Results: [ |
| 35 | + { |
| 36 | + User: "PlayTester", |
| 37 | + Submitted: "2024-07-31T11:22:23.000000Z", |
| 38 | + CommentText: "Comment 1", |
| 39 | + }, |
| 40 | + { |
| 41 | + User: "PlayTester", |
| 42 | + Submitted: "2024-07-31T11:22:23.000000Z", |
| 43 | + CommentText: "Comment 2", |
| 44 | + }, |
| 45 | + ], |
| 46 | + }; |
| 47 | + |
| 48 | + server.use( |
| 49 | + http.get(`${apiBaseUrl}/API_GetComments.php`, () => |
| 50 | + HttpResponse.json(mockResponse) |
| 51 | + ) |
| 52 | + ); |
| 53 | + |
| 54 | + // ACT |
| 55 | + const response = await getComments(authorization, { |
| 56 | + identifier: "xelnia", |
| 57 | + }); |
| 58 | + |
| 59 | + // ASSERT |
| 60 | + const expectedResponse: CommentsResponse = { |
| 61 | + count: 2, |
| 62 | + total: 2, |
| 63 | + results: [ |
| 64 | + { |
| 65 | + user: "PlayTester", |
| 66 | + submitted: "2024-07-31T11:22:23.000000Z", |
| 67 | + commentText: "Comment 1", |
| 68 | + }, |
| 69 | + { |
| 70 | + user: "PlayTester", |
| 71 | + submitted: "2024-07-31T11:22:23.000000Z", |
| 72 | + commentText: "Comment 2", |
| 73 | + }, |
| 74 | + ], |
| 75 | + }; |
| 76 | + |
| 77 | + expect(response).toEqual(expectedResponse); |
| 78 | + }); |
| 79 | + |
| 80 | + it("retrieves the comments on an game", async () => { |
| 81 | + // ARRANGE |
| 82 | + const authorization = buildAuthorization({ |
| 83 | + username: "mockUserName", |
| 84 | + webApiKey: "mockWebApiKey", |
| 85 | + }); |
| 86 | + |
| 87 | + const mockResponse: GetCommentsResponse = { |
| 88 | + Count: 2, |
| 89 | + Total: 2, |
| 90 | + Results: [ |
| 91 | + { |
| 92 | + User: "PlayTester", |
| 93 | + Submitted: "2024-07-31T11:22:23.000000Z", |
| 94 | + CommentText: "Comment 1", |
| 95 | + }, |
| 96 | + { |
| 97 | + User: "PlayTester", |
| 98 | + Submitted: "2024-07-31T11:22:23.000000Z", |
| 99 | + CommentText: "Comment 2", |
| 100 | + }, |
| 101 | + ], |
| 102 | + }; |
| 103 | + |
| 104 | + server.use( |
| 105 | + http.get(`${apiBaseUrl}/API_GetComments.php`, () => |
| 106 | + HttpResponse.json(mockResponse) |
| 107 | + ) |
| 108 | + ); |
| 109 | + |
| 110 | + // ACT |
| 111 | + const response = await getComments(authorization, { |
| 112 | + identifier: 321_865, |
| 113 | + kind: "game", |
| 114 | + }); |
| 115 | + |
| 116 | + // ASSERT |
| 117 | + const expectedResponse: CommentsResponse = { |
| 118 | + count: 2, |
| 119 | + total: 2, |
| 120 | + results: [ |
| 121 | + { |
| 122 | + user: "PlayTester", |
| 123 | + submitted: "2024-07-31T11:22:23.000000Z", |
| 124 | + commentText: "Comment 1", |
| 125 | + }, |
| 126 | + { |
| 127 | + user: "PlayTester", |
| 128 | + submitted: "2024-07-31T11:22:23.000000Z", |
| 129 | + commentText: "Comment 2", |
| 130 | + }, |
| 131 | + ], |
| 132 | + }; |
| 133 | + |
| 134 | + expect(response).toEqual(expectedResponse); |
| 135 | + }); |
| 136 | + |
| 137 | + it("retrieves the comments on an achievement, respecting count", async () => { |
| 138 | + // ARRANGE |
| 139 | + const authorization = buildAuthorization({ |
| 140 | + username: "mockUserName", |
| 141 | + webApiKey: "mockWebApiKey", |
| 142 | + }); |
| 143 | + |
| 144 | + const mockResponse: GetCommentsResponse = { |
| 145 | + Count: 2, |
| 146 | + Total: 4, |
| 147 | + Results: [ |
| 148 | + { |
| 149 | + User: "PlayTester", |
| 150 | + Submitted: "2024-07-31T11:22:23.000000Z", |
| 151 | + CommentText: "Comment 1", |
| 152 | + }, |
| 153 | + { |
| 154 | + User: "PlayTester", |
| 155 | + Submitted: "2024-07-31T11:22:23.000000Z", |
| 156 | + CommentText: "Comment 2", |
| 157 | + }, |
| 158 | + ], |
| 159 | + }; |
| 160 | + |
| 161 | + server.use( |
| 162 | + http.get(`${apiBaseUrl}/API_GetComments.php`, () => |
| 163 | + HttpResponse.json(mockResponse) |
| 164 | + ) |
| 165 | + ); |
| 166 | + |
| 167 | + // ACT |
| 168 | + const response = await getComments(authorization, { |
| 169 | + identifier: 321_865, |
| 170 | + count: 2, |
| 171 | + kind: "achievement", |
| 172 | + }); |
| 173 | + |
| 174 | + // ASSERT |
| 175 | + const expectedResponse: CommentsResponse = { |
| 176 | + count: 2, |
| 177 | + total: 4, |
| 178 | + results: [ |
| 179 | + { |
| 180 | + user: "PlayTester", |
| 181 | + submitted: "2024-07-31T11:22:23.000000Z", |
| 182 | + commentText: "Comment 1", |
| 183 | + }, |
| 184 | + { |
| 185 | + user: "PlayTester", |
| 186 | + submitted: "2024-07-31T11:22:23.000000Z", |
| 187 | + commentText: "Comment 2", |
| 188 | + }, |
| 189 | + ], |
| 190 | + }; |
| 191 | + |
| 192 | + expect(response).toEqual(expectedResponse); |
| 193 | + }); |
| 194 | + |
| 195 | + it("retrieves the comments on an game, respecting offset", async () => { |
| 196 | + // ARRANGE |
| 197 | + const authorization = buildAuthorization({ |
| 198 | + username: "mockUserName", |
| 199 | + webApiKey: "mockWebApiKey", |
| 200 | + }); |
| 201 | + |
| 202 | + const mockResponse: GetCommentsResponse = { |
| 203 | + Count: 1, |
| 204 | + Total: 2, |
| 205 | + Results: [ |
| 206 | + { |
| 207 | + User: "PlayTester", |
| 208 | + Submitted: "2024-07-31T11:22:23.000000Z", |
| 209 | + CommentText: "Comment 2", |
| 210 | + }, |
| 211 | + ], |
| 212 | + }; |
| 213 | + |
| 214 | + server.use( |
| 215 | + http.get(`${apiBaseUrl}/API_GetComments.php`, () => |
| 216 | + HttpResponse.json(mockResponse) |
| 217 | + ) |
| 218 | + ); |
| 219 | + |
| 220 | + // ACT |
| 221 | + const response = await getComments(authorization, { |
| 222 | + identifier: 321_865, |
| 223 | + offset: 1, |
| 224 | + kind: "game", |
| 225 | + }); |
| 226 | + |
| 227 | + // ASSERT |
| 228 | + const expectedResponse: CommentsResponse = { |
| 229 | + count: 1, |
| 230 | + total: 2, |
| 231 | + results: [ |
| 232 | + { |
| 233 | + user: "PlayTester", |
| 234 | + submitted: "2024-07-31T11:22:23.000000Z", |
| 235 | + commentText: "Comment 2", |
| 236 | + }, |
| 237 | + ], |
| 238 | + }; |
| 239 | + |
| 240 | + expect(response).toEqual(expectedResponse); |
| 241 | + }); |
| 242 | + |
| 243 | + it("warns the developer when they don't specify kind for achievements/games", async () => { |
| 244 | + // ARRANGE |
| 245 | + const authorization = buildAuthorization({ |
| 246 | + username: "mockUserName", |
| 247 | + webApiKey: "mockWebApiKey", |
| 248 | + }); |
| 249 | + |
| 250 | + const mockResponse: GetCommentsResponse = { |
| 251 | + Count: 1, |
| 252 | + Total: 2, |
| 253 | + Results: [ |
| 254 | + { |
| 255 | + User: "PlayTester", |
| 256 | + Submitted: "2024-07-31T11:22:23.000000Z", |
| 257 | + CommentText: "Comment 2", |
| 258 | + }, |
| 259 | + ], |
| 260 | + }; |
| 261 | + |
| 262 | + server.use( |
| 263 | + http.get(`${apiBaseUrl}/API_GetComments.php`, () => |
| 264 | + HttpResponse.json(mockResponse) |
| 265 | + ) |
| 266 | + ); |
| 267 | + |
| 268 | + // ACT |
| 269 | + const response = getComments(authorization, { |
| 270 | + identifier: 321_865, |
| 271 | + offset: 1, |
| 272 | + }); |
| 273 | + |
| 274 | + // ASSERT |
| 275 | + await expect(response).rejects.toThrow(TypeError); |
| 276 | + }); |
| 277 | +}); |
0 commit comments