Skip to content

Commit

Permalink
fix: test titles
Browse files Browse the repository at this point in the history
  • Loading branch information
sounmind committed Nov 14, 2024
1 parent c8141ed commit 48d414a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
23 changes: 10 additions & 13 deletions src/api/services/process/processService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { createProcessService } from "./processService";

const processService = createProcessService(mockConfig);

test("initialize members correctly", () => {
test("initialize members", () => {
// Act
const result = processService.analyzeMemberInfo(mockMembers, []);
const result = processService.getMembers(mockMembers, []);

// Assert
expect(result.length).toBe(mockMembers.length);
Expand All @@ -21,9 +21,9 @@ test("initialize members correctly", () => {
});
});

test("processService should calculate submissions and progress correctly", () => {
test("calculate submissions and progress", () => {
// Act
const result = processService.analyzeMemberInfo(mockMembers, mockSubmissions);
const result = processService.getMembers(mockMembers, mockSubmissions);

// Assert
const algoInfo = result.find((m) => m.id === "algo")!; // 2
Expand All @@ -34,7 +34,7 @@ test("processService should calculate submissions and progress correctly", () =>
expect(daleInfo.progress).toBe(25); // 1/4 * 100
});

test("processService should handle duplicate problem submissions", () => {
test("remove duplicate problem submissions", () => {
// Arrange
const duplicateSubmissions = [
{
Expand All @@ -50,17 +50,14 @@ test("processService should handle duplicate problem submissions", () => {
];

// Act
const result = processService.analyzeMemberInfo(
mockMembers,
duplicateSubmissions,
);
const result = processService.getMembers(mockMembers, duplicateSubmissions);

// Assert
const algo = result.find((m) => m.id === "algo")!;
expect(algo.solvedProblems.length).toBe(1); // duplicates should be ignored
});

test("processService should assign correct grades based on submissions", () => {
test("assign correct grades based on submissions", () => {
// Arrange
const submissions = [
// algo
Expand All @@ -72,7 +69,7 @@ test("processService should assign correct grades based on submissions", () => {
];

// Act
const result = processService.analyzeMemberInfo(mockMembers, submissions);
const result = processService.getMembers(mockMembers, submissions);

// Assert
const algoInfo = result.find((m) => m.id === "algo")!;
Expand All @@ -83,7 +80,7 @@ test("processService should assign correct grades based on submissions", () => {
expect(daleInfo.grade).toBe(Grades.SPROUT); // large or equal to 1
});

test("processService should calculate correct progress percentages", () => {
test("calculate correct progress percentages", () => {
// Arrange
const submissions = Array.from({ length: 4 }, (_, i) => ({
memberId: "algo",
Expand All @@ -92,7 +89,7 @@ test("processService should calculate correct progress percentages", () => {
}));

// Act
const result = processService.analyzeMemberInfo(mockMembers, submissions);
const result = processService.getMembers(mockMembers, submissions);

// Assert
const algoInfo = result.find((m) => m.id === "algo")!;
Expand Down
5 changes: 1 addition & 4 deletions src/api/services/process/processService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import type { Member, MemberIdentity, Submission } from "../common/types";

export function createProcessService(config: Config) {
return {
analyzeMemberInfo(
members: MemberIdentity[],
submissions: Submission[],
): Member[] {
getMembers(members: MemberIdentity[], submissions: Submission[]): Member[] {
const memberMap = initializeMemberMap(members);

updateSubmissions(memberMap, submissions);
Expand Down
23 changes: 10 additions & 13 deletions src/api/services/store/storeService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getMembers } from "./storeService";
// Mock services
const mockFetchMembers = vi.fn();
const mockFetchSubmissions = vi.fn();
const mockAnalyzeMemberInfo = vi.fn();
const mockGetMembers = vi.fn();

vi.mock("../fetch/fetchService");
vi.mocked(createFetchService).mockReturnValue({
Expand All @@ -17,47 +17,47 @@ vi.mocked(createFetchService).mockReturnValue({

vi.mock("../process/processService");
vi.mocked(createProcessService).mockReturnValue({
analyzeMemberInfo: mockAnalyzeMemberInfo,
getMembers: mockGetMembers,
});

beforeEach(() => {
vi.clearAllMocks();

mockFetchMembers.mockResolvedValue([]);
mockFetchSubmissions.mockResolvedValue([]);
mockAnalyzeMemberInfo.mockReturnValue(mockMembers);
mockGetMembers.mockReturnValue(mockMembers);
});

test("should fetch and process data correctly", async () => {
test("fetch and process data", async () => {
// Act
const result = await getMembers();

// Assert
expect(mockFetchMembers).toHaveBeenCalledTimes(1);
expect(mockFetchSubmissions).toHaveBeenCalledTimes(1);
expect(mockAnalyzeMemberInfo).toHaveBeenCalledTimes(1);
expect(mockGetMembers).toHaveBeenCalledTimes(1);
expect(result).toEqual(mockMembers);
});

test("getMembers should throw error when fetch fails", async () => {
test("getMembers throws error when fetch fails", async () => {
// Arrange
mockFetchMembers.mockRejectedValue(new Error("Fetch failed"));

// Act & Assert
await expect(getMembers()).rejects.toThrow("Fetch failed");
});

test("getMembers should handle process service errors", async () => {
test("getMembers handles process service errors", async () => {
// Arrange
mockAnalyzeMemberInfo.mockImplementation(() => {
mockGetMembers.mockImplementation(() => {
throw new Error("Process failed");
});

// Act & Assert
await expect(getMembers()).rejects.toThrow("Process failed");
});

test("getMembers should pass correct parameters to process service", async () => {
test("getMembers called with correct parameters to process service", async () => {
// Arrange
const mockMembers = [{ id: "1", name: "Test" }];
const mockSubmissions = [{ id: "1", score: 100 }];
Expand All @@ -68,8 +68,5 @@ test("getMembers should pass correct parameters to process service", async () =>
await getMembers();

// Assert
expect(mockAnalyzeMemberInfo).toHaveBeenCalledWith(
mockMembers,
mockSubmissions,
);
expect(mockGetMembers).toHaveBeenCalledWith(mockMembers, mockSubmissions);
});
4 changes: 2 additions & 2 deletions src/api/services/store/storeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export async function getMembers(): Promise<Member[]> {
const fetchService = createFetchService(CONFIG);
const processService = createProcessService(CONFIG);

const [members, submissions] = await Promise.all([
const [memberIdentities, submissions] = await Promise.all([
fetchService.fetchMembers(),
fetchService.fetchSubmissions(CONFIG.study.repository),
]);

return processService.analyzeMemberInfo(members, submissions);
return processService.getMembers(memberIdentities, submissions);
}

0 comments on commit 48d414a

Please sign in to comment.