fix(main): tests failing

This commit is contained in:
Kim, Jimin 2023-07-31 01:18:51 +09:00
parent 0c22e01397
commit a9f938f723
Signed by: pomp
GPG key ID: CE1DDB8A4A765403
2 changed files with 41 additions and 27 deletions

View file

@ -1,12 +1,19 @@
import dayjs from "dayjs" import dayjs from "dayjs"
import timezone from "dayjs/plugin/timezone"
import utc from "dayjs/plugin/utc"
import { testing } from "./getAge" import { testing } from "./getAge"
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.tz.setDefault("Asia/Seoul")
const { birth, getAge, ageInt, ageDecimal, isOverBirthDay } = testing const { birth, getAge, ageInt, ageDecimal, isOverBirthDay } = testing
describe("getAge tests", () => { describe("getAge tests", () => {
test("birthday to be 2002-07-30", () => { 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<{ test.each<{
@ -17,56 +24,56 @@ describe("getAge tests", () => {
date: number date: number
}>([ }>([
{ {
timestamp: "2022-12-31 00:00:00.000+09:00", timestamp: "2022-12-31",
overBD: true, overBD: true,
year: 2022, year: 2022,
monthIndex: 11, monthIndex: 11,
date: 31, date: 31,
}, },
{ {
timestamp: "2023-01-01 00:00:00.000+09:00", timestamp: "2023-01-01",
overBD: false, overBD: false,
year: 2023, year: 2023,
monthIndex: 0, monthIndex: 0,
date: 1, date: 1,
}, },
{ {
timestamp: "2023-07-29 00:00:00.000+09:00", timestamp: "2023-07-29",
overBD: false, overBD: false,
year: 2023, year: 2023,
monthIndex: 6, monthIndex: 6,
date: 29, date: 29,
}, },
{ {
timestamp: "2023-07-30 00:00:00.000+09:00", timestamp: "2023-07-30",
overBD: true, overBD: true,
year: 2023, year: 2023,
monthIndex: 6, monthIndex: 6,
date: 30, date: 30,
}, },
{ {
timestamp: "2023-07-31 00:00:00.000+09:00", timestamp: "2023-07-31",
overBD: true, overBD: true,
year: 2023, year: 2023,
monthIndex: 6, monthIndex: 6,
date: 31, date: 31,
}, },
{ {
timestamp: "2023-12-31 00:00:00.000+09:00", timestamp: "2023-12-31",
overBD: true, overBD: true,
year: 2023, year: 2023,
monthIndex: 11, monthIndex: 11,
date: 31, date: 31,
}, },
{ {
timestamp: "2024-01-01 00:00:00.000+09:00", timestamp: "2024-01-01",
overBD: false, overBD: false,
year: 2024, year: 2024,
monthIndex: 0, monthIndex: 0,
date: 1, date: 1,
}, },
])("isOverBirthDay to work ($timestamp)", (testData) => { ])("isOverBirthDay to work ($timestamp)", (testData) => {
const date = dayjs(testData.timestamp) const date = dayjs.tz(testData.timestamp)
expect(date.year()).toEqual(testData.year) expect(date.year()).toEqual(testData.year)
expect(date.month()).toEqual(testData.monthIndex) expect(date.month()).toEqual(testData.monthIndex)
@ -75,30 +82,30 @@ describe("getAge tests", () => {
}) })
test.each<[string, number]>([ test.each<[string, number]>([
["2002-07-30 00:00:00.000+09:00", 0], ["2002-07-30", 0],
["2023-07-29 00:00:00.000+09:00", 20], ["2023-07-29", 20],
["2023-07-30 00:00:00.000+09:00", 21], ["2023-07-30", 21],
["2023-07-31 00:00:00.000+09:00", 21], ["2023-07-31", 21],
])("ageInt to work for '%s'", (date, expected) => { ])("ageInt to work for '%s'", (date, expected) => {
expect(ageInt(dayjs(date))).toEqual(expected) expect(ageInt(dayjs.tz(date))).toEqual(expected)
}) })
test.each<[string, number]>([ test.each<[string, number]>([
["2023-07-29 00:00:00.000+09:00", 0.9972602739726028], ["2023-07-29", 0.9972602739726028],
["2023-07-30 00:00:00.000+09:00", 0.0], ["2023-07-30", 0.0],
["2023-07-31 00:00:00.000+09:00", 0.00273224043715847], ["2023-07-31", 0.00273224043715847],
])("ageDecimal to work for '%s'", (date, expected) => { ])("ageDecimal to work for '%s'", (date, expected) => {
expect(ageDecimal(dayjs(date))).toEqual(expected) expect(ageDecimal(dayjs.tz(date))).toEqual(expected)
expect(ageDecimal(dayjs(date))).toBeGreaterThanOrEqual(0.0) expect(ageDecimal(dayjs.tz(date))).toBeGreaterThanOrEqual(0.0)
expect(ageDecimal(dayjs(date))).toBeLessThan(1.0) expect(ageDecimal(dayjs.tz(date))).toBeLessThan(1.0)
}) })
test.each<[string, number]>([ test.each<[string, number]>([
["2002-07-30 00:00:00.000+09:00", 0.0], ["2002-07-30", 0.0],
["2023-07-29 00:00:00.000+09:00", 20.997260273972604], ["2023-07-29", 20.997260273972604],
["2023-07-30 00:00:00.000+09:00", 21.0], ["2023-07-30", 21.0],
["2023-07-31 00:00:00.000+09:00", 21.002732240437158], ["2023-07-31", 21.002732240437158],
])("getAge to work for '%s'", (date, expected) => { ])("getAge to work for '%s'", (date, expected) => {
expect(getAge(dayjs(date))).toEqual(expected) expect(getAge(dayjs.tz(date))).toEqual(expected)
}) })
}) })

View file

@ -1,14 +1,21 @@
import dayjs, { type Dayjs } from "dayjs" 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 // 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 * Gets developomp's age with decimal precision
* *
* @param now - current `Date` in KST * @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) return ageInt(now) + ageDecimal(now)
} }