Skip to content

A local-first personal finance app

fix: point the tour's To Budget step at a displayed month (#9038)

Mohammed Yasin Zuhayr6da9f6ac7842
diff --git a/packages/desktop-client/src/components/tour/steps.test.ts b/packages/desktop-client/src/components/tour/steps.test.ts
index b2e8f14158..4baab2f3db 100644
--- a/packages/desktop-client/src/components/tour/steps.test.ts
+++ b/packages/desktop-client/src/components/tour/steps.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from 'vitest';
+import { afterEach, describe, expect, it, vi } from 'vitest';
import { getTourSteps } from './steps';
import type { TourStepDeps } from './steps';
@@ -40,3 +40,79 @@ describe('getTourSteps', () => {
},
);
});
+
+// In test mode, monthUtils.currentMonth() returns '2017-01'
+const CURRENT_MONTH = '2017-01';
+
+type BudgetPageOptions = {
+ displayedMonths: string[];
+ summaryMonths: string[];
+};
+
+// Mirrors the budget page: the month picker marks the displayed months, and
+// the summaries strip renders one extra, clipped month on each side of them.
+function renderBudgetPage({
+ displayedMonths,
+ summaryMonths,
+}: BudgetPageOptions) {
+ document.body.innerHTML = [
+ ...displayedMonths.map(
+ month =>
+ `<div data-testid="selected-budget-month" data-month="${month}"></div>`,
+ ),
+ ...summaryMonths.map(
+ month => `<div data-testid="budget-summary" data-month="${month}"></div>`,
+ ),
+ ].join('');
+}
+
+function findBudgetSummaryTarget(): HTMLElement | null {
+ const steps = getTourSteps('budget-tour', deps);
+ const target = steps.find(step => step.id === 'budget-summary')?.target;
+ if (typeof target !== 'function') {
+ throw new Error('Expected the budget summary step to resolve its target');
+ }
+ return target();
+}
+
+describe('budget summary step target', () => {
+ afterEach(() => {
+ document.body.innerHTML = '';
+ });
+
+ it('targets the current month when it is displayed', () => {
+ renderBudgetPage({
+ displayedMonths: ['2016-12', CURRENT_MONTH],
+ summaryMonths: ['2016-11', '2016-12', CURRENT_MONTH, '2017-02'],
+ });
+
+ expect(findBudgetSummaryTarget()?.dataset.month).toBe(CURRENT_MONTH);
+ });
+
+ it('targets the first displayed month when the current month is not displayed', () => {
+ renderBudgetPage({
+ displayedMonths: ['2016-06', '2016-07'],
+ summaryMonths: ['2016-05', '2016-06', '2016-07', '2016-08'],
+ });
+
+ expect(findBudgetSummaryTarget()?.dataset.month).toBe('2016-06');
+ });
+
+ it('ignores a current month summary that is rendered but not displayed', () => {
+ renderBudgetPage({
+ displayedMonths: ['2016-11', '2016-12'],
+ summaryMonths: ['2016-10', '2016-11', '2016-12', CURRENT_MONTH],
+ });
+
+ expect(findBudgetSummaryTarget()?.dataset.month).toBe('2016-11');
+ });
+
+ it('falls back to the first summary when the month picker is not rendered', () => {
+ renderBudgetPage({
+ displayedMonths: [],
+ summaryMonths: ['2016-05', '2016-06'],
+ });
+
+ expect(findBudgetSummaryTarget()?.dataset.month).toBe('2016-05');
+ });
+});
diff --git a/packages/desktop-client/src/components/tour/steps.tsx b/packages/desktop-client/src/components/tour/steps.tsx
index 937bb1d9ca..6165f4bd73 100644
--- a/packages/desktop-client/src/components/tour/steps.tsx
+++ b/packages/desktop-client/src/components/tour/steps.tsx
@@ -14,10 +14,32 @@ export type TourStepDeps = {
budgetType: 'envelope' | 'tracking';
};
+function getDisplayedMonths(): string[] {
+ return Array.from(
+ document.querySelectorAll<HTMLElement>(
+ '[data-testid="selected-budget-month"]',
+ ),
+ )
+ .map(element => element.dataset.month)
+ .filter((month): month is string => Boolean(month));
+}
+
+// The budget summaries strip also renders the month before and after the
+// displayed months (clipped by its container) so it can animate between
+// months, so a summary being in the DOM does not mean it is on screen. Only
+// target the current month while the month picker shows it as displayed, and
+// otherwise fall back to the first displayed month.
function findBudgetSummary(): HTMLElement | null {
+ const displayedMonths = getDisplayedMonths();
+ const currentMonth = monthUtils.currentMonth();
+ const month =
+ displayedMonths.length === 0 || displayedMonths.includes(currentMonth)
+ ? currentMonth
+ : displayedMonths[0];
+
return (
document.querySelector<HTMLElement>(
- `[data-testid="budget-summary"][data-month="${monthUtils.currentMonth()}"]`,
+ `[data-testid="budget-summary"][data-month="${month}"]`,
) ?? document.querySelector<HTMLElement>('[data-testid="budget-summary"]')
);
}
diff --git a/upcoming-release-notes/tour-to-budget-displayed-month.md b/upcoming-release-notes/tour-to-budget-displayed-month.md
new file mode 100644
index 0000000000..8e620305f7
--- /dev/null
+++ b/upcoming-release-notes/tour-to-budget-displayed-month.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [YasinzHyper]
+---
+
+Fix the tour's "To Budget" step pointing off screen when the current month is not displayed