|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A public form's `thank-you` panel fills its `{{record.field_name}}` tokens |
| 5 | + * from the submit answer's `fields` (`PublicFormSubmitResponseSchema`): a |
| 6 | + * web-to-case visitor sees their case number, never the token's own text. A |
| 7 | + * name the answer carries no value for fills as empty, and a value is drawn as |
| 8 | + * text, never as markup. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 12 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 13 | +import userEvent from '@testing-library/user-event'; |
| 14 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 15 | +import { createI18n } from '@object-ui/i18n'; |
| 16 | +import { FormPage, fillThankYouPanel, readThankYouAnswerFields } from './FormPage'; |
| 17 | + |
| 18 | +vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 19 | + |
| 20 | +/** The public `/forms/:slug` resolver payload, with a `thank-you` panel. */ |
| 21 | +function publicPayload(panel: { title?: string; message?: string }) { |
| 22 | + return { |
| 23 | + slug: 'report-issue', |
| 24 | + object: 'support_case', |
| 25 | + label: 'Report an issue', |
| 26 | + form: { |
| 27 | + type: 'simple', |
| 28 | + sections: [{ fields: ['subject'] }], |
| 29 | + submitBehavior: { kind: 'thank-you', ...panel }, |
| 30 | + }, |
| 31 | + objectSchema: { |
| 32 | + name: 'support_case', |
| 33 | + fields: { |
| 34 | + subject: { type: 'text', label: 'Subject' }, |
| 35 | + case_number: { type: 'autonumber', label: 'Case number' }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function stubFetch(payload: unknown, answer: unknown) { |
| 42 | + return vi.fn(async (url: string) => { |
| 43 | + const body = String(url).includes('/forms/report-issue/submit') ? answer : payload; |
| 44 | + return { |
| 45 | + ok: true, |
| 46 | + status: 200, |
| 47 | + statusText: 'OK', |
| 48 | + json: async () => body, |
| 49 | + text: async () => JSON.stringify(body), |
| 50 | + } as unknown as Response; |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +async function submitPublic(panel: { title?: string; message?: string }, answer: unknown) { |
| 55 | + vi.stubGlobal('fetch', stubFetch(publicPayload(panel), answer)); |
| 56 | + const view = render( |
| 57 | + <MemoryRouter initialEntries={['/f/report-issue']}> |
| 58 | + <Routes> |
| 59 | + <Route path="/f/:slug" element={<FormPage mode="public" />} /> |
| 60 | + </Routes> |
| 61 | + </MemoryRouter>, |
| 62 | + ); |
| 63 | + await screen.findByLabelText(/Subject/); |
| 64 | + await userEvent.type(screen.getByLabelText(/Subject/), 'Printer jams'); |
| 65 | + await userEvent.click(screen.getByRole('button', { name: /Submit/ })); |
| 66 | + return view; |
| 67 | +} |
| 68 | + |
| 69 | +beforeEach(() => { |
| 70 | + createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false }); |
| 71 | +}); |
| 72 | + |
| 73 | +afterEach(() => { |
| 74 | + vi.unstubAllGlobals(); |
| 75 | +}); |
| 76 | + |
| 77 | +describe('public thank-you panel — record field tokens', () => { |
| 78 | + it('shows the answer\'s value for a `{{record.case_number}}` token in the message and the title', async () => { |
| 79 | + await submitPublic( |
| 80 | + { title: 'Case {{record.case_number}} opened', message: 'Your case number is {{record.case_number}}.' }, |
| 81 | + { id: 'case-1', fields: { case_number: 'CASE-0042' } }, |
| 82 | + ); |
| 83 | + |
| 84 | + expect(await screen.findByText('Your case number is CASE-0042.')).toBeInTheDocument(); |
| 85 | + expect(screen.getByRole('heading', { name: 'Case CASE-0042 opened' })).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('fills a token the answer carries no value for as empty, never as the token text', async () => { |
| 89 | + const { container } = await submitPublic( |
| 90 | + { message: 'Your case number is {{record.case_number}}.' }, |
| 91 | + { id: 'case-1' }, |
| 92 | + ); |
| 93 | + |
| 94 | + expect(await screen.findByText('Your case number is .')).toBeInTheDocument(); |
| 95 | + expect(container.textContent).not.toContain('{{'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('draws a value as text, never as markup', async () => { |
| 99 | + const value = '<img src="x" alt="injected"><b>bold</b>'; |
| 100 | + const { container } = await submitPublic( |
| 101 | + { message: 'Case: {{record.case_number}}' }, |
| 102 | + { id: 'case-1', fields: { case_number: value } }, |
| 103 | + ); |
| 104 | + |
| 105 | + expect(await screen.findByText(`Case: ${value}`)).toBeInTheDocument(); |
| 106 | + expect(container.querySelector('img')).toBeNull(); |
| 107 | + expect(container.querySelector('b')).toBeNull(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('reads the answer through the spec\'s shape: an answer with keys beyond `id` and `fields` fills nothing', async () => { |
| 111 | + await submitPublic( |
| 112 | + { message: 'Your case number is {{record.case_number}}.' }, |
| 113 | + { id: 'case-1', fields: { case_number: 'CASE-0042' }, record: { case_number: 'CASE-0042' } }, |
| 114 | + ); |
| 115 | + |
| 116 | + await waitFor(() => expect(screen.getByText('Your case number is .')).toBeInTheDocument()); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('fillThankYouPanel', () => { |
| 121 | + it('never reads a filled value for tokens again', () => { |
| 122 | + const filled = fillThankYouPanel( |
| 123 | + { message: '{{record.case_number}} / {{record.ref_code}}' }, |
| 124 | + { case_number: '{{record.ref_code}}', ref_code: 'R-7' }, |
| 125 | + ); |
| 126 | + expect(filled.message).toBe('{{record.ref_code}} / R-7'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('fills a name that is an Object.prototype key as empty', () => { |
| 130 | + expect(fillThankYouPanel({ message: 'x{{record.constructor}}y' }, {}).message).toBe('xy'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('leaves a panel with no token as authored', () => { |
| 134 | + const panel = { title: 'Thanks', message: 'We got it.' }; |
| 135 | + expect(fillThankYouPanel(panel, { case_number: 'CASE-1' })).toEqual(panel); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe('readThankYouAnswerFields', () => { |
| 140 | + it('reads `fields` off the public submit answer', () => { |
| 141 | + expect(readThankYouAnswerFields({ id: 'a', fields: { case_number: 7 } })).toEqual({ case_number: 7 }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('reads none off an answer that is not the public submit shape', () => { |
| 145 | + expect(readThankYouAnswerFields({ object: 'x', id: 'a', record: { case_number: 7 } })).toBeUndefined(); |
| 146 | + expect(readThankYouAnswerFields(undefined)).toBeUndefined(); |
| 147 | + }); |
| 148 | +}); |
0 commit comments