diff --git a/src/tracking/notifications/console.notifier.spec.ts b/src/tracking/notifications/console.notifier.spec.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/tracking/notifications/console.notifier.ts b/src/tracking/notifications/console.notifier.ts index e5e2704..a3d6f48 100644 --- a/src/tracking/notifications/console.notifier.ts +++ b/src/tracking/notifications/console.notifier.ts @@ -20,7 +20,7 @@ export class ConsoleNotifier extends NotifierAbstract implements INotifier { private generateMessages(): string[] { const messages: string[] = []; if (this.isDelayed) { - messages.push('Your shipment has been delayed.'); + messages.push('Your shipment has been delayed'); } if (this.daysToArrival <= 0) { diff --git a/src/tracking/notifications/notifier.factory.spec.ts b/src/tracking/notifications/notifier.factory.spec.ts deleted file mode 100644 index e69de29..0000000 diff --git a/tests/data/tracking.mock.ts b/tests/data/tracking.mock.ts index e4d8770..2dd904c 100644 --- a/tests/data/tracking.mock.ts +++ b/tests/data/tracking.mock.ts @@ -6,7 +6,7 @@ import { ContainerEntity } from '@/entities/container.entity'; export const mockTrackingInfo: TrackingDto = { carrierName: 'Evergreen', destinationUnLoCode: 'NLAMS', - departureDate: new Date('2023-05-14T11:00:00.000Z'), + departureDate: new Date('2023-01-14T11:00:00.000Z'), arrivalDateEstimate: new Date('2023-02-14T15:00:00.000Z'), containerNumber: 'AAAA000000000', }; @@ -17,7 +17,7 @@ export const mockShipment: ShipmentEntity = { carrierName: 'Evergreen', originPort: 'NLAMS', destinationPort: 'NLAMS', - departureDate: new Date('2023-05-14T11:00:00.000Z'), + departureDate: new Date('2023-01-14T11:00:00.000Z'), arrivalDateOriginal: new Date('2023-02-14T15:00:00.000Z'), arrivalDateEstimate: new Date('2023-02-14T15:00:00.000Z'), }; diff --git a/tests/tracking/notifications.service.spec.ts b/tests/tracking/notifications.service.spec.ts index 9e04bcd..3b0e31e 100644 --- a/tests/tracking/notifications.service.spec.ts +++ b/tests/tracking/notifications.service.spec.ts @@ -29,6 +29,6 @@ describe('NotificationsService', () => { it('should have a method send that resolves', async () => { await expect( service.send(mockShipment, mockTrackingInfo), - ).resolves.toBeDefined(); + ).resolves.not.toThrow(); }); }); diff --git a/tests/tracking/notifications/console.notifier.spec.ts b/tests/tracking/notifications/console.notifier.spec.ts new file mode 100644 index 0000000..56ede90 --- /dev/null +++ b/tests/tracking/notifications/console.notifier.spec.ts @@ -0,0 +1,118 @@ +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', + ); + }); +}); diff --git a/tests/tracking/notifications/notifier.factory.spec.ts b/tests/tracking/notifications/notifier.factory.spec.ts new file mode 100644 index 0000000..702c120 --- /dev/null +++ b/tests/tracking/notifications/notifier.factory.spec.ts @@ -0,0 +1,14 @@ +import { mockShipment, mockTrackingInfo } from '@tests/data/tracking.mock'; +import { NotifierFactory } from '@/tracking/notifications/notifier.factory'; +import { ConsoleNotifier } from '@/tracking/notifications/console.notifier'; + +describe('NotifierFactory', () => { + it('should be defined', () => { + expect(NotifierFactory).toBeDefined(); + }); + + it('should create a notifier instance', () => { + const factory = new NotifierFactory(mockShipment, mockTrackingInfo); + expect(factory.createNotifier()).toBeInstanceOf(ConsoleNotifier); + }); +});