Skip to content

Commit

Permalink
feat: increase milestones values precision
Browse files Browse the repository at this point in the history
  • Loading branch information
khmm12 committed Sep 24, 2024
1 parent 1febfaf commit 077c221
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports[`TimeMilestones > renders correctly 1`] = `
>
of day
0.67
0.6704
</div>
<div
aria-label="of week"
Expand All @@ -35,14 +35,14 @@ exports[`TimeMilestones > renders correctly 1`] = `
>
of month
0.15
0.1506
</div>
<div
aria-label="of year"
>
of year
0.17
0.1744
</div>
</div>
Expand Down
22 changes: 11 additions & 11 deletions src/components/TimeMilestones/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ describe('getDayMilestone', () => {
['06:00', 0.25],
['12:00', 0.5],
['18:00', 0.75],
['23:59', 0.99],
['23:59:59', 0.9999],
])('at %s equals to %f', (time, expected) => {
const [hours = 0, minutes = 0, seconds = 0] = time.split(':').map(Number)
const currentDateTime = R.pipe(new Date(), D.setHours(hours), D.setMinutes(minutes), D.setSeconds(seconds))

expect(getDayMilestone(currentDateTime)).toBe(expected)
expect(getDayMilestone(currentDateTime)).toBeCloseTo(expected)
})
})

describe('getMonthMilestone', () => {
it.each([
[1, '00:00', 0],
[16, '00:00', 0.5],
[30, '23:59', 0.99],
[30, '23:59:59', 0.9999],
])('on %s at %s equals %f', (day, time, expected) => {
const [hours = 0, minutes = 0, seconds = 0] = time.split(':').map(Number)
const currentDateTime = R.pipe(
Expand All @@ -33,15 +33,15 @@ describe('getMonthMilestone', () => {
D.setSeconds(seconds),
)

expect(getMonthMilestone(currentDateTime)).toBe(expected)
expect(getMonthMilestone(currentDateTime)).toBeCloseTo(expected)
})
})

describe('getWeekMilestone', () => {
it.each([
[1, '00:00', 0],
[4, '13:00', 0.5],
[7, '23:59', 0.99],
[4, '12:00', 0.5],
[7, '23:59:59', 0.9999],
])('on %s day at %s equals %f', (dayOfWeek, time, expected) => {
const [hours = 0, minutes = 0, seconds = 0] = time.split(':').map(Number)
const currentDateTime = R.pipe(
Expand All @@ -53,15 +53,15 @@ describe('getWeekMilestone', () => {
D.setSeconds(seconds),
)

expect(getWeekMilestone(currentDateTime)).toBe(expected)
expect(getWeekMilestone(currentDateTime)).toBeCloseTo(expected)
})
})

describe('getYearMilestone', () => {
it.each([
[1, 1, '00:00', 0],
[7, 2, '23:59', 0.5],
[12, 31, '23:59', 0.99],
[12, 31, '23:59', 0.9999],
])('in %d month %dst day at %s equals %f', (month, day, time, expected) => {
const [hours = 0, minutes = 0, seconds = 0] = time.split(':').map(Number)
const currentDateTime = R.pipe(
Expand All @@ -73,7 +73,7 @@ describe('getYearMilestone', () => {
D.setSeconds(seconds),
)

expect(getYearMilestone(currentDateTime)).toBe(expected)
expect(getYearMilestone(currentDateTime)).toBeCloseTo(expected, 2)
})
})

Expand All @@ -82,13 +82,13 @@ describe('getBirthDayMilestone', () => {
const currentDateTime = new Date('2022-04-01')
const birthDate = new Date('1970-06-31')

expect(getBirthdayMilestone(birthDate)(currentDateTime)).toBe(0.75)
expect(getBirthdayMilestone(birthDate)(currentDateTime)).toBeCloseTo(0.75)
})

it('handles appeared birth date in this year', () => {
const currentDateTime = new Date('2022-12-31')
const birthDate = new Date('1970-06-31')

expect(getBirthdayMilestone(birthDate)(currentDateTime)).toBe(0.5)
expect(getBirthdayMilestone(birthDate)(currentDateTime)).toBeCloseTo(0.5)
})
})
2 changes: 1 addition & 1 deletion src/components/TimeMilestones/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'date-fns'
import { roundDown } from '@/utils/rounds'

const PRECISION = 2
const PRECISION = 4

type CalculateRelative<T> = (now: Date) => T

Expand Down

0 comments on commit 077c221

Please sign in to comment.