chore: Simplify `getClosedPeriod` (#35751)

pull/31442/merge
Tasso Evangelista 9 months ago committed by GitHub
parent ff3e08f18d
commit d837f57bc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 101
      apps/meteor/client/components/dashboards/getClosedPeriod.spec.ts
  2. 18
      apps/meteor/client/components/dashboards/periods.ts

@ -1,101 +1,62 @@
import { getClosedPeriod } from './periods';
jest.mock('moment', () => {
return () => jest.requireActual('moment')('2024-05-19T12:00:00.000Z');
});
jest.useFakeTimers();
jest.setSystemTime(Date.parse('2024-05-19T12:00:00.000Z'));
it('should return the correct period range for this month', () => {
const monthExpectedReturn = {
start: new Date('5/1/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};
const period = getClosedPeriod({ startOf: 'month' })(true);
expect(period.start.toISOString().split('T')[0]).toEqual(monthExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(monthExpectedReturn.end);
expect(period.start).toEqual(new Date('2024-05-01T00:00:00.000Z'));
expect(period.end).toEqual(new Date('2024-05-19T23:59:59.999Z'));
});
it('should return the correct period range for this year', () => {
const yearExpectedReturn = {
start: new Date('1/1/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};
const period = getClosedPeriod({ startOf: 'year' })(true);
expect(period.start.toISOString().split('T')[0]).toEqual(yearExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(yearExpectedReturn.end);
expect(period.start).toEqual(new Date('2024-01-01T00:00:00.000Z'));
expect(period.end).toEqual(new Date('2024-05-19T23:59:59.999Z'));
});
it('should return the correct period range for last 6 months', () => {
const last6MonthsExpectedReturn = {
start: new Date('11/1/2023').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};
const period = getClosedPeriod({ startOf: 'month', subtract: { amount: 6, unit: 'months' } })(true);
expect(period.start.toISOString().split('T')[0]).toEqual(last6MonthsExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(last6MonthsExpectedReturn.end);
expect(period.start).toEqual(new Date('2023-11-01T00:00:00.000Z'));
expect(period.end).toEqual(new Date('2024-05-19T23:59:59.999Z'));
});
it('should return the correct period range for this week', () => {
const weekExpectedReturn = {
start: new Date('5/19/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};
const period = getClosedPeriod({ startOf: 'week' })(true);
expect(period.start.toISOString().split('T')[0]).toEqual(weekExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(weekExpectedReturn.end);
expect(period.start).toEqual(new Date('2024-05-19T00:00:00.000Z'));
expect(period.end).toEqual(new Date('2024-05-19T23:59:59.999Z'));
});
it('should return the correct period range for this month using local time', () => {
const monthExpectedReturn = {
start: new Date('5/1/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};
const period = getClosedPeriod({ startOf: 'month' })(false);
describe('using local time', () => {
it('should return the correct period range for this month', () => {
const period = getClosedPeriod({ startOf: 'month' })(false);
expect(period.start.toISOString().split('T')[0]).toEqual(monthExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(monthExpectedReturn.end);
});
expect(period.start).toEqual(new Date('2024-05-01T00:00:00.000'));
expect(period.end).toEqual(new Date('2024-05-19T23:59:59.999'));
});
it('should return the correct period range for this year using local time', () => {
const yearExpectedReturn = {
start: new Date('1/1/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};
it('should return the correct period range for this year', () => {
const period = getClosedPeriod({ startOf: 'year' })(false);
const period = getClosedPeriod({ startOf: 'year' })(false);
expect(period.start).toEqual(new Date('2024-01-01T00:00:00.000'));
expect(period.end).toEqual(new Date('2024-05-19T23:59:59.999'));
});
expect(period.start.toISOString().split('T')[0]).toEqual(yearExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(yearExpectedReturn.end);
});
it('should return the correct period range for last 6 months using local time', () => {
const last6MonthsExpectedReturn = {
start: new Date('11/1/2023').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};
const period = getClosedPeriod({ startOf: 'month', subtract: { amount: 6, unit: 'months' } })(true);
expect(period.start.toISOString().split('T')[0]).toEqual(last6MonthsExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(last6MonthsExpectedReturn.end);
});
it('should return the correct period range for last 6 months', () => {
const period = getClosedPeriod({ startOf: 'month', subtract: { amount: 6, unit: 'months' } })(false);
it('should return the correct period range for this week using local time', () => {
const weekExpectedReturn = {
start: new Date('5/19/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};
expect(period.start).toEqual(new Date('2023-11-01T00:00:00.000'));
expect(period.end).toEqual(new Date('2024-05-19T23:59:59.999'));
});
const period = getClosedPeriod({ startOf: 'week' })(false);
it('should return the correct period range for this week', () => {
const period = getClosedPeriod({ startOf: 'week' })(false);
expect(period.start.toISOString().split('T')[0]).toEqual(weekExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(weekExpectedReturn.end);
expect(period.start).toEqual(new Date('2024-05-19T00:00:00.000'));
expect(period.end).toEqual(new Date('2024-05-19T23:59:59.999'));
});
});

@ -16,27 +16,17 @@ export const getClosedPeriod =
end: Date;
}) =>
(utc): { start: Date; end: Date } => {
const date = new Date();
const offsetForMoment = -(date.getTimezoneOffset() / 60);
let start = moment(date).utc();
let end = moment(date).utc();
const start = utc ? moment().utc() : moment();
const end = utc ? moment().utc() : moment();
if (subtract) {
const { amount, unit } = subtract;
start.subtract(amount, unit);
}
if (!utc) {
start = start.utcOffset(offsetForMoment);
end = end.utcOffset(offsetForMoment);
}
// moment.toDate() can only return the date in localtime, that's why we do the new Date conversion
// https://github.com/moment/moment-timezone/issues/644
return {
start: new Date(start.startOf(startOf).format('YYYY-MM-DD HH:mm:ss')),
end: new Date(end.endOf('day').format('YYYY-MM-DD HH:mm:ss')),
start: start.startOf(startOf).toDate(),
end: end.endOf('day').toDate(),
};
};

Loading…
Cancel
Save