Skip to content

Commit 82b1fdc

Browse files
committed
feat: add parameter for 'kind', and test for it missing when needed
1 parent 4e7e63e commit 82b1fdc

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

src/comments/getComments.test.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe("Function: getComments", () => {
7777
expect(response).toEqual(expectedResponse);
7878
});
7979

80-
it("retrieves the comments on an game or achievement", async () => {
80+
it("retrieves the comments on an game", async () => {
8181
// ARRANGE
8282
const authorization = buildAuthorization({
8383
username: "mockUserName",
@@ -110,6 +110,7 @@ describe("Function: getComments", () => {
110110
// ACT
111111
const response = await getComments(authorization, {
112112
identifier: 321_865,
113+
kind: 1,
113114
});
114115

115116
// ASSERT
@@ -133,7 +134,7 @@ describe("Function: getComments", () => {
133134
expect(response).toEqual(expectedResponse);
134135
});
135136

136-
it("retrieves the comments on an game or achievement, respecting count", async () => {
137+
it("retrieves the comments on an achievement, respecting count", async () => {
137138
// ARRANGE
138139
const authorization = buildAuthorization({
139140
username: "mockUserName",
@@ -167,6 +168,7 @@ describe("Function: getComments", () => {
167168
const response = await getComments(authorization, {
168169
identifier: 321_865,
169170
count: 2,
171+
kind: 2,
170172
});
171173

172174
// ASSERT
@@ -190,7 +192,7 @@ describe("Function: getComments", () => {
190192
expect(response).toEqual(expectedResponse);
191193
});
192194

193-
it("retrieves the comments on an game or achievement, respecting offset", async () => {
195+
it("retrieves the comments on an game, respecting offset", async () => {
194196
// ARRANGE
195197
const authorization = buildAuthorization({
196198
username: "mockUserName",
@@ -219,6 +221,7 @@ describe("Function: getComments", () => {
219221
const response = await getComments(authorization, {
220222
identifier: 321_865,
221223
offset: 1,
224+
kind: 1,
222225
});
223226

224227
// ASSERT
@@ -236,4 +239,39 @@ describe("Function: getComments", () => {
236239

237240
expect(response).toEqual(expectedResponse);
238241
});
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+
});
239277
});

src/comments/getComments.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import type { Comments, GetCommentsResponse } from "./models";
1818
* be a string (the username), and for game and achievement walls, this will be a
1919
* the ID of the object in question.
2020
*
21+
* @param payload.kind What kind of identifier was used. This corresponds to 1 for a game,
22+
* 2 for an achievement, and 3 for a user. Required if type is 1 or 2.
23+
*
2124
* @param payload.offset Defaults to 0. The number of entries to skip.
2225
*
2326
* @param payload.count Defaults to 50, has a max of 500.
@@ -56,11 +59,19 @@ import type { Comments, GetCommentsResponse } from "./models";
5659
*/
5760
export const getComments = async (
5861
authorization: AuthObject,
59-
payload: { identifier: ID | string; offset?: number; count?: number }
62+
payload: { identifier: ID; kind?: number; offset?: number; count?: number }
6063
): Promise<Comments> => {
61-
const { identifier, offset, count } = payload;
64+
const { identifier, kind, offset, count } = payload;
65+
66+
const queryParams: Record<string, number | string> = { i: identifier };
6267

63-
const queryParams: Record<string, number | string> = { i: identifier, t: 1 };
68+
if (kind) {
69+
queryParams.t = kind;
70+
} else if (typeof identifier === "number") {
71+
throw new TypeError(
72+
"'kind' must be specified when looking up an achievement or game."
73+
);
74+
}
6475

6576
if (offset) {
6677
queryParams.o = offset;

0 commit comments

Comments
 (0)