diff options
author | Christian Fehmer <[email protected]> | 2024-06-16 23:29:08 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-06-16 23:29:08 +0200 |
commit | f0ec0c04fd4d8cee5f711637d6c6e6ee2e8578a1 (patch) | |
tree | 44395cb8543bdc1999c1a3676fd24fa408f78030 | |
parent | 92193f1dc55ed299c4a105e67cb25d9ae22bfdd1 (diff) | |
download | monkeytype-f0ec0c04fd4d8cee5f711637d6c6e6ee2e8578a1.tar.gz monkeytype-f0ec0c04fd4d8cee5f711637d6c6e6ee2e8578a1.zip |
fix: test activity graph showing 54 weeks (@fehmer) (#5497)
-rw-r--r-- | frontend/__tests__/elements/test-activity-calendar.spec.ts | 64 | ||||
-rw-r--r-- | frontend/src/ts/elements/test-activity-calendar.ts | 10 |
2 files changed, 71 insertions, 3 deletions
diff --git a/frontend/__tests__/elements/test-activity-calendar.spec.ts b/frontend/__tests__/elements/test-activity-calendar.spec.ts index accf29324..00c877a30 100644 --- a/frontend/__tests__/elements/test-activity-calendar.spec.ts +++ b/frontend/__tests__/elements/test-activity-calendar.spec.ts @@ -79,6 +79,10 @@ describe("test-activity-calendar.ts", () => { expect(calendar.getMonths()).toEqual([ { + text: "", + weeks: 2, + }, + { text: "may", weeks: 4, }, @@ -356,6 +360,66 @@ describe("test-activity-calendar.ts", () => { }, ]); }); + it("no double month for for 16th june", () => { + //set today + vi.setSystemTime(getDate("2024-06-16")); + const calendar = new TestActivityCalendar([], getDate("2024-06-01")); + + expect(calendar.getMonths()).toEqual([ + { + text: "", + weeks: 2, + }, + { + text: "jul", + weeks: 5, + }, + { + text: "aug", + weeks: 4, + }, + { + text: "sep", + weeks: 4, + }, + { + text: "oct", + weeks: 5, + }, + { + text: "nov", + weeks: 4, + }, + { + text: "dec", + weeks: 5, + }, + { + text: "jan", + weeks: 4, + }, + { + text: "feb", + weeks: 4, + }, + { + text: "mar", + weeks: 5, + }, + { + text: "apr", + weeks: 4, + }, + { + text: "may", + weeks: 4, + }, + { + text: "jun", + weeks: 3, + }, + ]); + }); }); describe("getDays", () => { diff --git a/frontend/src/ts/elements/test-activity-calendar.ts b/frontend/src/ts/elements/test-activity-calendar.ts index b5aaad2e5..16b4c9c9b 100644 --- a/frontend/src/ts/elements/test-activity-calendar.ts +++ b/frontend/src/ts/elements/test-activity-calendar.ts @@ -3,7 +3,6 @@ import { UTCDateMini } from "@date-fns/utc/date/mini"; import { format, endOfMonth, - subYears, addDays, differenceInDays, eachMonthOfInterval, @@ -18,6 +17,7 @@ import { isSunday, nextSaturday, isSaturday, + subWeeks, } from "date-fns"; export class TestActivityCalendar implements MonkeyTypes.TestActivityCalendar { @@ -44,7 +44,8 @@ export class TestActivityCalendar implements MonkeyTypes.TestActivityCalendar { const end = fullYear ? endOfYear(lastDay) : new Date(); let start = startOfYear(lastDay); if (!fullYear) { - start = addDays(subYears(end, 1), 1); + //show the last 52 weeks. Not using one year to avoid the graph to show 54 weeks + start = addDays(subWeeks(end, 52), 1); if (!isSunday(start)) start = previousSunday(start); } @@ -88,11 +89,14 @@ export class TestActivityCalendar implements MonkeyTypes.TestActivityCalendar { if (!isSaturday(end)) end = nextSaturday(end); const weeks = differenceInWeeks(end, start, { roundingMethod: "ceil" }); - if (weeks > 2) + if (weeks > 2) { results.push({ text: format(month, "MMM").toLowerCase(), weeks: weeks, }); + } else if (i == 0) { + results.push({ text: "", weeks: weeks }); + } } return results; } |