Skip to content

Commit c0b2c63

Browse files
authored
Merge pull request #432 from Codeinwp/fix/multiple-checked
fix: multiple default checked values on Checkbox
2 parents 52f690f + 57f7636 commit c0b2c63

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

js/admin/ppom-admin.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,16 @@ jQuery(function($) {
134134
const formData = new FormData();
135135
const ppomFields = new URLSearchParams();
136136

137-
// NOTE: since the request is to big for small values of `max_input_vars`, we will send the PPOM fields as a single string.
138-
(new FormData(this)).forEach(( value, key) => {
139-
if ( key.startsWith('ppom[') && typeof value === 'string' ) {
140-
ppomFields.append( key, value );
137+
/*
138+
NOTE: since the request is to big for small values of `max_input_vars`, we will send the PPOM fields as a single string.
139+
140+
INFO: some parts of the code use `\r\n` as delimiter for arrays in textarea. `serializeArray` respect this convention while native JS Form value access sanitize it to just `\n`.
141+
*/
142+
$( this ).serializeArray().forEach(({ value, name }) => {
143+
if ( name.startsWith('ppom[') && typeof value === 'string' ) {
144+
ppomFields.append( name, value );
141145
} else {
142-
formData.append(key, value);
146+
formData.append(name, value);
143147
}
144148
});
145149

tests/e2e/specs/checkbox.spec.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { test, expect } from "@wordpress/e2e-test-utils-playwright";
5+
6+
import {
7+
addNewField,
8+
addNewOptionInModal,
9+
fillFieldNameAndId,
10+
fillOptionNameAndValue,
11+
pickFieldTypeInModal,
12+
saveFieldInModal,
13+
saveFields,
14+
} from "../utils";
15+
16+
const CHECKBOX_OPTIONS = {
17+
CHECKED_OPTION_1: 'yes',
18+
CHECKED_OPTION_2: 'no',
19+
UNCHECKED_OPTION_1: 'maybe'
20+
};
21+
22+
test.describe("Checkbox", () => {
23+
/***
24+
* Create a simple checkbox with 3 options. Two will be marked as checked by default. Check if the selection is respected on rendering.
25+
*/
26+
test("check default selected options", async ({ page, admin }) => {
27+
await admin.visitAdminPage("admin.php?page=ppom");
28+
29+
await page.getByRole("link", { name: "Add New Group" }).click();
30+
await page
31+
.getByRole("textbox")
32+
.fill("Default Value for Checkbox");
33+
34+
await addNewField(page);
35+
await pickFieldTypeInModal(page, "checkbox");
36+
37+
const modelId = 1;
38+
const fieldId = `checkbox_test`;
39+
40+
await fillFieldNameAndId(
41+
page,
42+
modelId,
43+
`Checkbox Default values`,
44+
fieldId,
45+
);
46+
47+
48+
await page.locator(`textarea[name="ppom\\[${modelId}\\]\\[checked\\]"]`)
49+
.fill(`${CHECKBOX_OPTIONS.CHECKED_OPTION_1}\r\n${CHECKBOX_OPTIONS.CHECKED_OPTION_2}`);
50+
51+
await page
52+
.locator(`#ppom_field_model_${modelId}`)
53+
.getByText("Add Options", { exact: true })
54+
.click();
55+
56+
await fillOptionNameAndValue(page, modelId, 0, CHECKBOX_OPTIONS.CHECKED_OPTION_1, CHECKBOX_OPTIONS.CHECKED_OPTION_1);
57+
await addNewOptionInModal(page, modelId);
58+
await fillOptionNameAndValue(page, modelId, 1, CHECKBOX_OPTIONS.CHECKED_OPTION_2, CHECKBOX_OPTIONS.CHECKED_OPTION_2);
59+
await addNewOptionInModal(page, modelId);
60+
await fillOptionNameAndValue(page, modelId, 2, CHECKBOX_OPTIONS.UNCHECKED_OPTION_1, CHECKBOX_OPTIONS.UNCHECKED_OPTION_1);
61+
await saveFieldInModal(page, modelId);
62+
63+
await saveFields(page);
64+
await page.waitForLoadState("networkidle");
65+
await page.reload();
66+
67+
await page.getByText("Attach to Products").click({ force: true });
68+
await page.waitForLoadState("networkidle");
69+
70+
const productSelector = page.locator(
71+
'select[name="ppom-attach-to-products\\[\\]"]',
72+
);
73+
await page.waitForLoadState("networkidle");
74+
75+
await productSelector.selectOption({ index: 0 });
76+
const selectedProductId = await productSelector.inputValue();
77+
await page.getByRole("button", { name: "Save", exact: true }).click();
78+
79+
await page.waitForLoadState("networkidle");
80+
await page.goto(`/?p=${selectedProductId}`);
81+
82+
await expect(page.locator(`input[name="ppom[fields][${fieldId}][]"][value="${CHECKBOX_OPTIONS.CHECKED_OPTION_1}"]`)).toBeChecked();
83+
await expect(page.locator(`input[name="ppom[fields][${fieldId}][]"][value="${CHECKBOX_OPTIONS.CHECKED_OPTION_2}"]`)).toBeChecked();
84+
await expect(page.locator(`input[name="ppom[fields][${fieldId}][]"][value="${CHECKBOX_OPTIONS.UNCHECKED_OPTION_1}"]`)).not.toBeChecked();
85+
});
86+
});

0 commit comments

Comments
 (0)