test: participant characterization (9 tests)

- Participant.characterization.test.js: addMonster (shape, initiative range, NPC), deleteParticipant, toggleActive, applyDamage, damage-to-0, heal revive, toggleCondition
- testHelpers.js: getParticipantForm (scoped), addMonsterViaUI, setupReady, startCombatViaUI

Locks participant write paths + payload shapes. Refactor guard.
This commit is contained in:
david raistrick
2026-06-28 18:50:42 -04:00
parent 0c1196aee1
commit 4158a1634d
2 changed files with 175 additions and 1 deletions
+49 -1
View File
@@ -1,9 +1,22 @@
// test helpers: drive App UI to states. Used across characterization suites.
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { render, screen, fireEvent, waitFor, within } from '@testing-library/react';
import App from './App';
import { MOCK_DB } from './__mocks__/firebase/_mock-db';
// Scoped container: the "Add Participants" section (avoids label clashes with CharacterManager).
export function getParticipantForm() {
const heading = screen.getByText('Add Participants');
// closest section/div wrapping the form
let node = heading;
for (let i = 0; i < 6; i++) {
node = node.parentElement;
if (!node) break;
if (node.querySelector('form')) return node;
}
return heading.parentElement;
}
// Render app, wait for auth + campaign list.
export async function renderApp() {
window.history.replaceState({}, '', '/');
@@ -53,4 +66,39 @@ export async function selectEncounterByName(name) {
await waitFor(() => screen.getByText(/Managing Encounter:/i));
}
// Add a monster participant via the ParticipantManager form. Assumes encounter selected.
export async function addMonsterViaUI(name = 'Goblin', maxHp = 7, initMod = 2) {
const form = within(getParticipantForm());
fireEvent.change(form.getByPlaceholderText('e.g., Dire Wolf'), { target: { value: name } });
fireEvent.change(form.getByLabelText(/Init Mod/i), { target: { value: String(initMod) } });
fireEvent.change(form.getByLabelText(/Max HP/i), { target: { value: String(maxHp) } });
fireEvent.click(form.getByRole('button', { name: /Add to Encounter/i }));
const { getCalls } = require('./__mocks__/firebase/_mock-db');
await waitFor(() => {
const calls = getCalls().filter(c => c.fn === 'updateDoc' && c.path.includes('/encounters/'));
const last = calls[calls.length - 1];
return last && last.data.participants && last.data.participants.some(p => p.name === name);
});
}
// Full setup: app -> campaign -> encounter selected.
export async function setupReady(campName = 'Camp', encName = 'Enc') {
await renderApp();
await createCampaignViaUI(campName);
await selectCampaignByName(campName);
await createEncounterViaUI(encName);
await selectEncounterByName(encName);
}
// Start combat. Assumes encounter selected with active participants.
export async function startCombatViaUI() {
fireEvent.click(screen.getByRole('button', { name: /Start Combat/i }));
const { getCalls } = require('./__mocks__/firebase/_mock-db');
await waitFor(() => {
const calls = getCalls().filter(c => c.fn === 'updateDoc' && c.path.includes('/encounters/'));
const last = calls[calls.length - 1];
return last && last.data.isStarted === true;
});
}
export { MOCK_DB };