|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import React, { act } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 5 | + |
| 6 | +const { get, post } = vi.hoisted(() => ({ |
| 7 | + get: vi.fn(async () => [ |
| 8 | + { id: "H1", formId: "F1", title: "About you", fieldType: "Heading", required: true }, |
| 9 | + { id: "Q1", formId: "F1", title: "Name", fieldType: "Textbox" } |
| 10 | + ]), |
| 11 | + post: vi.fn(async (_url: string, data: any) => data) |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("@churchapps/helpers", async (importOriginal) => { |
| 15 | + const actual: any = await importOriginal(); |
| 16 | + return { ...actual, ApiHelper: { ...actual.ApiHelper, get, post } }; |
| 17 | +}); |
| 18 | + |
| 19 | +import { QuestionEdit } from "../QuestionEdit"; |
| 20 | +import { FormSubmissionEdit } from "../FormSubmissionEdit"; |
| 21 | + |
| 22 | +(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true; |
| 23 | + |
| 24 | +describe("Question descriptions", () => { |
| 25 | + it("render as Markdown", () => { |
| 26 | + const html = renderToStaticMarkup( |
| 27 | + <QuestionEdit |
| 28 | + question={{ id: "q1", title: "Name", fieldType: "Textbox", description: "**Bold** and [a link](https://example.com)" }} |
| 29 | + answer={{ questionId: "q1", value: "" }} |
| 30 | + changeFunction={() => { }} |
| 31 | + /> |
| 32 | + ); |
| 33 | + expect(html).toContain("<strong>Bold</strong>"); |
| 34 | + expect(html).toContain("href=\"https://example.com\""); |
| 35 | + }); |
| 36 | + |
| 37 | + it("strip unsafe links", () => { |
| 38 | + const html = renderToStaticMarkup( |
| 39 | + <QuestionEdit question={{ id: "q1", title: "Name", fieldType: "Textbox", description: "[x](javascript:alert(1))" }} answer={{ questionId: "q1", value: "" }} changeFunction={() => { }} /> |
| 40 | + ); |
| 41 | + expect(html).not.toContain("javascript:"); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("Heading questions", () => { |
| 46 | + it("store no answer and are never required", async () => { |
| 47 | + const container = document.createElement("div"); |
| 48 | + document.body.appendChild(container); |
| 49 | + const root = createRoot(container); |
| 50 | + |
| 51 | + await act(async () => { |
| 52 | + root.render(<FormSubmissionEdit formSubmissionId="" addFormId="F1" contentType="form" contentId="F1" showHeader={false} updatedFunction={() => { }} />); |
| 53 | + }); |
| 54 | + expect(container.textContent).toContain("About you"); |
| 55 | + |
| 56 | + const buttons = Array.from(container.querySelectorAll("button")); |
| 57 | + await act(async () => { buttons[buttons.length - 1].click(); }); |
| 58 | + |
| 59 | + expect(post).toHaveBeenCalledTimes(1); |
| 60 | + const submission = post.mock.calls[0][1][0]; |
| 61 | + expect(submission.answers.map((a: any) => a.questionId)).toEqual(["Q1"]); |
| 62 | + |
| 63 | + act(() => root.unmount()); |
| 64 | + container.remove(); |
| 65 | + }); |
| 66 | +}); |
0 commit comments