-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e9fa03
commit c47af11
Showing
7 changed files
with
173 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { http, HttpResponse } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
|
||
import { apiBaseUrl } from "../utils/internal"; | ||
import { buildAuthorization } from "../utils/public"; | ||
import { getUserProfile } from "./getUserProfile"; | ||
import type { GetUserProfileResponse } from "./models"; | ||
|
||
const server = setupServer(); | ||
|
||
describe("Function: getUserProfile", () => { | ||
// MSW Setup | ||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
it("is defined #sanity", () => { | ||
// ASSERT | ||
expect(getUserProfile).toBeDefined(); | ||
}); | ||
|
||
it("given a username, retrieves minimal user profile information about the user", async () => { | ||
// ARRANGE | ||
const authorization = buildAuthorization({ | ||
userName: "mockUserName", | ||
webApiKey: "mockWebApiKey" | ||
}); | ||
|
||
const mockResponse: GetUserProfileResponse = { | ||
User: "MaxMilyin", | ||
UserPic: "/UserPic/MaxMilyin.png", | ||
MemberSince: "2016-01-02 00:43:04", | ||
RichPresenceMsg: | ||
"Playing ~Hack~ 11th Annual Vanilla Level Design Contest, The", | ||
LastGameID: 19_504, | ||
ContribCount: 0, | ||
ContribYield: 0, | ||
TotalPoints: 399_597, | ||
TotalSoftcorePoints: 0, | ||
TotalTruePoints: 1_599_212, | ||
Permissions: 1, | ||
Untracked: 0, | ||
ID: 16_446, | ||
UserWallActive: 1, | ||
Motto: "Join me on Twitch! GameSquadSquad for live RA" | ||
}; | ||
|
||
server.use( | ||
http.get(`${apiBaseUrl}/API_GetUserProfile.php`, () => | ||
HttpResponse.json(mockResponse) | ||
) | ||
); | ||
|
||
// ACT | ||
const response = await getUserProfile(authorization, { | ||
userName: "WCopeland" | ||
}); | ||
|
||
// ASSERT | ||
expect(response).toEqual({ | ||
user: "MaxMilyin", | ||
userPic: "/UserPic/MaxMilyin.png", | ||
memberSince: "2016-01-02 00:43:04", | ||
richPresenceMsg: | ||
"Playing ~Hack~ 11th Annual Vanilla Level Design Contest, The", | ||
lastGameId: 19_504, | ||
contribCount: 0, | ||
contribYield: 0, | ||
totalPoints: 399_597, | ||
totalSoftcorePoints: 0, | ||
totalTruePoints: 1_599_212, | ||
permissions: 1, | ||
untracked: false, | ||
id: 16_446, | ||
userWallActive: true, | ||
motto: "Join me on Twitch! GameSquadSquad for live RA" | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { | ||
apiBaseUrl, | ||
buildRequestUrl, | ||
call, | ||
serializeProperties | ||
} from "../utils/internal"; | ||
import type { AuthObject } from "../utils/public"; | ||
import type { GetUserProfileResponse, UserProfile } from "./models"; | ||
|
||
/** | ||
* A call to this function will retrieve summary information about | ||
* a given user, targeted by username. | ||
* | ||
* @param authorization An object containing your userName and webApiKey. | ||
* This can be constructed with `buildAuthorization()`. | ||
* | ||
* @param payload.userName The user for which to retrieve the summary for. | ||
* | ||
* @example | ||
* ``` | ||
* const userSummary = await getUserProfile( | ||
* authorization, | ||
* { userName: "xelnia" } | ||
* ); | ||
* ``` | ||
* | ||
* @returns An object containing profile summary metadata about a target user. | ||
*/ | ||
export const getUserProfile = async ( | ||
authorization: AuthObject, | ||
payload: { | ||
userName: string; | ||
} | ||
): Promise<UserProfile> => { | ||
const { userName } = payload; | ||
|
||
const url = buildRequestUrl( | ||
apiBaseUrl, | ||
"/API_GetUserProfile.php", | ||
authorization, | ||
{ u: userName } | ||
); | ||
|
||
const rawResponse = await call<GetUserProfileResponse>({ url }); | ||
|
||
return serializeProperties(rawResponse, { | ||
shouldCastToNumbers: [ | ||
"TotalPoints", | ||
"TotalSoftcorePoints", | ||
"TotalTruePoints", | ||
"Permissions" | ||
], | ||
shouldMapToBooleans: ["Untracked", "UserWallActive"] | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface GetUserProfileResponse { | ||
User: string; | ||
UserPic: string; | ||
MemberSince: string; | ||
RichPresenceMsg: string; | ||
LastGameID: number; | ||
ContribCount: number; | ||
ContribYield: number; | ||
TotalPoints: number; | ||
TotalSoftcorePoints: number; | ||
TotalTruePoints: number; | ||
Permissions: number; | ||
Untracked: number; | ||
ID: number; | ||
UserWallActive: number; | ||
Motto: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface UserProfile { | ||
user: string; | ||
userPic: string; | ||
memberSince: string; | ||
richPresenceMsg: string; | ||
lastGameId: number; | ||
contribCount: number; | ||
contribYield: number; | ||
totalPoints: number; | ||
totalSoftcorePoints: number; | ||
totalTruePoints: number; | ||
permissions: number; | ||
untracked: boolean; | ||
id: number; | ||
userWallActive: boolean; | ||
motto: string; | ||
} |