diff --git a/packages/view/src/components/Statistics/AuthorBarChart/AuthorBarChart.util.test.ts b/packages/view/src/components/Statistics/AuthorBarChart/AuthorBarChart.util.test.ts index 57bec39f..37f60ef7 100644 --- a/packages/view/src/components/Statistics/AuthorBarChart/AuthorBarChart.util.test.ts +++ b/packages/view/src/components/Statistics/AuthorBarChart/AuthorBarChart.util.test.ts @@ -1,7 +1,8 @@ -import type { ClusterNode } from "types"; +import type { ClusterNode, CommitNode } from "types"; import type { Commit } from "types/Commit"; -import { getDataByAuthor } from "./AuthorBarChart.util"; +import { convertNumberFormat, getDataByAuthor, sortDataByAuthor, sortDataByName } from "./AuthorBarChart.util"; +import type { AuthorDataType } from "./AuthorBarChart.type"; describe("getDataByAuthor", () => { it("should return empty array if no data is provided", () => { @@ -87,6 +88,145 @@ describe("getDataByAuthor", () => { insertion: 3, deletion: 2, }, - ]); + ] as AuthorDataType[]); + }); +}); + +describe("sortDataByName", () => { + it("should return 1 when nameA is lexicographically smaller than nameB", () => { + const result = sortDataByName("apple", "banana"); + expect(result).toBe(1); + }); + + it("should return -1 when nameA is lexicographically larger than nameB", () => { + const result = sortDataByName("banana", "apple"); + expect(result).toBe(-1); + }); + + it("should return 0 when nameA is equal to nameB", () => { + const result = sortDataByName("apple", "APPLE"); + expect(result).toBe(0); + }); +}); + +describe("convertNumberFormat", () => { + it("should return the number as a string if it's between 0 and 1", () => { + const result = convertNumberFormat(0.5); + expect(result).toBe("0.5"); + }); + + it("should format numbers greater than 1", () => { + const result = convertNumberFormat(1000); + expect(result).toBe("1k"); + }); +}); + +describe("sortDataByAuthor", () => { + it("should filter clusters based on author names", () => { + const fakeData: ClusterNode[] = [ + { + nodeTypeName: "CLUSTER", + commitNodeList: [ + { + nodeTypeName: "COMMIT", + commit: { + id: "1", + parentIds: ["0"], + author: { names: ["author1"] }, + committer: { names: ["author1"] }, + authorDate: "2024-01-01T00:00:00Z", + commitDate: "2024-01-01T00:00:00Z", + diffStatistics: { + insertions: 5, + deletions: 3, + }, + message: "Initial commit", + } as CommitNode["commit"], + seq: 1, + clusterId: 101, + }, + { + nodeTypeName: "COMMIT", + commit: { + id: "2", + parentIds: ["1"], + author: { names: ["author2"] }, + committer: { names: ["author2"] }, + authorDate: "2024-01-02T00:00:00Z", + commitDate: "2024-01-02T00:00:00Z", + diffStatistics: { + insertions: 2, + deletions: 1, + }, + message: "Second commit", + } as CommitNode["commit"], + seq: 2, + clusterId: 101, + }, + ], + }, + ]; + + const names = ["author1"]; + const result = sortDataByAuthor(fakeData, names); + + expect(result).toEqual([ + { + nodeTypeName: "CLUSTER", + commitNodeList: [ + { + nodeTypeName: "COMMIT", + commit: { + id: "1", + parentIds: ["0"], + author: { names: ["author1"] }, + committer: { names: ["author1"] }, + authorDate: "2024-01-01T00:00:00Z", + commitDate: "2024-01-01T00:00:00Z", + diffStatistics: { + insertions: 5, + deletions: 3, + }, + message: "Initial commit", + }, + seq: 1, + clusterId: 101, + }, + ], + }, + ] as ClusterNode[]); + }); + + it("should return an empty array if no author matches", () => { + const fakeData: ClusterNode[] = [ + { + nodeTypeName: "CLUSTER", + commitNodeList: [ + { + nodeTypeName: "COMMIT", + commit: { + id: "1", + parentIds: ["0"], + author: { names: ["author1"] }, + committer: { names: ["author1"] }, + authorDate: "2024-01-01T00:00:00Z", + commitDate: "2024-01-01T00:00:00Z", + diffStatistics: { + insertions: 5, + deletions: 3, + }, + message: "Initial commit", + } as CommitNode["commit"], + seq: 1, + clusterId: 101, + }, + ], + }, + ]; + + const names = ["nonexistentAuthor"]; + const result = sortDataByAuthor(fakeData, names); + + expect(result).toEqual([]); }); });