Skip to content

Commit c2c2d56

Browse files
stefan-burkeTest
andauthored
Handle missing contact form item tag labels (#1478)
Co-authored-by: Test <test@test.com>
1 parent 6336b4c commit c2c2d56

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/_lib/config/form-helpers.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* @typedef {object} ContactFormData
2929
* @property {ContactFormField[]} fields
30-
* @property {Record<string, string>} itemTagLabels
30+
* @property {Record<string, string>} [itemTagLabels]
3131
*/
3232

3333
/**
@@ -45,8 +45,9 @@
4545
* @returns {ContactFormField[]}
4646
*/
4747
export function resolveFormFields(contactForm, tags, skipShowOn = false) {
48+
const { fields, itemTagLabels = {} } = contactForm;
4849
const tagList = Array.isArray(tags) ? tags : [];
49-
const matchEntry = Object.entries(contactForm.itemTagLabels).find(([tag]) =>
50+
const matchEntry = Object.entries(itemTagLabels).find(([tag]) =>
5051
tagList.includes(tag),
5152
);
5253
const match = matchEntry
@@ -83,7 +84,7 @@ export function resolveFormFields(contactForm, tags, skipShowOn = false) {
8384
return [field];
8485
};
8586

86-
return contactForm.fields.flatMap(resolveField);
87+
return fields.flatMap(resolveField);
8788
}
8889

8990
/**

test/unit/contact-form-fields.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ describe("resolveFormFields", () => {
9393
// showForItemTag field is dropped (no match), plain fields pass through
9494
expect(out.map((f) => f.name)).toEqual(["name", "message"]);
9595
});
96+
97+
test("treats missing item tag labels as an empty mapping", () => {
98+
const contactForm = { fields: baseContactForm.fields };
99+
const out = resolveFormFields(contactForm, ["products"]);
100+
expect(out.map((f) => f.name)).toEqual(["name", "message"]);
101+
});
96102
});

0 commit comments

Comments
 (0)