feat(FEAT-3): initiative first-class field (add + inline edit)
Add form: optional initiative field (monster + character). Empty = roll d20+mod (current behavior). Filled = use value, skip roll. 'blank=roll' hint + 'auto' placeholder for clarity. Inline edit: ALL participants. Number input in participant row. Blur or Enter commits. Capped 2 digits (max 99). Auto-select on focus for quick overwrite. Styled to match other fields (border-stone-700, rounded-md, shadow-sm, w-10). handleAddParticipant: manualInit detects set value. lastRollDetails adapts display (manual flag shows 'Set initiative' vs 'Rolled d20'). Campaign card date: Clock icon + 'Created:' prefix, own row, muted stone-300 opacity-70. Was crammed inline with character/encounter counts. Tests: InitiativeField.test.js (2 tests - set value + empty=roll). RED first (field missing), then green after impl. App + Participant characterization still green (18 total). Note: inline init edit does NOT re-sort. Known followup — displays + list order must reflect changed initiative. Tracked separately.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
// RED test: FEAT-3 initiative field on add participant.
|
||||
// If initiative field set, use it (no roll). Empty = roll d20+mod (current).
|
||||
import React from 'react';
|
||||
import { screen, waitFor, within } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom';
|
||||
import { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, getParticipantForm } from './testHelpers';
|
||||
import { fireEvent } from '@testing-library/react';
|
||||
import { getCalls } from '../__mocks__/firebase/_mock-db';
|
||||
|
||||
function lastParticipantUpdate(name) {
|
||||
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.find(p => p.name === name);
|
||||
}
|
||||
|
||||
describe('FEAT-3: initiative field on add (optional, empty=roll)', () => {
|
||||
test('initiative field set → uses value, no roll', async () => {
|
||||
await renderApp();
|
||||
await createCampaignViaUI('Camp');
|
||||
await selectCampaignByName('Camp');
|
||||
await createEncounterViaUI('Enc');
|
||||
await selectEncounterByName('Enc');
|
||||
|
||||
const form = within(getParticipantForm());
|
||||
fireEvent.change(form.getByPlaceholderText('e.g., Dire Wolf'), { target: { value: 'Goblin' } });
|
||||
fireEvent.change(form.getByLabelText(/Init Mod/i), { target: { value: '2' } });
|
||||
fireEvent.change(form.getByLabelText(/Max HP/i), { target: { value: '7' } });
|
||||
// set explicit initiative
|
||||
fireEvent.change(form.getByPlaceholderText('auto'), { target: { value: '15' } });
|
||||
fireEvent.click(form.getByRole('button', { name: /Add to Encounter/i }));
|
||||
|
||||
await waitFor(() => lastParticipantUpdate('Goblin'));
|
||||
const p = lastParticipantUpdate('Goblin');
|
||||
expect(p.initiative).toBe(15);
|
||||
});
|
||||
|
||||
test('initiative field empty → rolls d20+mod', async () => {
|
||||
await renderApp();
|
||||
await createCampaignViaUI('Camp2');
|
||||
await selectCampaignByName('Camp2');
|
||||
await createEncounterViaUI('Enc2');
|
||||
await selectEncounterByName('Enc2');
|
||||
|
||||
const form = within(getParticipantForm());
|
||||
fireEvent.change(form.getByPlaceholderText('e.g., Dire Wolf'), { target: { value: 'Wolf' } });
|
||||
fireEvent.change(form.getByLabelText(/Init Mod/i), { target: { value: '3' } });
|
||||
fireEvent.change(form.getByLabelText(/Max HP/i), { target: { value: '11' } });
|
||||
// leave initiative empty
|
||||
fireEvent.click(form.getByRole('button', { name: /Add to Encounter/i }));
|
||||
|
||||
await waitFor(() => lastParticipantUpdate('Wolf'));
|
||||
const p = lastParticipantUpdate('Wolf');
|
||||
// rolled d20 (1-20) + mod 3 = range 4-23
|
||||
expect(p.initiative).toBeGreaterThanOrEqual(4);
|
||||
expect(p.initiative).toBeLessThanOrEqual(23);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user