Skip to content

A local-first personal finance app

2.8 KB
Raw Blame
import type { Locator, Page } from '@playwright/test';

import { BootstrapPage } from './bootstrap-page';
import { BudgetPage } from './budget-page';

export class ConfigurationPage {
  readonly page: Page;
  readonly heading: Locator;

  constructor(page: Page) {
    this.page = page;

    this.heading = page.getByRole('heading');
  }

  async createTestFile() {
    await this.page
      .getByRole('button', { name: 'Try the demo', exact: true })
      .click();
    const budgetPage = new BudgetPage(this.page);
    // Wait for the budget page to be fully mounted before returning so
    // callers don't race the virtualized budget-table's layout step.
    await budgetPage.waitFor();
    return budgetPage;
  }

  async createDemoFile() {
    await this.page.getByRole('button', { name: 'Try the demo' }).click();
    const budgetPage = new BudgetPage(this.page);
    await budgetPage.waitFor();
    return budgetPage;
  }

  async clickOnConnectServer() {
    await this.page
      .getByRole('button', { name: 'Connect to a sync server' })
      .click();
  }

  async clickOnNoServer() {
    await this.page.getByRole('button', { name: "Don't use a server" }).click();
  }

  async clickOnStartSyncServer() {
    await this.page.getByRole('button', { name: 'Start' }).click();
    await this.page.waitForURL('**/bootstrap');
    return new BootstrapPage(this.page);
  }

  async startFresh() {
    await this.page.getByRole('button', { name: 'Start budgeting' }).click();

    const budgetPage = new BudgetPage(this.page);
    await budgetPage.waitFor();
    return budgetPage;
  }

  async importBudget(type: 'YNAB4' | 'nYNAB' | 'Actual', file: string) {
    const fileChooserPromise = this.page.waitForEvent('filechooser');
    await this.page.getByRole('button', { name: 'Import my budget' }).click();

    switch (type) {
      case 'YNAB4':
        await this.page
          .getByRole('button', {
            name: 'YNAB4 The old unsupported desktop app',
          })
          .click();
        await this.page
          .getByRole('button', { name: 'Select zip file...' })
          .click();
        break;

      case 'nYNAB':
        await this.page
          .getByRole('button', { name: 'nYNAB The newer web app' })
          .click();
        await this.page.getByRole('button', { name: 'Select file...' }).click();
        break;

      case 'Actual':
        await this.page
          .getByRole('button', {
            name: 'Actual Import a file exported from Actual',
          })
          .click();
        await this.page.getByRole('button', { name: 'Select file...' }).click();
        break;

      default:
        throw new Error(`Unrecognized import type: ${String(type)}`);
    }

    const fileChooser = await fileChooserPromise;
    await fileChooser.setFiles(file);

    return new BudgetPage(this.page);
  }
}