Skip to content

Instantly share code, notes, and snippets.

@dmitry-grinko
Created October 24, 2024 19:24
Show Gist options
  • Save dmitry-grinko/75b4535c14a221e3a62cde624c0573e5 to your computer and use it in GitHub Desktop.
Save dmitry-grinko/75b4535c14a221e3a62cde624c0573e5 to your computer and use it in GitHub Desktop.
import moment from 'moment';
import { getDiningDayAndTime } from './path-to-your-file';
jest.mock('moment', () => {
const mockMoment = jest.fn();
const mockMomentInstance = {
get: jest.fn(),
format: jest.fn(),
calendar: jest.fn()
};
mockMoment.mockReturnValue(mockMomentInstance);
return mockMoment;
});
describe('getDiningDayAndTime', () => {
let mockMomentInstance;
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();
mockMomentInstance = moment();
});
it('should format time as hA when minutes are 0', () => {
// Setup
mockMomentInstance.get.mockReturnValue(0); // minutes = 0
mockMomentInstance.format.mockReturnValue('9AM');
mockMomentInstance.calendar.mockReturnValue('[Today]');
// Execute
const result = getDiningDayAndTime(new Date());
// Assert
expect(result).toEqual(['[Today]', '9AM']);
expect(mockMomentInstance.format).toHaveBeenCalledWith('hA');
});
it('should format time as h:mmA when minutes are not 0', () => {
// Setup
mockMomentInstance.get.mockReturnValue(30); // minutes = 30
mockMomentInstance.format.mockReturnValue('9:30AM');
mockMomentInstance.calendar.mockReturnValue('[Today]');
// Execute
const result = getDiningDayAndTime(new Date());
// Assert
expect(result).toEqual(['[Today]', '9:30AM']);
expect(mockMomentInstance.format).toHaveBeenCalledWith('h:mmA');
});
it('should return [Today] for same day', () => {
// Setup
mockMomentInstance.get.mockReturnValue(0);
mockMomentInstance.format.mockReturnValue('9AM');
mockMomentInstance.calendar.mockReturnValue('[Today]');
// Execute
const result = getDiningDayAndTime(new Date());
// Assert
expect(result[0]).toBe('[Today]');
expect(mockMomentInstance.calendar).toHaveBeenCalledWith(undefined, {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'ddd Do MMM',
sameElse: 'ddd Do MMM'
});
});
it('should return [Tomorrow] for next day', () => {
// Setup
mockMomentInstance.get.mockReturnValue(0);
mockMomentInstance.format.mockReturnValue('9AM');
mockMomentInstance.calendar.mockReturnValue('[Tomorrow]');
// Execute
const tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000);
const result = getDiningDayAndTime(tomorrow);
// Assert
expect(result[0]).toBe('[Tomorrow]');
});
it('should format date as "ddd Do MMM" for next week', () => {
// Setup
mockMomentInstance.get.mockReturnValue(0);
mockMomentInstance.format.mockReturnValue('9AM');
mockMomentInstance.calendar.mockReturnValue('Mon 15th Jan');
// Execute
const nextWeek = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
const result = getDiningDayAndTime(nextWeek);
// Assert
expect(result[0]).toBe('Mon 15th Jan');
});
it('should handle string input for dateAndTime', () => {
// Setup
mockMomentInstance.get.mockReturnValue(0);
mockMomentInstance.format.mockReturnValue('9AM');
mockMomentInstance.calendar.mockReturnValue('[Today]');
// Execute
const result = getDiningDayAndTime('2024-01-01T09:00:00');
// Assert
expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(2);
expect(moment).toHaveBeenCalledWith('2024-01-01T09:00:00');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment