|
| 1 | +import nock from "nock"; |
| 2 | + |
| 3 | +import type { AuthorizationPayload, PurchasedGamesResponse } from "../models"; |
| 4 | +import { getPurchasedGames } from "./getPurchasedGames"; |
| 5 | +import { GRAPHQL_BASE_URL } from "./GRAPHQL_BASE_URL"; |
| 6 | + |
| 7 | +const accessToken = "mockAccessToken"; |
| 8 | + |
| 9 | +describe("Function: getPurchasedGames", () => { |
| 10 | + afterEach(() => { |
| 11 | + nock.cleanAll(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("is defined #sanity", () => { |
| 15 | + // ASSERT |
| 16 | + expect(getPurchasedGames).toBeDefined(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("retrieves purchased games for the user", async () => { |
| 20 | + // ARRANGE |
| 21 | + const mockAuthorization: AuthorizationPayload = { |
| 22 | + accessToken |
| 23 | + }; |
| 24 | + |
| 25 | + const mockResponse: PurchasedGamesResponse = { |
| 26 | + data: { |
| 27 | + purchasedTitlesRetrieve: { |
| 28 | + __typename: "GameList", |
| 29 | + games: [ |
| 30 | + { |
| 31 | + __typename: "GameLibraryTitle", |
| 32 | + conceptId: "203715", |
| 33 | + entitlementId: "EP2002-CUSA01433_00-ROCKETLEAGUEEU01", |
| 34 | + image: { |
| 35 | + __typename: "Media", |
| 36 | + url: "https://image.api.playstation.com/gs2-sec/appkgo/prod/CUSA01433_00/7/i_5c5e430a49994f22df5fd81f446ead7b6ae45027af490b415fe4e744a9918e4c/i/icon0.png" |
| 37 | + }, |
| 38 | + isActive: true, |
| 39 | + isDownloadable: true, |
| 40 | + isPreOrder: false, |
| 41 | + membership: "NONE", |
| 42 | + name: "Rocket League®", |
| 43 | + platform: "PS4", |
| 44 | + productId: "EP2002-CUSA01433_00-ROCKETLEAGUEEU01", |
| 45 | + titleId: "CUSA01433_00" |
| 46 | + } |
| 47 | + ] |
| 48 | + } |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + const baseUrlObj = new URL(GRAPHQL_BASE_URL); |
| 53 | + const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; |
| 54 | + const basePath = baseUrlObj.pathname; |
| 55 | + |
| 56 | + // ... we need to use a nock matcher to verify the query parameters ... |
| 57 | + const expectedVariables = JSON.stringify({ |
| 58 | + isActive: true, |
| 59 | + platform: ["ps4", "ps5"], |
| 60 | + size: 24, |
| 61 | + start: 0, |
| 62 | + sortBy: "ACTIVE_DATE", |
| 63 | + sortDirection: "desc" |
| 64 | + }); |
| 65 | + const expectedExtensions = JSON.stringify({ |
| 66 | + persistedQuery: { |
| 67 | + version: 1, |
| 68 | + sha256Hash: |
| 69 | + "827a423f6a8ddca4107ac01395af2ec0eafd8396fc7fa204aaf9b7ed2eefa168" |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + const mockScope = nock(baseUrl) |
| 74 | + .get(basePath) |
| 75 | + .query((params) => { |
| 76 | + expect(params.operationName).toEqual("getPurchasedGameList"); |
| 77 | + expect(params.variables).toEqual(expectedVariables); |
| 78 | + expect(params.extensions).toEqual(expectedExtensions); |
| 79 | + return true; |
| 80 | + }) |
| 81 | + .matchHeader("authorization", `Bearer ${accessToken}`) |
| 82 | + .reply(200, mockResponse); |
| 83 | + |
| 84 | + // ACT |
| 85 | + const response = await getPurchasedGames(mockAuthorization); |
| 86 | + |
| 87 | + // ASSERT |
| 88 | + expect(response).toEqual(mockResponse); |
| 89 | + expect(mockScope.isDone()).toBeTruthy(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("retrieves purchased games with custom options", async () => { |
| 93 | + // ARRANGE |
| 94 | + const mockAuthorization: AuthorizationPayload = { |
| 95 | + accessToken |
| 96 | + }; |
| 97 | + |
| 98 | + const mockResponse: PurchasedGamesResponse = { |
| 99 | + data: { |
| 100 | + purchasedTitlesRetrieve: { |
| 101 | + __typename: "GameList", |
| 102 | + games: [] |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + const baseUrlObj = new URL(GRAPHQL_BASE_URL); |
| 108 | + const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; |
| 109 | + const basePath = baseUrlObj.pathname; |
| 110 | + |
| 111 | + const expectedVariables = JSON.stringify({ |
| 112 | + isActive: false, |
| 113 | + platform: ["ps4", "ps5"], |
| 114 | + size: 50, |
| 115 | + start: 0, |
| 116 | + sortBy: "ACTIVE_DATE", |
| 117 | + sortDirection: "desc" |
| 118 | + }); |
| 119 | + const expectedExtensions = JSON.stringify({ |
| 120 | + persistedQuery: { |
| 121 | + version: 1, |
| 122 | + sha256Hash: |
| 123 | + "827a423f6a8ddca4107ac01395af2ec0eafd8396fc7fa204aaf9b7ed2eefa168" |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + const mockScope = nock(baseUrl) |
| 128 | + .get(basePath) |
| 129 | + .query((params) => { |
| 130 | + expect(params.operationName).toEqual("getPurchasedGameList"); |
| 131 | + expect(params.variables).toEqual(expectedVariables); |
| 132 | + expect(params.extensions).toEqual(expectedExtensions); |
| 133 | + return true; |
| 134 | + }) |
| 135 | + .matchHeader("authorization", `Bearer ${accessToken}`) |
| 136 | + .reply(200, mockResponse); |
| 137 | + |
| 138 | + // ACT |
| 139 | + const response = await getPurchasedGames(mockAuthorization, { |
| 140 | + isActive: false, |
| 141 | + size: 50, |
| 142 | + sortBy: "ACTIVE_DATE" |
| 143 | + }); |
| 144 | + |
| 145 | + // ASSERT |
| 146 | + expect(response).toEqual(mockResponse); |
| 147 | + expect(mockScope.isDone()).toBeTruthy(); |
| 148 | + }); |
| 149 | + |
| 150 | + it("throws an error if response data is null", async () => { |
| 151 | + // ARRANGE |
| 152 | + const mockAuthorization: AuthorizationPayload = { |
| 153 | + accessToken |
| 154 | + }; |
| 155 | + |
| 156 | + const mockErrorResponse = { |
| 157 | + data: null |
| 158 | + }; |
| 159 | + |
| 160 | + const baseUrlObj = new URL(GRAPHQL_BASE_URL); |
| 161 | + const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; |
| 162 | + const basePath = baseUrlObj.pathname; |
| 163 | + |
| 164 | + nock(baseUrl) |
| 165 | + .get(basePath) |
| 166 | + .query(true) |
| 167 | + .matchHeader("authorization", `Bearer ${accessToken}`) |
| 168 | + .reply(200, mockErrorResponse); |
| 169 | + |
| 170 | + // ASSERT |
| 171 | + await expect(getPurchasedGames(mockAuthorization)).rejects.toThrowError( |
| 172 | + JSON.stringify(mockErrorResponse) |
| 173 | + ); |
| 174 | + }); |
| 175 | + |
| 176 | + it("throws an error if purchasedTitlesRetrieve is null", async () => { |
| 177 | + // ARRANGE |
| 178 | + const mockAuthorization: AuthorizationPayload = { |
| 179 | + accessToken |
| 180 | + }; |
| 181 | + |
| 182 | + const mockErrorResponse = { |
| 183 | + data: { |
| 184 | + purchasedTitlesRetrieve: null |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + const baseUrlObj = new URL(GRAPHQL_BASE_URL); |
| 189 | + const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; |
| 190 | + const basePath = baseUrlObj.pathname; |
| 191 | + |
| 192 | + nock(baseUrl) |
| 193 | + .get(basePath) |
| 194 | + .query(true) |
| 195 | + .matchHeader("authorization", `Bearer ${accessToken}`) |
| 196 | + .reply(200, mockErrorResponse); |
| 197 | + |
| 198 | + // ASSERT |
| 199 | + await expect(getPurchasedGames(mockAuthorization)).rejects.toThrowError( |
| 200 | + JSON.stringify(mockErrorResponse) |
| 201 | + ); |
| 202 | + }); |
| 203 | +}); |
0 commit comments