-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add Heading 1/2/3 field types #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5e0ea1e
feat(form-field): add Heading 1/2/3 fieldtypes to doctype and backend…
harshtandiya ef02dd9
feat(heading): wire up Heading 1/2/3 field types across the frontend
harshtandiya 1d663d7
test(heading): add backend and E2E tests for heading field types
harshtandiya 83212f2
fix(form-field): escape HTML in heading labels for get_options method
harshtandiya d4cc1d1
chore: minor styling
harshtandiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Copyright (c) 2025, harsh@buildwithhussain.com and contributors | ||
| # For license information, please see license.txt | ||
|
|
||
| import frappe | ||
| from frappe.tests import IntegrationTestCase | ||
|
|
||
| from forms_pro.forms_pro.doctype.form_field.form_field import FORM_TO_FRAPPE_FIELDTYPE | ||
|
|
||
|
|
||
| def _build_field( | ||
| fieldtype: str, label: str = "Test Field", fieldname: str = "test_field", options: str | None = None | ||
| ): | ||
| """Build an in-memory FormField document without inserting.""" | ||
| doc = frappe.get_doc( | ||
| { | ||
| "doctype": "Form Field", | ||
| "fieldtype": fieldtype, | ||
| "label": label, | ||
| "fieldname": fieldname, | ||
| "options": options, | ||
| } | ||
| ) | ||
| return doc | ||
|
|
||
|
|
||
| class TestFormFieldGetOptions(IntegrationTestCase): | ||
| def test_heading_1_returns_h1_tag_wrapping_label(self): | ||
| field = _build_field("Heading 1", label="Introduction") | ||
| self.assertEqual(field.get_options(), "<h1>Introduction</h1>") | ||
|
|
||
| def test_heading_2_returns_h2_tag_wrapping_label(self): | ||
| field = _build_field("Heading 2", label="Section A") | ||
| self.assertEqual(field.get_options(), "<h2>Section A</h2>") | ||
|
|
||
| def test_heading_3_returns_h3_tag_wrapping_label(self): | ||
| field = _build_field("Heading 3", label="Subsection") | ||
| self.assertEqual(field.get_options(), "<h3>Subsection</h3>") | ||
|
|
||
| def test_non_heading_returns_options_unchanged(self): | ||
| field = _build_field("Select", label="Color", options="Red\nBlue\nGreen") | ||
| self.assertEqual(field.get_options(), "Red\nBlue\nGreen") | ||
|
|
||
| def test_non_heading_with_no_options_returns_none(self): | ||
| field = _build_field("Data", label="Name") | ||
| self.assertIsNone(field.get_options()) | ||
|
|
||
|
|
||
| class TestFormFieldToFrappeField(IntegrationTestCase): | ||
| def test_heading_1_maps_to_html_fieldtype(self): | ||
| field = _build_field("Heading 1", label="My Heading", fieldname="my_heading") | ||
| result = field.to_frappe_field | ||
| self.assertEqual(result["fieldtype"], "HTML") | ||
|
|
||
| def test_heading_2_maps_to_html_fieldtype(self): | ||
| field = _build_field("Heading 2", label="Sub Heading", fieldname="sub_heading") | ||
| self.assertEqual(field.to_frappe_field["fieldtype"], "HTML") | ||
|
|
||
| def test_heading_3_maps_to_html_fieldtype(self): | ||
| field = _build_field("Heading 3", label="Minor Heading", fieldname="minor_heading") | ||
| self.assertEqual(field.to_frappe_field["fieldtype"], "HTML") | ||
|
|
||
| def test_heading_options_contains_html_tag(self): | ||
| field = _build_field("Heading 1", label="My Title", fieldname="my_title") | ||
| result = field.to_frappe_field | ||
| self.assertIn("<h1>", result["options"]) | ||
| self.assertIn("My Title", result["options"]) | ||
|
|
||
| def test_data_field_maps_to_data_frappe_fieldtype(self): | ||
| field = _build_field("Data", label="Name", fieldname="name_field") | ||
| self.assertEqual(field.to_frappe_field["fieldtype"], "Data") | ||
|
|
||
| def test_form_to_frappe_fieldtype_has_all_heading_levels(self): | ||
| for fieldtype in ("Heading 1", "Heading 2", "Heading 3"): | ||
| with self.subTest(fieldtype=fieldtype): | ||
| self.assertIn(fieldtype, FORM_TO_FRAPPE_FIELDTYPE) | ||
| self.assertEqual(FORM_TO_FRAPPE_FIELDTYPE[fieldtype]["fieldtype"], "HTML") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { test, expect } from "../fixtures/test-data.fixture"; | ||
| import { FormBuilderPage } from "../helpers/form-builder"; | ||
| import { SubmissionPage } from "../helpers/submission"; | ||
|
|
||
| test.describe("Heading fields", () => { | ||
| test("all three heading types appear in the Add Fields sidebar", async ({ | ||
| page, | ||
| createForm, | ||
| }) => { | ||
| const formId = await createForm(); | ||
| const builder = new FormBuilderPage(page); | ||
| await builder.goto(formId); | ||
|
|
||
| const sidebar = page.locator( | ||
| '[data-form-builder-component="form-builder-sidebar"]' | ||
| ); | ||
| await expect(sidebar.getByText("Heading 1", { exact: true })).toBeVisible(); | ||
| await expect(sidebar.getByText("Heading 2", { exact: true })).toBeVisible(); | ||
| await expect(sidebar.getByText("Heading 3", { exact: true })).toBeVisible(); | ||
| }); | ||
|
|
||
| test("adding Heading 1 shows editable input in builder (edit mode)", async ({ | ||
| page, | ||
| createForm, | ||
| }) => { | ||
| const formId = await createForm(); | ||
| const builder = new FormBuilderPage(page); | ||
| await builder.goto(formId); | ||
|
|
||
| await builder.addField("Heading 1"); | ||
|
|
||
| // In edit mode (field selected), shows input with placeholder | ||
| const headingInput = page.getByPlaceholder("Heading Text").first(); | ||
| await expect(headingInput).toBeVisible({ timeout: 5000 }); | ||
| }); | ||
|
|
||
| test("heading label is editable in builder", async ({ page, createForm }) => { | ||
| const formId = await createForm(); | ||
| const builder = new FormBuilderPage(page); | ||
| await builder.goto(formId); | ||
|
|
||
| await builder.addField("Heading 1"); | ||
|
|
||
| // Edit mode shows a text input for the heading label | ||
| const headingInput = page.getByPlaceholder("Heading Text").first(); | ||
| await expect(headingInput).toBeVisible({ timeout: 5000 }); | ||
| await headingInput.fill("About You"); | ||
|
|
||
| // Verify the input has the value we typed | ||
| await expect(headingInput).toHaveValue("About You"); | ||
| }); | ||
|
|
||
| test("heading renders on the public submission page without an input", async ({ | ||
| page, | ||
| browser, | ||
| createForm, | ||
| apiContext, | ||
| }) => { | ||
| const formId = await createForm(); | ||
| const builder = new FormBuilderPage(page); | ||
| await builder.goto(formId); | ||
|
|
||
| await builder.addField("Heading 2"); | ||
| const headingInput = page.getByPlaceholder("Heading Text").first(); | ||
| await expect(headingInput).toBeVisible({ timeout: 5000 }); | ||
| await headingInput.fill("Your Details"); | ||
|
|
||
| await builder.publish(); | ||
|
|
||
| const res = await apiContext.get(`/api/resource/Form/${formId}`); | ||
| const { data } = await res.json(); | ||
| const route: string = data.route; | ||
|
|
||
| const guestCtx = await browser.newContext(); | ||
| const guestPage = await guestCtx.newPage(); | ||
| const submissionPage = new SubmissionPage(guestPage); | ||
| await submissionPage.goto(route); | ||
|
|
||
| // Heading renders as text, not as an input | ||
| await expect( | ||
| guestPage.locator("h3", { hasText: "Your Details" }) | ||
| ).toBeVisible({ | ||
| timeout: 10000, | ||
| }); | ||
| // h3 because Heading 2 → h3 tag in Heading.vue | ||
|
|
||
| await guestCtx.close(); | ||
| }); | ||
|
|
||
| test("form with heading fields submits successfully", async ({ | ||
| browser, | ||
| createPublishedForm, | ||
| apiContext, | ||
| }) => { | ||
| const { formId, route } = await createPublishedForm(); | ||
|
|
||
| // Add a Heading 1 field to the published form via REST | ||
| const formRes = await apiContext.get(`/api/resource/Form/${formId}`); | ||
| const { data: formData } = await formRes.json(); | ||
|
|
||
| await apiContext.put(`/api/resource/Form/${formId}`, { | ||
| data: { | ||
| fields: [ | ||
| ...(formData.fields ?? []), | ||
| { | ||
| fieldtype: "Heading 1", | ||
| label: "Section Header", | ||
| fieldname: "section_header", | ||
| reqd: 0, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| // Guest submits — heading must not cause a server-side validation error | ||
| const guestCtx = await browser.newContext(); | ||
| const guestPage = await guestCtx.newPage(); | ||
| const submissionPage = new SubmissionPage(guestPage); | ||
| await submissionPage.goto(route); | ||
|
|
||
| await expect(guestPage.getByRole("button", { name: "Submit" })).toBeVisible( | ||
| { | ||
| timeout: 10000, | ||
| } | ||
| ); | ||
| await submissionPage.submit(); | ||
|
|
||
| await expect(submissionPage.successMessage()).toBeVisible({ | ||
| timeout: 10000, | ||
| }); | ||
|
|
||
| await guestCtx.close(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 453
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 189
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 441
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 147
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 241
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 420
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 2161
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 3839
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 259
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 3163
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 601
🏁 Script executed:
Repository: BuildWithHussain/forms_pro
Length of output: 965
get_options()has incorrect return type annotation and test assertion.The method is declared as
def get_options(self) -> str:(neverNone), but the test assertsself.assertIsNone(field.get_options()). Whenfrappe.get_doc()constructs the doc with"options": None(or omitted), Frappe coerces theSmall Textfield to""(empty string), notNone. This means:field.optionswill be"", notNoneget_options()returnsself.options, so it returns""assertIsNone("")fails-> strcontradicts the test's expectation ofNoneFix: Either change the return type to
str | Noneand handle both cases, or change the test toassertFalsy(field.get_options())/assertEqual(field.get_options(), "").🤖 Prompt for AI Agents