Skip to content

Instantly share code, notes, and snippets.

@IvanAdmaers
Created October 7, 2022 13:40
Show Gist options
  • Save IvanAdmaers/265dc3f6d1aabc4485fed1fc258570aa to your computer and use it in GitHub Desktop.
Save IvanAdmaers/265dc3f6d1aabc4485fed1fc258570aa to your computer and use it in GitHub Desktop.
getTimeGreetings
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);
});
});
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