Added tests for notifiers
This commit is contained in:
parent
afebfc9150
commit
c33cc366d6
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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'),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue