Skip to content

A local-first personal finance app

🐛 fixed current month upcoming calculation (#4629)

Alec Bakholdin2c4c5014eab8
diff --git a/packages/loot-core/src/client/data-hooks/transactions.ts b/packages/loot-core/src/client/data-hooks/transactions.ts
index 9099f9bae3..2fc3286774 100644
--- a/packages/loot-core/src/client/data-hooks/transactions.ts
+++ b/packages/loot-core/src/client/data-hooks/transactions.ts
@@ -187,15 +187,15 @@ export function usePreviewTransactions(): UsePreviewTransactionsResult {
while (day <= upcomingPeriodEnd) {
const nextDate = getNextDate(dateConditions, day);
- if (parseDate(nextDate) > upcomingPeriodEnd) break;
+ if (d.startOfDay(parseDate(nextDate)) > upcomingPeriodEnd) break;
if (dates.includes(nextDate)) {
- day = parseDate(addDays(day, 1));
+ day = d.startOfDay(parseDate(addDays(day, 1)));
continue;
}
dates.push(nextDate);
- day = parseDate(addDays(nextDate, 1));
+ day = d.startOfDay(parseDate(addDays(nextDate, 1)));
}
}
diff --git a/packages/loot-core/src/shared/schedules.test.ts b/packages/loot-core/src/shared/schedules.test.ts
index 143c6dc6e1..ac4743a899 100644
--- a/packages/loot-core/src/shared/schedules.test.ts
+++ b/packages/loot-core/src/shared/schedules.test.ts
@@ -418,17 +418,27 @@ describe('schedules', () => {
describe('getUpcomingDays', () => {
it.each([
- ['1', 1],
- ['7', 7],
- ['14', 14],
- ['oneMonth', 32],
- ['currentMonth', 31],
- ['2-day', 2],
- ['5-week', 35],
- ['3-month', 91],
- ['4-year', 1462],
- ])('value of %s returns %i days', (value: string, expected: number) => {
- expect(getUpcomingDays(value)).toEqual(expected);
- });
+ ['1', 1, '2017-01-01'],
+ ['7', 7, '2017-01-01'],
+ ['14', 14, '2017-01-01'],
+ ['oneMonth', 31, '2017-01-01'],
+ ['oneMonth', 30, '2017-04-01'],
+ ['oneMonth', 30, '2017-04-15'],
+ ['oneMonth', 28, '2017-02-01'],
+ ['oneMonth', 29, '2020-02-01'], // leap-year
+ ['currentMonth', 30, '2017-01-01'],
+ ['currentMonth', 27, '2017-02-01'],
+ ['currentMonth', 20, '2017-02-08'],
+ ['currentMonth', 28, '2020-02-01'], // leap-year
+ ['2-day', 2, '2017-01-01'],
+ ['5-week', 35, '2017-01-01'],
+ ['3-month', 91, '2017-01-01'],
+ ['4-year', 1462, '2017-01-01'],
+ ])(
+ 'value of %s on returns %i days on %s',
+ (value: string, expected: number, date: string) => {
+ expect(getUpcomingDays(value, date)).toEqual(expected);
+ },
+ );
});
});
diff --git a/packages/loot-core/src/shared/schedules.ts b/packages/loot-core/src/shared/schedules.ts
index de747b18e3..6d050925df 100644
--- a/packages/loot-core/src/shared/schedules.ts
+++ b/packages/loot-core/src/shared/schedules.ts
@@ -401,22 +401,22 @@ export function describeSchedule(schedule, payee) {
}
}
-export function getUpcomingDays(upcomingLength = '7'): number {
- const today = monthUtils.currentDay();
+export function getUpcomingDays(
+ upcomingLength = '7',
+ today = monthUtils.currentDay(), // for testability
+): number {
const month = monthUtils.getMonth(today);
switch (upcomingLength) {
case 'currentMonth': {
const day = monthUtils.getDay(today);
const end = monthUtils.getDay(monthUtils.getMonthEnd(today));
- return end - day + 1;
+ return end - day;
}
case 'oneMonth': {
- return (
- monthUtils.differenceInCalendarDays(
- monthUtils.nextMonth(month),
- month,
- ) + 1
+ return monthUtils.differenceInCalendarDays(
+ monthUtils.nextMonth(month),
+ month,
);
}
default:
diff --git a/upcoming-release-notes/4629.md b/upcoming-release-notes/4629.md
new file mode 100644
index 0000000000..79d66aec13
--- /dev/null
+++ b/upcoming-release-notes/4629.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [alecbakholdin]
+---
+
+Fixed bug in displaying upcoming tag in schedule view when using current month