|
| 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