Created
October 7, 2022 13:40
-
-
Save IvanAdmaers/265dc3f6d1aabc4485fed1fc258570aa to your computer and use it in GitHub Desktop.
getTimeGreetings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getTimeGreetings, welcomes } from '.'; | |
describe('getTimeGreetings', () => { | |
beforeEach(() => { | |
jest.useFakeTimers(); | |
}); | |
it('should return night greetings', () => { | |
jest.setSystemTime( | |
new Date('Thu Oct 06 2022 00:00:54 GMT-0400 (Eastern Daylight Time)') | |
); | |
expect(getTimeGreetings()).toBe(welcomes.night); | |
}); | |
it('should return morning greetings', () => { | |
jest.setSystemTime( | |
new Date('Thu Oct 06 2022 08:29:54 GMT-0400 (Eastern Daylight Time)') | |
); | |
expect(getTimeGreetings()).toBe(welcomes.morning); | |
}); | |
it('should return afternoon greetings', () => { | |
jest.setSystemTime( | |
new Date('Thu Oct 06 2022 15:29:54 GMT-0400 (Eastern Daylight Time)') | |
); | |
expect(getTimeGreetings()).toBe(welcomes.afternoon); | |
}); | |
it('should return evening greetings', () => { | |
jest.setSystemTime( | |
new Date('Thu Oct 06 2022 19:29:54 GMT-0400 (Eastern Daylight Time)') | |
); | |
expect(getTimeGreetings()).toBe(welcomes.evening); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const welcomes = { | |
morning: 'Доброе утро', | |
afternoon: 'Добрый день', | |
evening: 'Добрый вечер', | |
night: 'Доброй ночи', | |
}; | |
/** | |
* This function gets time of day greetings | |
*/ | |
export const getTimeGreetings = () => { | |
const hour = new Date().getHours(); | |
if (hour < 6) { | |
return welcomes.night; | |
} | |
if (hour < 12) { | |
return welcomes.morning; | |
} | |
if (hour < 18) { | |
return welcomes.afternoon; | |
} | |
return welcomes.evening; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment