fix: move hourToDaytime to separate file in order to pass tests
This commit is contained in:
31
src/helpers/hourToDaytime.test.ts
Normal file
31
src/helpers/hourToDaytime.test.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import {describe, it, expect} from 'vitest'
|
||||
import {hourToDaytime} from "./hourToDaytime"
|
||||
|
||||
function dateWithHour(hours: number): Date {
|
||||
const newDate = new Date()
|
||||
newDate.setHours(hours, 0, 0,0 )
|
||||
return newDate
|
||||
}
|
||||
|
||||
describe('Salutation', () => {
|
||||
it('shows the right salutation in the night', () => {
|
||||
const salutation = hourToDaytime(dateWithHour(4))
|
||||
expect(salutation).toBe('night')
|
||||
})
|
||||
it('shows the right salutation in the morning', () => {
|
||||
const salutation = hourToDaytime(dateWithHour(8))
|
||||
expect(salutation).toBe('morning')
|
||||
})
|
||||
it('shows the right salutation in the day', () => {
|
||||
const salutation = hourToDaytime(dateWithHour(13))
|
||||
expect(salutation).toBe('day')
|
||||
})
|
||||
it('shows the right salutation in the night', () => {
|
||||
const salutation = hourToDaytime(dateWithHour(20))
|
||||
expect(salutation).toBe('evening')
|
||||
})
|
||||
it('shows the right salutation in the night again', () => {
|
||||
const salutation = hourToDaytime(dateWithHour(23))
|
||||
expect(salutation).toBe('night')
|
||||
})
|
||||
})
|
14
src/helpers/hourToDaytime.ts
Normal file
14
src/helpers/hourToDaytime.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import type { Daytime } from '@/composables/useDaytimeSalutation'
|
||||
|
||||
export function hourToDaytime(now: Date): Daytime {
|
||||
const hours = now.getHours()
|
||||
|
||||
const daytimeMap = {
|
||||
night: hours < 5 || hours > 23,
|
||||
morning: hours < 11,
|
||||
day: hours < 18,
|
||||
evening: hours < 23,
|
||||
} as Record<Daytime, boolean>
|
||||
|
||||
return (Object.keys(daytimeMap) as Daytime[]).find((daytime) => daytimeMap[daytime]) || 'night'
|
||||
}
|
Reference in New Issue
Block a user