Files

286 lines
12 KiB
TypeScript

import { expect, test, type Request } from '@playwright/test';
import { prepared } from './overview-fixture';
import { historyRowCount } from './history-fixture';
test('homepage periods select one loaded snapshot and history without data requests', async ({
page
}) => {
await page.setViewportSize({ width: 1440, height: 900 });
await prepared(page);
const controls = page.getByRole('button', { name: 'Overview period', exact: true });
const transactions = page
.getByText('Transactions', { exact: true })
.locator('..')
.getByRole('definition');
await expect(transactions).toHaveText((10 + historyRowCount).toLocaleString('en-US'));
const incomeRows = page
.getByRole('table', { name: 'Monthly income and spending', exact: true })
.locator('tbody tr');
await expect(controls).toHaveText('3 months');
await expect(incomeRows).toHaveCount(6);
await expect(incomeRows.nth(4).getByRole('rowheader')).toHaveText('2026-04');
await expect(incomeRows.nth(4).getByRole('cell')).toHaveText(['Income', '1000.00', 'USD']);
await expect(incomeRows.nth(5).getByRole('rowheader')).toHaveText('2026-04');
await expect(incomeRows.nth(5).getByRole('cell')).toHaveText(['Spending', '100.00', 'USD']);
await expect(
page.getByRole('img', { name: 'Income and spending by month in USD', exact: true })
).toBeVisible();
const latest = page
.getByRole('region', { name: 'Key figures', exact: true })
.getByText('Latest net worth', { exact: true })
.locator('..')
.getByRole('definition');
await expect(latest).toHaveText('$11,825.00');
const latestText = await latest.innerText();
await expect(page.getByRole('region', { name: 'Journal summary', exact: true })).toHaveCount(0);
await expect(page.getByRole('region', { name: 'Net worth trend', exact: true })).toHaveCount(0);
const mainCards = [
page.getByRole('region', { name: 'Income and spending', exact: true }),
page.getByRole('region', { name: 'Spending by category', exact: true })
];
async function expectCardSizes() {
const boxes = await Promise.all(mainCards.map((card) => card.boundingBox()));
const first = boxes[0]!;
const figures = page.getByRole('region', { name: 'Key figures', exact: true });
const income = (await figures
.getByText('Income', { exact: true })
.locator('..')
.boundingBox())!;
const spending = (await figures
.getByText('Spending', { exact: true })
.locator('..')
.boundingBox())!;
const titleSpan =
Math.abs(income.y - spending.y) < 1 ? spending.x + spending.width - income.x : income.width;
expect(first.width).toBeCloseTo(titleSpan, 0);
for (const box of boxes) expect(box!.width).toBeCloseTo(first.width, 0);
for (const box of boxes) expect(box!.height).toBeCloseTo(first.height, 0);
}
const categoryCard = mainCards[1];
const categoryCharts = categoryCard.getByRole('region', {
name: 'Spending by category charts',
exact: true
});
const categoryRows = categoryCard
.getByRole('table', { name: 'Spending by account', exact: true })
.locator('tbody tr');
const clothing = categoryRows.filter({
has: page.getByRole('rowheader', { name: 'expenses:clothing', exact: true })
});
const foreign = categoryRows.filter({
has: page.getByRole('rowheader', { name: 'expenses:foreign', exact: true })
});
const refund = categoryRows.filter({
has: page.getByRole('rowheader', { name: 'expenses:foreign-refund', exact: true })
});
const currency = categoryCard.getByRole('combobox', {
name: 'Spending chart currency',
exact: true
});
const chartValues = categoryCard.locator('#expense-categories-chart-values');
const usdChartValues = [
'expenses:travel: 50.00 USD',
'expenses:food: 20.00 USD',
'expenses:utilities: 10.00 USD',
'expenses:books: 8.00 USD',
'expenses:health: 6.00 USD',
'Other (grouped): 6.00 USD'
];
async function expectContainedCategoryChart(commodity = 'USD') {
await expect(
categoryCharts.getByRole('img', {
name: `Spending by category in ${commodity}`,
exact: true
})
).toBeVisible();
await expect(categoryCharts.getByRole('img')).toHaveCount(1);
await expect
.poll(() =>
categoryCharts.evaluate(
(node) => node.scrollHeight <= node.clientHeight && node.scrollWidth <= node.clientWidth
)
)
.toBe(true);
}
await expectCardSizes();
await expect(categoryRows).toHaveCount(7);
await expect(clothing.getByRole('cell')).toHaveText(['4.00', 'USD']);
await expect(foreign).toHaveCount(0);
await expect(currency).toHaveCount(0);
await expectContainedCategoryChart();
const usdCanvas = categoryCharts.getByRole('img', {
name: 'Spending by category in USD',
exact: true
});
await expect(usdCanvas).toHaveAttribute('aria-describedby', 'expense-categories-chart-values');
await expect(chartValues).not.toBeVisible();
// The closed native disclosure still describes the plotted result for assistive technology.
await expect(usdCanvas).toHaveAccessibleDescription(usdChartValues.join(' '));
const summary = categoryCard.locator('summary', { hasText: /^Chart values$/ });
await summary.focus();
await summary.press('Enter');
await expect(chartValues).toBeVisible();
await expect(chartValues.getByRole('listitem')).toHaveText(usdChartValues);
const spendingAmounts = page
.getByRole('region', { name: 'Key figures', exact: true })
.getByText('Spending', { exact: true })
.locator('..')
.getByRole('listitem');
await expect(spendingAmounts).toHaveText(['$100.00']);
const accounts = page.getByRole('complementary', { name: 'Accounts', exact: true });
const accountsText = await accounts.innerText();
const requests: string[] = [];
const observe = (request: Request) => {
if (
['fetch', 'xhr', 'document'].includes(request.resourceType()) ||
request.method() === 'POST'
) {
requests.push(`${request.method()} ${new URL(request.url()).pathname}`);
}
};
page.on('request', observe);
await page.evaluate(() => Object.assign(window, { periodNavigationToken: 'retained' }));
await controls.focus();
await controls.press('Enter');
await expect(page.getByRole('listbox')).toBeVisible();
await page.keyboard.press('End');
await page.keyboard.press('Enter');
await expect(page).toHaveURL(/\?months=12$/);
await expect(controls).toHaveText('12 months');
await expect(page.getByRole('listbox')).not.toBeVisible();
await expect(incomeRows).toHaveCount(24);
await expect(transactions).toHaveText((11 + historyRowCount).toLocaleString('en-US'));
await expect(controls).toBeFocused();
await expectCardSizes();
await expectContainedCategoryChart();
await expect(spendingAmounts).toHaveText(['€0.00', '$100.00']);
const januarySpending = incomeRows.filter({ hasText: '2026-01' }).filter({ hasText: 'Spending' });
await expect(januarySpending.getByRole('cell')).toHaveText(['Spending', '0.00', 'EUR']);
// One full native inventory: neither Top 5/Other nor chart currency filters this table.
await expect(categoryRows.getByRole('rowheader')).toHaveText([
'expenses:living',
'expenses:travel',
'expenses:food',
'expenses:utilities',
'expenses:books',
'expenses:health',
'expenses:clothing',
'expenses:foreign',
'expenses:foreign-refund'
]);
await expect(categoryRows.getByRole('cell')).toHaveText([
'2.00',
'USD',
'50.00',
'USD',
'20.00',
'USD',
'10.00',
'USD',
'8.00',
'USD',
'6.00',
'USD',
'4.00',
'USD',
'5.00',
'EUR',
'-5.00',
'EUR'
]);
await expect(currency).toHaveValue('USD');
await expect(currency.locator('option')).toHaveText(['USD', 'EUR']);
await currency.selectOption('EUR');
await expectContainedCategoryChart('EUR');
await expect(usdCanvas).toHaveCount(0);
await expect(chartValues.getByRole('listitem')).toHaveText([
'expenses:foreign: 5.00 EUR',
'expenses:foreign-refund: -5.00 EUR'
]);
await expect(clothing.getByRole('cell')).toHaveText(['4.00', 'USD']);
await expect(refund.getByRole('cell')).toHaveText(['-5.00', 'EUR']);
const details = page.getByRole('region', { name: 'Income and spending details', exact: true });
await page.mouse.move(0, 0);
await expect(
details.locator('..').locator('[data-scroll-area-scrollbar][data-orientation="vertical"]')
).toBeVisible();
const tableHeading = details.locator('thead');
await details.focus();
const headingBefore = await tableHeading.boundingBox();
await page.keyboard.press('PageDown');
await expect.poll(() => details.evaluate((node) => node.scrollTop)).toBeGreaterThan(0);
const headingAfter = await tableHeading.boundingBox();
expect(headingAfter!.y).toBeCloseTo(headingBefore!.y, 0);
// Leaving January removes EUR amounts altogether, not just their geometry.
await page.goBack();
await expect(controls).toHaveText('3 months');
await expect(incomeRows).toHaveCount(6);
await expect(transactions).toHaveText((10 + historyRowCount).toLocaleString('en-US'));
await expect(currency).toHaveCount(0);
await expectContainedCategoryChart();
await expect(chartValues.getByRole('listitem')).toHaveText(usdChartValues);
await expect(categoryRows).toHaveCount(7);
await expect(foreign).toHaveCount(0);
await expect(refund).toHaveCount(0);
await expect(clothing.getByRole('cell')).toHaveText(['4.00', 'USD']);
await expect(spendingAmounts).toHaveText(['$100.00']);
await controls.press('Enter');
await expect(page.getByRole('option', { name: '3 months', exact: true })).toHaveAttribute(
'aria-selected',
'true'
);
await page.keyboard.press('Escape');
await expect(page.getByRole('listbox')).not.toBeVisible();
await expect(controls).toBeFocused();
await page.goForward();
await expect(controls).toHaveText('12 months');
await expect(currency.locator('option')).toHaveText(['USD', 'EUR']);
await expect(foreign.getByRole('cell')).toHaveText(['5.00', 'EUR']);
await currency.selectOption('USD');
await expectContainedCategoryChart();
await expect(chartValues.getByRole('listitem')).toHaveText(usdChartValues);
await expect(refund.getByRole('cell')).toHaveText(['-5.00', 'EUR']);
expect(await latest.innerText()).toBe(latestText);
expect(await accounts.innerText()).toBe(accountsText);
expect(await page.evaluate(() => Reflect.get(window, 'periodNavigationToken'))).toBe('retained');
expect(requests).toEqual([]);
page.off('request', observe);
await page
.getByRole('navigation', { name: 'Primary', exact: true })
.getByRole('link', { name: 'Overview', exact: true })
.click();
await expect(page).toHaveURL(/\/$/);
await expect(controls).toHaveText('3 months');
await expect(incomeRows).toHaveCount(6);
await page.goto('/?months=6');
await expect(controls).toHaveText('6 months');
await expect(incomeRows).toHaveCount(12);
await expect(transactions).toHaveText((11 + historyRowCount).toLocaleString('en-US'));
await expectContainedCategoryChart();
await expect(spendingAmounts).toHaveText(['€0.00', '$100.00']);
await expect(januarySpending.getByRole('cell')).toHaveText(['Spending', '0.00', 'EUR']);
await expect(clothing.getByRole('cell')).toHaveText(['4.00', 'USD']);
await expect(refund.getByRole('cell')).toHaveText(['-5.00', 'EUR']);
expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(1440);
await page.setViewportSize({ width: 390, height: 844 });
await page.emulateMedia({ colorScheme: 'dark' });
await expectCardSizes();
await expectContainedCategoryChart();
await expect(clothing.getByRole('cell')).toHaveText(['4.00', 'USD']);
await expect(foreign.getByRole('cell')).toHaveText(['5.00', 'EUR']);
await controls.click();
await expect(page.getByRole('option', { name: '12 months', exact: true })).toBeInViewport();
await page.keyboard.press('Escape');
await expect(controls).toBeFocused();
await expect(page.getByRole('region', { name: 'Key figures', exact: true })).toBeVisible();
await expect(
page.getByRole('table', { name: 'Monthly income and spending', exact: true })
).toBeVisible();
expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(390);
});