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 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)
})
})

View file

@ -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)
}