Skip to content

Commit

Permalink
pass extra actions in tests as parts (#875)
Browse files Browse the repository at this point in the history
* pass extra actions in tests as parts

* confirm form is stable in delete
  • Loading branch information
jbolda authored Dec 12, 2024
1 parent 4ecb125 commit e45cf85
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 37 deletions.
1 change: 1 addition & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default defineConfig({
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
timeout: 15000,
reporter: process.env.CI
? [
['html', { open: 'never' }],
Expand Down
41 changes: 22 additions & 19 deletions tests/helpers/elements.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import { test, expect, type Page } from '@playwright/test';

export const selectDate = async (
export const selectDate = (
page: Page,
label: string,
value: { day: string; month: string; year: string }
) => {
const dateField = page.getByLabel(label);
await page.getByText(label).click();
await page.keyboard.type(value.month);
await page.waitForTimeout(50);
await page.keyboard.type(value.day);
await page.waitForTimeout(50);
await page.keyboard.type(value.year);
await page.waitForTimeout(50);
await expect(dateField.locator('input')).toHaveValue(
`${value.year}-${value.month}-${value.day}`
);
};
) =>
test.step(`Pick ${label} on Date Picker`, async () => {
const dateField = page.getByLabel(label);
await page.getByText(label).click();
await page.keyboard.type(value.month);
await page.waitForTimeout(50);
await page.keyboard.type(value.day);
await page.waitForTimeout(50);
await page.keyboard.type(value.year);
await page.waitForTimeout(50);
await expect(dateField.locator('input')).toHaveValue(
`${value.year}-${value.month}-${value.day}`
);
});

export const selectOption = (page: Page, label: string, option: string) =>
test.step(`Select ${option} from ${label}`, async () => {
const selectInput = page.getByLabel(label, { exact: true });
// seems to be needed to properly handle click and opening select items
await selectInput.scrollIntoViewIfNeeded();
await expect(selectInput).toBeVisible();
await expect(async () => {
await selectInput.scrollIntoViewIfNeeded();
await selectInput.click();
await page.getByRole('option', { name: option }).click();
}).toPass();
await selectInput.click();
// seems the only way to reliably get focus
await page.keyboard.press('ArrowDown');

await page.getByRole('option', { name: option }).click();
});

export const selectOnlyTag = (
Expand Down
11 changes: 7 additions & 4 deletions tests/transactions/form-submission.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test, expect } from '@playwright/test';

import { selectOption } from '../helpers/elements';
import { navigateTo } from '../helpers/navigate';
import { addDefaultAccount, addGenericTransaction, selectOnly } from './helper';

Expand All @@ -25,7 +24,7 @@ test('submits simple transaction', async ({ page }, testInfo) => {
test('check income is listed in income tab after submit', async ({ page }) => {
await addGenericTransaction(page, {
value: '55.00',
extraActions: [selectOption(page, 'Transaction Type', 'Income')]
extraActions: [{ fn: 'selectOption', args: ['Transaction Type', 'Income'] }]
});

await selectOnly(page, 'Income');
Expand All @@ -38,7 +37,9 @@ test('check expense is listed in expense tab after submit', async ({
}) => {
await addGenericTransaction(page, {
value: '67.00',
extraActions: [selectOption(page, 'Transaction Type', 'Expense')]
extraActions: [
{ fn: 'selectOption', args: ['Transaction Type', 'Expense'] }
]
});

await selectOnly(page, 'Expenses');
Expand All @@ -51,7 +52,9 @@ test('check transfer is listed in transfer tab after submit', async ({
}) => {
await addGenericTransaction(page, {
value: '53',
extraActions: [selectOption(page, 'Transaction Type', 'Transfer')]
extraActions: [
{ fn: 'selectOption', args: ['Transaction Type', 'Transfer'] }
]
});

await selectOnly(page, 'Transfers');
Expand Down
33 changes: 24 additions & 9 deletions tests/transactions/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,33 @@ export const addDefaultAccount = async (page: Page) => {
});
};

export const addGenericTransaction = async (
type SelectOptionsParams = Parameters<typeof selectOption>;
type ExtraActions = {
fn: 'selectOption';
args: [SelectOptionsParams[1], SelectOptionsParams[2]];
}[];

export const addGenericTransaction = (
page: Page,
{ value, extraActions }: { value: string; extraActions: Promise<any>[] } = {
{
value,
extraActions
}: {
value: string;
extraActions: ExtraActions;
} = {
value: '55.00',
extraActions: []
}
) => {
await test.step('Add Generic Transaction', async () => {
const addButton = page.getByText('Add Transaction');
await addButton.click();

) =>
test.step('Add Generic Transaction', async () => {
await page.getByText('Add Transaction').click();
// confirms the form is loaded and stable
await expect(page.getByText('Add a Transaction')).toBeAttached();

await page.getByLabel('description').fill('test transaction');
await page.getByLabel('Category').fill('generic');

await selectOption(page, 'Account', 'Test Account Submission');
await selectOption(page, 'Repeat Type', 'No Repeating');

Expand All @@ -39,15 +51,18 @@ export const addGenericTransaction = async (

if (extraActions) {
for (let pageAction of extraActions) {
await pageAction;
if (pageAction.fn === 'selectOption') {
// the tracing and actions gets real weird if we don't specifically
// pass it and call it here
await selectOption(page, pageAction.args[0], pageAction.args[1]);
}
}
}

await page.getByLabel('value').first().fill(value);
await page.keyboard.press('Enter');
await expect(page.locator('table').getByText(value)).toBeVisible();
});
};

const possibleTransactionTypes = ['Income', 'Expenses', 'Transfers'] as const;
export const selectOnly = (
Expand Down
14 changes: 9 additions & 5 deletions tests/transactions/transaction-delete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ test.beforeEach(async ({ page }) => {
await page.goto('/');
await addDefaultAccount(page);
await navigateTo(page, 'Planning');

await page.getByText('Add Transaction').click();
// confirms the form is loaded and stable
await expect(page.getByText('Add a Transaction')).toBeAttached();

await selectOption(page, 'Account', 'Test Account Submission');
await page.getByLabel('value').first().fill('55');
await selectOption(page, 'Account', 'Test Account Submission'); //
await page.getByLabel('value').first().fill('55'); //
await page.getByLabel('ending').click();
await selectDate(page, 'Start Date', {
//
month: '01',
day: '01',
year: '2024'
});
await selectOption(page, 'Repeat Type', 'No Repeating');
await page.getByLabel('Category').fill('generic');
await page.getByLabel('description').fill('test transaction');
await selectOption(page, 'Repeat Type', 'No Repeating'); //
await page.getByLabel('Category').fill('generic'); //
await page.getByLabel('description').fill('test transaction'); //
await page.keyboard.press('Enter');
});

Expand Down

0 comments on commit e45cf85

Please sign in to comment.