Skip to content

Add frontend tests, centralize API URL and fix coverage rate translation - #2

Merged
kris7011 merged 2 commits into
mainfrom
frontend/add-vitest-tests
Oct 16, 2026
Merged

kris7011 merged 2 commits into
mainfrom
frontend/add-vitest-tests

Conversation

@kris7011

Copy link
Copy Markdown
Owner

Stacked on #1. Review and merge #1 first. GitHub then retargets this PR to main.

Problem

  • The React dashboard had no tests (about 2,100 lines with a lot of status and translation logic).
  • Bug: coverage rate shown as "7857%". The API builds "Schedule coverage is {coverageRate}%." with the server's culture. translateRiskMessage then called .replace(".", ""), which removes the first period. On a Danish machine (78,57%.) that happens to be the final period. With an English or invariant culture, for example in the Docker image or on a typical Linux/cloud host, the decimal point is removed and the dashboard shows "Vagtplanens dækningsgrad er 7857%."
  • API_BASE_URL = "http://localhost:5026" was duplicated in 9 API modules and could not be configured.

Solution

  • Bug fix: strip only the sentence-ending period (/\.$/). It works for both 78,57% and 78.57%.
  • src/api/config.ts: one API_BASE_URL, configurable via VITE_API_BASE_URL (with a new .env.example). The default is unchanged.
  • Tests: Vitest 5 + React Testing Library + jsdom.
    • fetch is stubbed with fixtures captured from the real API after POST /api/demo/reset (src/test/fixtures/*.json). The coverage message uses invariant formatting (78.57%), as on CI.
    • App.test.tsx: overview rendering and Danish translations, error page with "Prøv igen", dashboard staying visible while refreshing after "Generér vagtplan", preference profiles reloaded after "Nulstil demo-data".
    • WeeklyScheduleTable.test.tsx: shifts per weekday and assigned names, 14/4/10 counts, weekend label, "Kun ubesatte" / "Kun tildelte" filters, week staying visible on refresh, error and retry.
  • CI: the frontend job now runs npm test between lint and build.
  • README: frontend tests, VITE_API_BASE_URL, frontend check commands.

Test results

npm test        → 2 files, 8 tests passed
npm run lint    → 0 problems
npm run build   → OK
dotnet test     → 59 passed (backend unchanged)

I checked that the tests are meaningful: run against the main versions of App.tsx and WeeklyScheduleTable.tsx, 4 of 8 fail (coverage translation, refresh flicker in two places, missing preference reload).

Limitations

  • SimulationPanel and the unassigned-shift and employee detail panels are only covered indirectly. Simulation and the API layer get more coverage in the backend integration test PR.
  • The underlying culture dependency is in the backend (ScheduleOverviewService formats with the current culture). Changing the message format would affect API consumers, so I left it as is and made the frontend robust instead.

🤖 Generated with Claude Code

kris7011 and others added 2 commits October 16, 2026 13:40
- Replace nine hard-coded API_BASE_URL constants with api/config.ts,
  configurable through VITE_API_BASE_URL (default unchanged)
- translateRiskMessage removed the first '.' in 'Schedule coverage is
  78.57%.', showing '7857%' whenever the API runs with an English or
  invariant culture (e.g. in the Docker image). Only strip the
  sentence-ending period.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests use fixtures captured from the real API after a demo reset and
cover the overview, error/retry, refresh behaviour after generation,
preference reload after reset and the weekly schedule table. CI now
runs npm test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kris7011
kris7011 changed the base branch from ci/add-frontend-validation to main October 16, 2026 13:30
@kris7011
kris7011 merged commit 40db24a into main Oct 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant