diff --git a/src/tests/EncounterRename.test.js b/src/tests/EncounterRename.test.js new file mode 100644 index 0000000..2478c32 --- /dev/null +++ b/src/tests/EncounterRename.test.js @@ -0,0 +1,78 @@ +// Encounter rename: pencil button on card opens inline input, saves via updateDoc. +// NOTE: background snapshot rerenders can replace the input/form DOM nodes mid-test, +// so every interaction re-queries the live node (fresh()). + +import { screen, fireEvent, act, waitFor } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, MOCK_DB } from './testHelpers'; + +const encPath = (cid, eid) => `artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/${cid}/encounters/${eid}`; + +const fresh = () => screen.getByLabelText('Encounter name'); +const freshQuery = () => screen.queryByLabelText('Encounter name'); + +async function openRename() { + await screen.findByTitle('Rename Encounter'); + // settle async rerenders (fetchDetails etc.) so click hits live node + await act(async () => { await new Promise(r => setTimeout(r, 250)); }); + fireEvent.click(screen.getByTitle('Rename Encounter')); + await waitFor(() => expect(fresh()).toBeInTheDocument()); +} + +describe('encounter rename', () => { + test('pencil opens inline edit prefilled; Enter saves to Firestore doc', async () => { + await renderApp(); + const cid = await createCampaignViaUI('RenameCamp'); + await selectCampaignByName('RenameCamp'); + const eid = await createEncounterViaUI('Old Name'); + + await openRename(); + expect(await screen.findByDisplayValue('Old Name')).toBeInTheDocument(); + + fireEvent.change(fresh(), { target: { value: 'New Name' } }); + await waitFor(() => expect(fresh().value).toBe('New Name')); + // eslint-disable-next-line testing-library/no-node-access + fireEvent.submit(fresh().closest('form')); + + await screen.findByText('New Name'); + expect(MOCK_DB.get(encPath(cid, eid)).name).toBe('New Name'); + expect(freshQuery()).not.toBeInTheDocument(); + }); + + test('Escape cancels without changing doc', async () => { + await renderApp(); + const cid = await createCampaignViaUI('RenameCamp2'); + await selectCampaignByName('RenameCamp2'); + const eid = await createEncounterViaUI('Keep Me'); + + await openRename(); + expect(await screen.findByDisplayValue('Keep Me')).toBeInTheDocument(); + + fireEvent.change(fresh(), { target: { value: 'Discard' } }); + await waitFor(() => expect(fresh().value).toBe('Discard')); + fireEvent.keyDown(fresh(), { key: 'Escape', code: 'Escape' }); + + await screen.findByText('Keep Me'); + expect(MOCK_DB.get(encPath(cid, eid)).name).toBe('Keep Me'); + expect(screen.queryByDisplayValue('Discard')).not.toBeInTheDocument(); + }); + + test('empty name ignored on save', async () => { + await renderApp(); + const cid = await createCampaignViaUI('RenameCamp3'); + await selectCampaignByName('RenameCamp3'); + const eid = await createEncounterViaUI('Real Name'); + + await openRename(); + expect(await screen.findByDisplayValue('Real Name')).toBeInTheDocument(); + + fireEvent.change(fresh(), { target: { value: ' ' } }); + await waitFor(() => expect(fresh().value).toBe(' ')); + // eslint-disable-next-line testing-library/no-node-access + fireEvent.submit(fresh().closest('form')); + + await screen.findByText('Real Name'); + expect(MOCK_DB.get(encPath(cid, eid)).name).toBe('Real Name'); + expect(freshQuery()).not.toBeInTheDocument(); + }); +});