Add test for quarter end date function
This commit is contained in:
@@ -15,7 +15,11 @@
|
||||
*/
|
||||
|
||||
import { Duration } from '../types';
|
||||
import { inclusiveEndDateOf, inclusiveStartDateOf } from './duration';
|
||||
import {
|
||||
inclusiveEndDateOf,
|
||||
inclusiveStartDateOf,
|
||||
quarterEndDate,
|
||||
} from './duration';
|
||||
|
||||
const lastCompleteBillingDate = '2020-06-05';
|
||||
|
||||
@@ -32,3 +36,14 @@ describe.each`
|
||||
expect(inclusiveEndDateOf(duration, lastCompleteBillingDate)).toBe(endDate);
|
||||
});
|
||||
});
|
||||
|
||||
describe.each`
|
||||
inclusiveEndDate | expectedQuarterEndDate
|
||||
${'2020-12-31'} | ${'2020-12-31'}
|
||||
${'2020-12-30'} | ${'2020-09-30'}
|
||||
${'2021-02-19'} | ${'2020-12-31'}
|
||||
`('quarterEndDate', ({ inclusiveEndDate, expectedQuarterEndDate }) => {
|
||||
it(`calculates quarter end date correctly from inclusive end date ${inclusiveEndDate}`, () => {
|
||||
expect(quarterEndDate(inclusiveEndDate)).toBe(expectedQuarterEndDate);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -60,7 +60,10 @@ export function exclusiveEndDateOf(
|
||||
.add(1, 'day')
|
||||
.format(DEFAULT_DATE_FORMAT);
|
||||
case Duration.P3M:
|
||||
return quarterEndDate(inclusiveEndDate);
|
||||
return moment(quarterEndDate(inclusiveEndDate))
|
||||
.utc()
|
||||
.add(1, 'day')
|
||||
.format(DEFAULT_DATE_FORMAT);
|
||||
default:
|
||||
return assertNever(duration);
|
||||
}
|
||||
@@ -81,11 +84,14 @@ export function intervalsOf(duration: Duration, inclusiveEndDate: string) {
|
||||
return `R2/${duration}/${exclusiveEndDateOf(duration, inclusiveEndDate)}`;
|
||||
}
|
||||
|
||||
function quarterEndDate(inclusiveEndDate: string): string {
|
||||
export function quarterEndDate(inclusiveEndDate: string): string {
|
||||
const endDate = moment(inclusiveEndDate).utc();
|
||||
const endOfQuarter = endDate.endOf('quarter').format(DEFAULT_DATE_FORMAT);
|
||||
if (endOfQuarter === inclusiveEndDate) {
|
||||
return endDate.add(1, 'day').format(DEFAULT_DATE_FORMAT);
|
||||
return endDate.format(DEFAULT_DATE_FORMAT);
|
||||
}
|
||||
return endDate.startOf('quarter').format(DEFAULT_DATE_FORMAT);
|
||||
return endDate
|
||||
.startOf('quarter')
|
||||
.subtract(1, 'day')
|
||||
.format(DEFAULT_DATE_FORMAT);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user