-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement displaying user scope depending on game manifest :)
- Loading branch information
Showing
8 changed files
with
94 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import useGame from "./useGame"; | ||
|
||
it("loads the game", async () => { | ||
const { result, waitForValueToChange } = renderHook(() => useGame()); | ||
|
||
await waitForValueToChange(() => result.current.data); | ||
expect(result.current.data).toEqual(require("../../../mocks/data/game.json")); | ||
}); | ||
|
||
it("returns an error when the game is not found", async () => { | ||
const { result, waitForValueToChange } = renderHook(() => useGame()); | ||
|
||
await waitForValueToChange(() => result.current.error); | ||
expect(result.current.error).toEqual( | ||
require("../../../mocks/data/game-not-found.json") | ||
); | ||
}); |
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,26 @@ | ||
import useSWR from "swr"; | ||
|
||
import { Game } from "../../types/Game"; | ||
import ApiError from "./helper/ApiError"; | ||
import config from "../../../config"; | ||
import errorAwareFetcher from "./helper/errorAwareFetcher"; | ||
import useApiUrl from "./useApiUrl"; | ||
|
||
interface GameApiResponse { | ||
data: Game; | ||
} | ||
|
||
/** | ||
* A React hook that retrieves the game manifest | ||
* | ||
* @returns The game manifest. May be null during initial load | ||
*/ | ||
const useGame = () => { | ||
const url = useApiUrl((gameId) => config.apiEndpoints.game(gameId)); | ||
return useSWR<GameApiResponse, ApiError>( | ||
() => url, | ||
(url) => errorAwareFetcher(() => fetch(url)) | ||
); | ||
}; | ||
|
||
export default useGame; |
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,8 @@ | ||
export interface Game { | ||
type: "game"; | ||
id: string; | ||
attributes: { | ||
hasMessages: boolean; | ||
hasUserParameterScope: boolean; | ||
}; | ||
} |
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
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,9 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"id": "not-found", | ||
"status": 404, | ||
"title": "Unknown game" | ||
} | ||
] | ||
} |
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