From a9f938f7237e8bcc84a77e0c26a472b33eb93c30 Mon Sep 17 00:00:00 2001 From: developomp Date: Mon, 31 Jul 2023 01:18:51 +0900 Subject: [PATCH] fix(main): tests failing --- apps/main/src/utils/getAge.test.ts | 57 +++++++++++++++++------------- apps/main/src/utils/getAge.ts | 11 ++++-- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/apps/main/src/utils/getAge.test.ts b/apps/main/src/utils/getAge.test.ts index 500c53f..68667c2 100644 --- a/apps/main/src/utils/getAge.test.ts +++ b/apps/main/src/utils/getAge.test.ts @@ -1,12 +1,19 @@ import dayjs from "dayjs" +import timezone from "dayjs/plugin/timezone" +import utc from "dayjs/plugin/utc" import { testing } from "./getAge" +dayjs.extend(utc) +dayjs.extend(timezone) + +dayjs.tz.setDefault("Asia/Seoul") + const { birth, getAge, ageInt, ageDecimal, isOverBirthDay } = testing describe("getAge tests", () => { test("birthday to be 2002-07-30", () => { - expect(birth).toEqual(dayjs("2002-07-30 00:00:00.000+09:00")) + expect(birth).toEqual(dayjs.tz("2002-07-30")) }) test.each<{ @@ -17,56 +24,56 @@ describe("getAge tests", () => { date: number }>([ { - timestamp: "2022-12-31 00:00:00.000+09:00", + timestamp: "2022-12-31", overBD: true, year: 2022, monthIndex: 11, date: 31, }, { - timestamp: "2023-01-01 00:00:00.000+09:00", + timestamp: "2023-01-01", overBD: false, year: 2023, monthIndex: 0, date: 1, }, { - timestamp: "2023-07-29 00:00:00.000+09:00", + timestamp: "2023-07-29", overBD: false, year: 2023, monthIndex: 6, date: 29, }, { - timestamp: "2023-07-30 00:00:00.000+09:00", + timestamp: "2023-07-30", overBD: true, year: 2023, monthIndex: 6, date: 30, }, { - timestamp: "2023-07-31 00:00:00.000+09:00", + timestamp: "2023-07-31", overBD: true, year: 2023, monthIndex: 6, date: 31, }, { - timestamp: "2023-12-31 00:00:00.000+09:00", + timestamp: "2023-12-31", overBD: true, year: 2023, monthIndex: 11, date: 31, }, { - timestamp: "2024-01-01 00:00:00.000+09:00", + timestamp: "2024-01-01", overBD: false, year: 2024, monthIndex: 0, date: 1, }, ])("isOverBirthDay to work ($timestamp)", (testData) => { - const date = dayjs(testData.timestamp) + const date = dayjs.tz(testData.timestamp) expect(date.year()).toEqual(testData.year) expect(date.month()).toEqual(testData.monthIndex) @@ -75,30 +82,30 @@ describe("getAge tests", () => { }) test.each<[string, number]>([ - ["2002-07-30 00:00:00.000+09:00", 0], - ["2023-07-29 00:00:00.000+09:00", 20], - ["2023-07-30 00:00:00.000+09:00", 21], - ["2023-07-31 00:00:00.000+09:00", 21], + ["2002-07-30", 0], + ["2023-07-29", 20], + ["2023-07-30", 21], + ["2023-07-31", 21], ])("ageInt to work for '%s'", (date, expected) => { - expect(ageInt(dayjs(date))).toEqual(expected) + expect(ageInt(dayjs.tz(date))).toEqual(expected) }) test.each<[string, number]>([ - ["2023-07-29 00:00:00.000+09:00", 0.9972602739726028], - ["2023-07-30 00:00:00.000+09:00", 0.0], - ["2023-07-31 00:00:00.000+09:00", 0.00273224043715847], + ["2023-07-29", 0.9972602739726028], + ["2023-07-30", 0.0], + ["2023-07-31", 0.00273224043715847], ])("ageDecimal to work for '%s'", (date, expected) => { - expect(ageDecimal(dayjs(date))).toEqual(expected) - expect(ageDecimal(dayjs(date))).toBeGreaterThanOrEqual(0.0) - expect(ageDecimal(dayjs(date))).toBeLessThan(1.0) + expect(ageDecimal(dayjs.tz(date))).toEqual(expected) + expect(ageDecimal(dayjs.tz(date))).toBeGreaterThanOrEqual(0.0) + expect(ageDecimal(dayjs.tz(date))).toBeLessThan(1.0) }) test.each<[string, number]>([ - ["2002-07-30 00:00:00.000+09:00", 0.0], - ["2023-07-29 00:00:00.000+09:00", 20.997260273972604], - ["2023-07-30 00:00:00.000+09:00", 21.0], - ["2023-07-31 00:00:00.000+09:00", 21.002732240437158], + ["2002-07-30", 0.0], + ["2023-07-29", 20.997260273972604], + ["2023-07-30", 21.0], + ["2023-07-31", 21.002732240437158], ])("getAge to work for '%s'", (date, expected) => { - expect(getAge(dayjs(date))).toEqual(expected) + expect(getAge(dayjs.tz(date))).toEqual(expected) }) }) diff --git a/apps/main/src/utils/getAge.ts b/apps/main/src/utils/getAge.ts index b2cdbad..dcd791f 100644 --- a/apps/main/src/utils/getAge.ts +++ b/apps/main/src/utils/getAge.ts @@ -1,14 +1,21 @@ import dayjs, { type Dayjs } from "dayjs" +import timezone from "dayjs/plugin/timezone" +import utc from "dayjs/plugin/utc" + +dayjs.extend(utc) +dayjs.extend(timezone) + +dayjs.tz.setDefault("Asia/Seoul") // my birthday in KST :D -const birth = dayjs("2002-07-30 00:00:00.000+09:00") +const birth = dayjs.tz("2002-07-30") /** * Gets developomp's age with decimal precision * * @param now - current `Date` in KST */ -export default function getAge(now: Dayjs = dayjs()): number { +export default function getAge(now: Dayjs = dayjs.tz()): number { return ageInt(now) + ageDecimal(now) }