Skip to content

Commit

Permalink
test(RankChart): fix test for getRanking
Browse files Browse the repository at this point in the history
`getRanking`にコメントとして結果の変数が満たしうる条件を残しているので、それらが満たされているかどうかをチェック
  • Loading branch information
ryoppippi committed Feb 5, 2025
1 parent db3af87 commit 42b0dd4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 147 deletions.
146 changes: 0 additions & 146 deletions src/components/RankChart/__snapshots__/utils.test.ts.snap

This file was deleted.

39 changes: 38 additions & 1 deletion src/components/RankChart/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,47 @@ describe("RankChart utils.ts", () => {
});

it("getRanking", () => {
expect(RANKING).toMatchSnapshot(); // Snapshotが大きいので別途ファイルに保存
const publishedFirst50Articles = getArticles({
isPublished: true,
}).slice(0, 50);
const ranking = utils.getRanking(publishedFirst50Articles);

/** ランクが1から始まっているか */
expect(ranking[0].rank).toBe(1);

/** 同じ記事数の場合は同じランクになるか.ソートされているか.上位10件のみで検証 */
Array.from({ length: 10 }).forEach((_, i) => {
const current = ranking[i];
const prev = ranking[i - 1];

/** どちらかが存在しない場合はスキップ */
if (!(current && prev)) {
return;
}

if (prev && prev.articleCount === current.articleCount) {
expect(current.rank).toBe(prev.rank);
} else {
expect(current.rank).toBeGreaterThan(prev.rank);
}
});

/** 記事数で降順ソートされているか */
expect(ranking).toEqual(
ranking.toSorted((a, b) => b.articleCount - a.articleCount),
);

/** 記事数の合計が50であるか */
const totalArticleCount = ranking.reduce(
(acc, { articleCount }) => acc + articleCount,
0,
);

expect(totalArticleCount).toBe(50);
});

it("get to 10 ranking using getTopNRanking", () => {
expect(TOP_10_RANKING.at(-1)?.rank).toBeLessThanOrEqual(10);
expect(TOP_10_RANKING).toMatchInlineSnapshot(`
[
{
Expand Down

0 comments on commit 42b0dd4

Please sign in to comment.