Skip to content

Commit

Permalink
refactor: convert Grade to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
sounmind committed Nov 14, 2024
1 parent 48d414a commit f124cf8
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/api/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Grade } from "../types";
import type { Grade } from "../services/common/types";
import type { Config } from "./types";

export const CONFIG: Config = {
Expand Down
2 changes: 1 addition & 1 deletion src/api/config/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Grade } from "../types";
import type { Grade } from "../services/common/types";

export type StudyConfig = {
organization: string;
Expand Down
3 changes: 1 addition & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export type { Submission } from "./services/common/types";
export type { Grade, Submission } from "./services/common/types";
export { getMembers } from "./services/store/storeService";
export type { Grade } from "./types";
7 changes: 3 additions & 4 deletions src/api/services/common/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { StudyConfig } from "../../config/types";
import type {
GitHubTeam,
GitHubMember,
GitHubTeam,
GitHubTree,
} from "../../infra/gitHub/types";
import { Grades, type Grade } from "../../types";
import type { Submission } from "./types";
import { Grade, Submission } from "./types";

export const mockStudyConfig: StudyConfig = {
organization: "test-org",
Expand Down Expand Up @@ -158,7 +157,7 @@ export const mockMembers = mockGitHubMembers.map((member) => ({
{ memberId: member.login, problemTitle: "problem2", language: "ts" },
],
progress: 50,
grade: member.login === "algo" ? Grades.BIG_TREE : Grades.SPROUT,
grade: member.login === "algo" ? Grade.BIG_TREE : Grade.SPROUT,
}));

export const mockSubmissions: Submission[] = [
Expand Down
9 changes: 8 additions & 1 deletion src/api/services/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export type Cohort = number;

export enum Grade {
SEED = "SEED",
SPROUT = "SPROUT",
SMALL_TREE = "SMALL_TREE",
BIG_TREE = "BIG_TREE",
}

export type MemberIdentity = {
id: string; // lowercase
name: string;
Expand All @@ -22,7 +29,7 @@ export interface Member {
profileUrl?: string;
/** Unit: % */
progress: number;
grade: "SEED" | "SPROUT" | "SMALL_TREE" | "BIG_TREE";
grade: Grade;
/** Example: ["best-time-to-buy-and-sell-stock", "3sum", "climbing-stairs", ...] */
solvedProblems: string[];
}
10 changes: 5 additions & 5 deletions src/api/services/process/processService.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "vitest";
import { Grades } from "../../types";
import { mockConfig, mockMembers, mockSubmissions } from "../common/fixtures";
import { Grade } from "../common/types";
import { createProcessService } from "./processService";

const processService = createProcessService(mockConfig);
Expand All @@ -16,7 +16,7 @@ test("initialize members", () => {
...mockMembers.find((m) => m.id === member.id),
solvedProblems: [],
progress: 0,
grade: Grades.SEED,
grade: Grade.SEED,
});
});
});
Expand Down Expand Up @@ -76,8 +76,8 @@ test("assign correct grades based on submissions", () => {
const daleInfo = result.find((m) => m.id === "dale")!;

// mockConfig gradeThresholds: BIG_TREE(3), SMALL_TREE(2), SPROUT(1), SEED(0)
expect(algoInfo.grade).toBe(Grades.BIG_TREE); // large or equal to 3
expect(daleInfo.grade).toBe(Grades.SPROUT); // large or equal to 1
expect(algoInfo.grade).toBe(Grade.BIG_TREE); // large or equal to 3
expect(daleInfo.grade).toBe(Grade.SPROUT); // large or equal to 1
});

test("calculate correct progress percentages", () => {
Expand All @@ -94,5 +94,5 @@ test("calculate correct progress percentages", () => {
// Assert
const algoInfo = result.find((m) => m.id === "algo")!;
expect(algoInfo.progress).toBe(100); // 4/4 * 100
expect(algoInfo.grade).toBe(Grades.BIG_TREE);
expect(algoInfo.grade).toBe(Grade.BIG_TREE);
});
12 changes: 8 additions & 4 deletions src/api/services/process/processService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { Config } from "../../config/types";
import { type Grade, Grades } from "../../types";
import type { Member, MemberIdentity, Submission } from "../common/types";
import {
Grade,
type Member,
type MemberIdentity,
type Submission,
} from "../common/types";

export function createProcessService(config: Config) {
return {
Expand All @@ -26,7 +30,7 @@ const initializeMemberMap = (
...member,
solvedProblems: [],
progress: 0,
grade: Grades.SEED,
grade: Grade.SEED,
};
});

Expand Down Expand Up @@ -81,5 +85,5 @@ const determineGrade = (
([, threshold]) => totalSubmissions >= threshold,
);

return grade ? grade[0] : Grades.SEED;
return grade ? grade[0] : Grade.SEED;
};
8 changes: 0 additions & 8 deletions src/api/types.ts

This file was deleted.

9 changes: 2 additions & 7 deletions src/hooks/useMembers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from "@faker-js/faker";
import { renderHook, waitFor } from "@testing-library/react";
import { expect, test, vi } from "vitest";

import type { Member } from "../api/services/common/types";
import { Grade, type Member } from "../api/services/common/types";
import useMembers from "./useMembers";

test("fetch member info successfully and update state", async () => {
Expand All @@ -12,12 +12,7 @@ test("fetch member info successfully and update state", async () => {
cohort: faker.number.int({ min: 1, max: 10 }),
profileUrl: faker.internet.url(),
progress: faker.number.int({ min: 0, max: 100 }),
grade: faker.helpers.arrayElement([
"SEED",
"SPROUT",
"SMALL_TREE",
"BIG_TREE",
]),
grade: faker.helpers.arrayElement(Object.values(Grade)),
solvedProblems: Array.from({ length: 5 }, () =>
faker.lorem.words(3).replaceAll(" ", "-"),
),
Expand Down

0 comments on commit f124cf8

Please sign in to comment.