Skip to content

Commit

Permalink
M2-6837: [Mobile] Complete test coverage for ScheduledDateCalculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Nalivaiko, Aleksej authored and Nalivaiko, Aleksej committed Jul 25, 2024
1 parent 8001dc3 commit a1f2f69
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
30 changes: 16 additions & 14 deletions src/entities/event/model/operations/ScheduledDateCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {

type EventParseInput = Parameters<typeof Parse.schedule>[0];

const cache = new Map();

export class ScheduledDateCalculator {
private cache = new Map();

constructor() {}

private setTime(target: Date, availability: EventAvailability) {
Expand All @@ -34,6 +34,12 @@ export class ScheduledDateCalculator {
return new Date();
}

private getCacheKey(event: ScheduleEvent): string {
const today = this.getNow().toDateString();

return `${JSON.stringify(event.availability)}${event.selectedDate?.getTime() ?? ''}${today}`;
}

private calculateForMonthly(
selectedDate: Date,
availability: EventAvailability,
Expand Down Expand Up @@ -87,7 +93,7 @@ export class ScheduledDateCalculator {
return result;
}

private calculateScheduledAt(event: ScheduleEvent): Date | null {
private calculateInternal(event: ScheduleEvent): Date | null {
const { availability, selectedDate } = event;

const now = this.getNow();
Expand Down Expand Up @@ -158,22 +164,18 @@ export class ScheduledDateCalculator {
useCache: boolean = true,
): Date | null {
if (!useCache) {
return this.calculateScheduledAt(event);
return this.calculateInternal(event);
}

const today = this.getNow().toDateString();

const key =
JSON.stringify(event.availability) +
(event.selectedDate?.getTime() ?? '') +
today;
const key = this.getCacheKey(event);

if (cache.has(key)) {
return cache.get(key);
if (this.cache.has(key)) {
return this.cache.get(key) as Date;
}

const result = this.calculateScheduledAt(event);
cache.set(key, result);
const result = this.calculateInternal(event);

this.cache.set(key, result);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { AvailabilityType, PeriodicityType } from '@app/abstract/lib';

import {
EventAvailability,
ScheduleEvent,
} from '../../../lib/types/scheduledDateCalculator';
import { ScheduledDateCalculator } from '../ScheduledDateCalculator';

const createEvent = () => {
const eventAvailability: EventAvailability = {
availabilityType: AvailabilityType.AlwaysAvailable,
periodicityType: PeriodicityType.Always,
timeFrom: null,
timeTo: null,
startDate: null,
endDate: null,
};

const scheduleEvent: ScheduleEvent = {
id: 'eventTestId',
entityId: 'entityTestId',
availability: eventAvailability,
selectedDate: null,
scheduledAt: null,
};

return scheduleEvent;
};

describe('Test ScheduledDateCalculator: edge cases and cache', () => {
it('Should take value from cache when call calculate 2nd time with the same event settings', () => {
const calculator = new ScheduledDateCalculator();

const mockDate = new Date(2023, 5, 10);

const calculateInternalMock = jest.fn().mockReturnValue(mockDate);
//@ts-expect-error
calculator.calculateInternal = calculateInternalMock;

const scheduleEvent = createEvent();

const result = calculator.calculate(scheduleEvent);

expect(result).toEqual(mockDate);

const secondResult = calculator.calculate({ ...scheduleEvent });

expect(secondResult).toEqual(mockDate);

expect(calculateInternalMock).toHaveBeenCalledTimes(1);
});

it('Should throw error if to pass event with time set in the selectedDate', () => {
const calculator = new ScheduledDateCalculator();

const scheduleEvent = createEvent();

scheduleEvent.selectedDate = new Date(2023, 10, 5, 15, 30);

expect(() => calculator.calculate(scheduleEvent)).toThrow(
'[ScheduledDateCalculator]: selectedDate contains time set',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { addDays, addMonths, startOfDay, subDays, subMonths } from 'date-fns';

import { AvailabilityType, PeriodicityType } from '@app/abstract/lib';

import { ScheduledDateCalculator } from './ScheduledDateCalculator';
import { ScheduleEvent } from '../../lib/types/scheduledDateCalculator';
import { ScheduleEvent } from '../../../lib/types/scheduledDateCalculator';
import { ScheduledDateCalculator } from '../ScheduledDateCalculator';

describe('ScheduledDateCalculator: test monthly events', () => {
describe('Test ScheduledDateCalculator: monthly events', () => {
const getScheduledDateCalculator = (now: Date) => {
const instance = new ScheduledDateCalculator();
//@ts-expect-error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { addDays, startOfDay, subDays } from 'date-fns';

import ScheduledDateCalculator from './ScheduledDateCalculator';
import ScheduledDateCalculator from '../ScheduledDateCalculator';

const now = new Date(2024, 0, 25);

describe('ScheduledDateCalculator', () => {
describe('Test ScheduledDateCalculator', () => {
let tempGetNow;

beforeAll(() => {
Expand Down

0 comments on commit a1f2f69

Please sign in to comment.