Add test for quarter end date function

This commit is contained in:
Brenda Sukh
2020-12-08 12:15:12 -05:00
parent b81c973f2e
commit 8f996c88b0
2 changed files with 26 additions and 5 deletions
@@ -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);
});
});
+10 -4
View File
@@ -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);
}