Files
medassist-ng/frontend/e2e/settings.spec.ts
T
Daniel Volz 98939877db feat: expand Playwright E2E coverage (#155)
* feat: comprehensive Playwright E2E test rewrite

Rewrite all E2E tests with correct CSS selectors, add new spec files,
and implement robust auth handling to work within backend rate limits.

Changes:
- Rewrite fixtures/index.ts with JWT-based /auth/me mock to avoid
  10 req/min rate limit on /auth/me during test runs
- Rewrite auth.setup.ts with offline JWT validity check to reuse
  existing auth state across runs (saves login rate-limit budget)
- Rewrite auth.spec.ts (6 tests) - login page, fields, submit,
  redirect guard, invalid credentials, login/register toggle
- Rewrite dashboard.spec.ts (8 tests) - header, nav tabs,
  navigation, overview/schedules sections, days selector, redirect
- Rewrite medications.spec.ts (8 tests) - form fields, stock
  inventory, package type toggle, intake schedule, save/cancel,
  unsaved changes guard
- Rewrite settings.spec.ts (12 tests) - language, notification
  matrix, thresholds, calculation mode, toggle switch, export/import,
  user menu navigation
- Create planner.spec.ts (9 tests) - form, date inputs, calculate,
  reset, checkbox, submit, tab state, eyebrow heading
- Create schedule.spec.ts (12 tests) - timeline, days selector,
  past/future toggles, day blocks, today highlight, collapse/expand,
  overview table, share button
- Update playwright.config.ts: remove mobile projects, enable
  webServer section for CI
- Add .github/workflows/e2e.yml CI workflow for Playwright tests

Total: 57 E2E tests across 6 spec files, all passing consistently
across 5+ consecutive runs without backend restart.

Closes #154

* feat: add comprehensive E2E data tests with medication CRUD, dashboard, planner, schedule

Add 48 new Playwright E2E tests covering real medication data scenarios:
- medication-crud: 14 tests for create/edit/delete/list via UI form
- dashboard-data: 13 tests for overview table, timeline, dose tracking
- planner-data: 9 tests for demand calculator with results/status chips
- schedule-data: 11 tests for timeline, collapse/expand, dose mark/undo

Infrastructure improvements:
- Add API helpers (createMedicationViaAPI, deleteMedicationViaAPI,
  deleteAllMedicationsViaAPI) with retry logic for rate-limit resilience
- Configure chromium-data project for serial execution with retry:1
- Add /auth/me mock to avoid rate-limit exhaustion on auth endpoint
- Increase navigateTo reliability with networkidle waits
- Increase auth token validity threshold from 2 to 10 minutes
- Make backend rate limit configurable via RATE_LIMIT_MAX env var
- Set RATE_LIMIT_MAX=300 in dev docker-compose for E2E test support

Total suite: 57 empty-state + 48 data tests = 105 tests (chromium)

* test: add E2E tests for medication editing, stock status, and share schedule

- medication-edit.spec.ts: 10 tests covering generic name, notes,
  taken-by add/remove, expiry date, refill, intake schedule editing,
  adding intake rows, reminder toggle, and package type changes
- stock-status.spec.ts: 12 tests verifying dashboard shows correct
  status chips (High/Normal/Warning/Danger) for different stock levels,
  overview table, reorder card, detail modal, and planner integration
- share-schedule.spec.ts: 10 tests for taken-by badges, share button,
  share dialog, link generation, shared schedule page navigation,
  dose tracking on shared page, and notes display
- fixtures/index.ts: add createShareTokenViaAPI, updateSettingsViaAPI
  helpers; expand createMedicationViaAPI with takenBy, notes, expiryDate
- playwright.config.ts: update testMatch/testIgnore for new test files
- docker-compose.dev.yml: increase RATE_LIMIT_MAX to 1000 for E2E tests

* docs: refine release-manager instructions for CLI safety and commit-linked release notes

* fix: resolve PR155 CI failures for frontend lint and e2e proxy

* fix: stabilize auth-related e2e checks in CI
2026-02-12 20:06:11 +01:00

188 lines
6.1 KiB
TypeScript

import { expect } from "@playwright/test";
import { authFile, navigateTo, test } from "./fixtures";
/**
* Settings Page E2E Tests
*
* Verifies settings form sections: language, notifications,
* stock thresholds, export/import, and the save workflow.
*/
test.describe("Settings Page", () => {
test.use({ storageState: authFile });
test("should display settings form", async ({ page }) => {
await navigateTo(page, "/settings");
await expect(page.locator("form.settings-form")).toBeVisible();
});
test("should show language section with select", async ({ page }) => {
await navigateTo(page, "/settings");
const languageSelect = page.locator("select.language-select");
await expect(languageSelect).toBeVisible();
// Should have at least English and German
await expect(languageSelect.locator("option")).toHaveCount(2);
});
test("should allow switching language", async ({ page }) => {
await navigateTo(page, "/settings");
const languageSelect = page.locator("select.language-select");
const currentValue = await languageSelect.inputValue();
// Switch to the other language
const targetLang = currentValue === "en" ? "de" : "en";
await languageSelect.selectOption(targetLang);
await expect(languageSelect).toHaveValue(targetLang);
// Switch back to original
await languageSelect.selectOption(currentValue);
await expect(languageSelect).toHaveValue(currentValue);
});
test("should show notification matrix", async ({ page }) => {
await navigateTo(page, "/settings");
const matrix = page.locator("div.notification-matrix");
await expect(matrix).toBeVisible();
// Matrix contains toggle switches
const toggles = matrix.locator("label.toggle-switch");
expect(await toggles.count()).toBeGreaterThanOrEqual(2);
});
test("should show stock settings section with threshold inputs", async ({ page }) => {
await navigateTo(page, "/settings");
const thresholdGroup = page.locator("div.threshold-chips-group");
await expect(thresholdGroup).toBeVisible();
// Should have three threshold number inputs
const thresholdInputs = thresholdGroup.locator('input[type="number"]');
await expect(thresholdInputs).toHaveCount(3);
});
test("should show calculation mode radio cards", async ({ page }) => {
await navigateTo(page, "/settings");
const modeGroup = page.locator("div.calculation-mode-group");
await expect(modeGroup).toBeVisible();
// Two radio cards: automatic and manual
const radioCards = modeGroup.locator("label.radio-card");
await expect(radioCards).toHaveCount(2);
// One should be selected
await expect(modeGroup.locator("label.radio-card.selected")).toHaveCount(1);
});
test("should toggle calculation mode", async ({ page }) => {
await navigateTo(page, "/settings");
const modeGroup = page.locator("div.calculation-mode-group");
const radioCards = modeGroup.locator("label.radio-card");
// Find the non-selected card and click it
const firstSelected = await radioCards.first().evaluate((el) => el.classList.contains("selected"));
const targetCard = firstSelected ? radioCards.nth(1) : radioCards.first();
await targetCard.click();
await expect(targetCard).toHaveClass(/selected/);
// Click the other one back
const otherCard = firstSelected ? radioCards.first() : radioCards.nth(1);
await otherCard.click();
await expect(otherCard).toHaveClass(/selected/);
});
test("should have save button in form footer", async ({ page }) => {
await navigateTo(page, "/settings");
const saveButton = page.locator('div.form-footer > button[type="submit"]');
await expect(saveButton).toBeVisible();
});
test("should show export/import section", async ({ page }) => {
await navigateTo(page, "/settings");
// Export button
const exportBtn = page.locator("div.action-card button.secondary").first();
await expect(exportBtn).toBeVisible();
});
test("should toggle a notification switch", async ({ page }) => {
await navigateTo(page, "/settings");
// Find all toggle-switch labels on the entire settings page
const allToggleLabels = page.locator("label.toggle-switch");
const count = await allToggleLabels.count();
// Find the first toggle that is NOT disabled
let enabledToggle = null;
for (let i = 0; i < count; i++) {
const label = allToggleLabels.nth(i);
const isDisabled = await label.evaluate((el) => el.classList.contains("disabled"));
if (!isDisabled) {
enabledToggle = label;
break;
}
}
if (!enabledToggle) {
// All toggles disabled (no notification channels configured) — skip
return;
}
const checkbox = enabledToggle.locator('input[type="checkbox"]');
const initialState = await checkbox.isChecked();
// Click the label to toggle
await enabledToggle.click();
if (initialState) {
await expect(checkbox).not.toBeChecked();
} else {
await expect(checkbox).toBeChecked();
}
// Toggle back to restore original state
await enabledToggle.click();
await expect(checkbox).toHaveJSProperty("checked", initialState);
});
test("should validate stock thresholds", async ({ page }) => {
await navigateTo(page, "/settings");
const thresholdGroup = page.locator("div.threshold-chips-group");
const inputs = thresholdGroup.locator('input[type="number"]');
// Set an invalid value (critical > low)
const criticalInput = inputs.first();
await criticalInput.fill("999");
// Should show validation error
const validationError = page.locator("p.threshold-validation-error");
await expect(validationError).toBeVisible();
});
test("should reach settings via user menu", async ({ page }) => {
await navigateTo(page, "/dashboard");
const userMenuButton = page.locator("button.user-menu-btn");
test.skip(!(await userMenuButton.isVisible().catch(() => false)), "User menu is unavailable when auth is disabled");
// Open user menu
await userMenuButton.click();
// Click settings option in dropdown
const settingsOption = page.locator(".user-dropdown").getByText(/Settings/i);
await expect(settingsOption).toBeVisible();
await settingsOption.click();
await expect(page).toHaveURL(/\/settings/);
await expect(page.locator("form.settings-form")).toBeVisible();
});
});