Skip to content

Commit

Permalink
feat: mock API requests for leaderboard story
Browse files Browse the repository at this point in the history
  • Loading branch information
DaleSeo committed Nov 29, 2024
1 parent dcbaa88 commit 047a7bd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/api/infra/gitHub/gitHubClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import type {
} from "./types";

export function createGitHubClient(config: GitHubConfig) {
const request = async (url: string, token: string): Promise<unknown> => {
const response = await fetch(url, {
headers: {
Accept: config.mediaType,
Authorization: `token ${token}`,
},
});
const request = async (url: string, token?: string): Promise<unknown> => {
const headers: Record<string, string> = {
Accept: config.mediaType,
};

if (token) {
headers.Authorization = `token ${token}`;
}

const response = await fetch(url, { headers });

if (!response.ok) {
throw new Error(
Expand Down Expand Up @@ -48,7 +51,6 @@ export function createGitHubClient(config: GitHubConfig) {
): Promise<GitHubTree[]> =>
request(
`${config.baseUrl}/repos/${owner}/${repo}/git/trees/${treeSha}?recursive=${recursive}`,
config.token,
).then((response) => (response as GitHubTreeResponse).tree),
};
}
73 changes: 72 additions & 1 deletion src/components/Leaderboard/Leaderboard.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Meta, StoryObj } from "@storybook/react";
import { http, HttpResponse } from "msw";
import Leaderboard from "./Leaderboard";

const meta = {
Expand All @@ -7,4 +8,74 @@ const meta = {

export default meta;

export const Default: StoryObj<typeof meta> = {};
export const Default: StoryObj<typeof meta> = {
parameters: {
msw: {
handlers: [
http.get("https://api.github.com/orgs/DaleStudy/teams", () =>
HttpResponse.json([{ name: "leetcode01" }, { name: "leetcode02" }]),
),
http.get(
"https://api.github.com/orgs/DaleStudy/teams/leetcode01/members",
() =>
HttpResponse.json([
{
login: "sounmind",
avatar_url:
"https://avatars.githubusercontent.com/u/37020415?v=4",
},
{
login: "yolophg",
avatar_url:
"https://avatars.githubusercontent.com/u/38199103?v=4",
},
{
login: "SamTheKorean",
avatar_url:
"https://avatars.githubusercontent.com/u/104721736?v=4",
},
]),
),
http.get(
"https://api.github.com/orgs/DaleStudy/teams/leetcode02/members",
() =>
HttpResponse.json([
{
login: "Sunjae95",
avatar_url:
"https://avatars.githubusercontent.com/u/63578094?v=4",
},
{
login: "HC-kang",
id: 81678439,
node_id: "MDQ6VXNlcjgxNjc4NDM5",
avatar_url:
"https://avatars.githubusercontent.com/u/81678439?v=4",
gravatar_id: "",
url: "https://api.github.com/users/HC-kang",
html_url: "https://github.com/HC-kang",
followers_url: "https://api.github.com/users/HC-kang/followers",
following_url:
"https://api.github.com/users/HC-kang/following{/other_user}",
gists_url:
"https://api.github.com/users/HC-kang/gists{/gist_id}",
starred_url:
"https://api.github.com/users/HC-kang/starred{/owner}{/repo}",
subscriptions_url:
"https://api.github.com/users/HC-kang/subscriptions",
organizations_url: "https://api.github.com/users/HC-kang/orgs",
repos_url: "https://api.github.com/users/HC-kang/repos",
events_url:
"https://api.github.com/users/HC-kang/events{/privacy}",
received_events_url:
"https://api.github.com/users/HC-kang/received_events",
type: "User",
user_view_type: "public",
site_admin: false,
},
]),
),
],
},
},
};

0 comments on commit 047a7bd

Please sign in to comment.