|
| 1 | +import nock from "nock"; |
| 2 | + |
| 3 | +import type { |
| 4 | + AuthorizationPayload, |
| 5 | + GetUserFriendsRequestsResponse |
| 6 | +} from "../models"; |
| 7 | +import { getUserFriendsRequests } from "./getUserFriendsRequests"; |
| 8 | +import { USER_BASE_URL } from "./USER_BASE_URL"; |
| 9 | + |
| 10 | +describe("Function: getUserFriendsRequests", () => { |
| 11 | + afterEach(() => { |
| 12 | + nock.cleanAll(); |
| 13 | + }); |
| 14 | + |
| 15 | + it("is defined #sanity", () => { |
| 16 | + // ASSERT |
| 17 | + expect(getUserFriendsRequests).toBeDefined(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("retrieves the received friend requests for the authenticated user", async () => { |
| 21 | + // ARRANGE |
| 22 | + const mockAuthorization: AuthorizationPayload = { |
| 23 | + accessToken: "mockAccessToken" |
| 24 | + }; |
| 25 | + |
| 26 | + const mockResponse: GetUserFriendsRequestsResponse = { |
| 27 | + receivedRequests: ["2984038888603282554", "8403439712302084350"], |
| 28 | + totalItemCount: 2 |
| 29 | + }; |
| 30 | + |
| 31 | + const baseUrlObj = new URL(USER_BASE_URL); |
| 32 | + const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; |
| 33 | + const basePath = baseUrlObj.pathname; |
| 34 | + |
| 35 | + nock(baseUrl) |
| 36 | + .get(`${basePath}/me/friends/receivedRequests`) |
| 37 | + .query(true) |
| 38 | + .reply(200, mockResponse); |
| 39 | + |
| 40 | + // ACT |
| 41 | + const response = await getUserFriendsRequests(mockAuthorization); |
| 42 | + |
| 43 | + // ASSERT |
| 44 | + expect(response).toEqual(mockResponse); |
| 45 | + }); |
| 46 | + |
| 47 | + it("throws an error if we receive a response containing an `error` object", async () => { |
| 48 | + // ARRANGE |
| 49 | + const mockAuthorization: AuthorizationPayload = { |
| 50 | + accessToken: "mockAccessToken" |
| 51 | + }; |
| 52 | + |
| 53 | + const mockResponse = { |
| 54 | + error: { |
| 55 | + referenceId: "d71bd8ff-5f63-11ec-87da-d5dfd3bc6e67", |
| 56 | + code: 2_281_604, |
| 57 | + message: "Not Found" |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + nock("https://m.np.playstation.com") |
| 62 | + .get("/api/userProfile/v1/internal/users/me/friends/receivedRequests") |
| 63 | + .query(true) |
| 64 | + .reply(200, mockResponse); |
| 65 | + |
| 66 | + // ASSERT |
| 67 | + await expect( |
| 68 | + getUserFriendsRequests(mockAuthorization) |
| 69 | + ).rejects.toThrow(); |
| 70 | + }); |
| 71 | +}); |
0 commit comments