import { mockShipment, mockTrackingInfo } from '@tests/data/tracking.mock'; import { ConsoleNotifier } from '@/tracking/notifications/console.notifier'; import { resetDate, setDate } from '@/utils/fakeDate'; import { TrackingDto } from '@/dtos/tracking.dto'; import { ShipmentEntity } from '@/entities/shipment.entity'; describe('ConsoleNotifier', () => { beforeEach(() => { jest.clearAllMocks(); setDate('2023-02-14 14:00:00'); }); afterAll(() => { resetDate(); jest.clearAllMocks(); }); it('should be defined', () => { expect(ConsoleNotifier).toBeDefined(); }); it('should be arriving soon if arrivalEstimate is between 0 and 1 days of today', async () => { const logSpy = jest.spyOn(global.console, 'log').mockImplementation(); const consoleNotifier = new ConsoleNotifier(mockShipment, mockTrackingInfo); expect(consoleNotifier.isDelayed).toBe(false); expect(consoleNotifier.daysToArrival).toBeGreaterThan(0); expect(consoleNotifier.daysToArrival).toBeLessThan(1); await expect(consoleNotifier.send()).resolves.toBe(true); expect(logSpy).toHaveBeenCalledWith( 'Send email with the following messages: Your shipment will arrive soon', ); }); it('should be arriving soon if arrivalEstimate is less than or equal to today', async () => { const logSpy = jest.spyOn(global.console, 'log').mockImplementation(); const arrivesTodayTrackingInfo: TrackingDto = { ...mockTrackingInfo, arrivalDateEstimate: new Date(), }; const consoleNotifier = new ConsoleNotifier( mockShipment, arrivesTodayTrackingInfo, ); expect(consoleNotifier.isDelayed).toBe(false); expect(consoleNotifier.daysToArrival).toBeLessThanOrEqual(0); await expect(consoleNotifier.send()).resolves.toBe(true); expect(logSpy).toHaveBeenCalledWith( 'Send email with the following messages: Your shipment has arrived', ); }); it('should be delayed if arrivalDateEstimate is more than 1 day after arrivalDateOriginal', async () => { const logSpy = jest.spyOn(global.console, 'log').mockImplementation(); const trackingDelay = 2; const newArrivalDate = new Date(); newArrivalDate.setDate( mockShipment.arrivalDateOriginal.getDate() + trackingDelay, ); const delayedTrackingInfo: TrackingDto = { ...mockTrackingInfo, arrivalDateEstimate: newArrivalDate, }; delayedTrackingInfo.arrivalDateEstimate; const consoleNotifier = new ConsoleNotifier( mockShipment, delayedTrackingInfo, ); expect(consoleNotifier.isDelayed).toBe(true); expect(consoleNotifier.daysToArrival).toBe(trackingDelay); await expect(consoleNotifier.send()).resolves.toBe(true); expect(logSpy).toHaveBeenCalledTimes(1); expect(logSpy).toHaveBeenCalledWith( 'Send email with the following messages: Your shipment has been delayed', ); }); it('should log multiple messages if shipment has arrived but original time has been exceeded by more than 1 day', async () => { const logSpy = jest.spyOn(global.console, 'log').mockImplementation(); const shipment: ShipmentEntity = { ...mockShipment, arrivalDateOriginal: new Date('2023-02-12 14:00:00'), }; const trackingInfo: TrackingDto = { ...mockTrackingInfo, arrivalDateEstimate: new Date('2023-02-14 14:00:00'), }; const consoleNotifier = new ConsoleNotifier(shipment, trackingInfo); expect(consoleNotifier.isDelayed).toBe(true); expect(consoleNotifier.daysToArrival).toBe(0); await expect(consoleNotifier.send()).resolves.toBe(true); expect(logSpy).toHaveBeenCalledWith( 'Send email with the following messages: Your shipment has been delayed\nYour shipment has arrived', ); }); it('should log multiple messages if shipment will arrive soon but original time has been exceeded by more than 1 day', async () => { const logSpy = jest.spyOn(global.console, 'log').mockImplementation(); const shipment: ShipmentEntity = { ...mockShipment, arrivalDateOriginal: new Date('2023-02-12 14:00:00'), }; const trackingInfo: TrackingDto = { ...mockTrackingInfo, arrivalDateEstimate: new Date('2023-02-14 15:00:00'), }; const consoleNotifier = new ConsoleNotifier(shipment, trackingInfo); expect(consoleNotifier.isDelayed).toBe(true); expect(consoleNotifier.daysToArrival).toBeGreaterThan(0); expect(consoleNotifier.daysToArrival).toBeLessThanOrEqual(1); await expect(consoleNotifier.send()).resolves.toBe(true); expect(logSpy).toHaveBeenCalledWith( 'Send email with the following messages: Your shipment has been delayed\nYour shipment will arrive soon', ); }); });