|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2025 Adobe |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + ******************************************************************************/ |
| 16 | +describe("Form Runtime with Date Picker - Min/Max Constraints", () => { |
| 17 | + |
| 18 | + const pagePath = "content/forms/af/core-components-it/samples/datepicker/basic.html" |
| 19 | + const bemBlock = 'cmp-adaptiveform-datepicker' |
| 20 | + |
| 21 | + let formContainer = null |
| 22 | + const fmPropertiesUI = "/libs/fd/fm/gui/content/forms/formmetadataeditor.html/content/dam/formsanddocuments/core-components-it/samples/datepicker/basic" |
| 23 | + const themeRef = 'input[name="./jcr:content/metadata/themeRef"]' |
| 24 | + const propertiesSaveBtn = '#shell-propertiespage-doneactivator' |
| 25 | + |
| 26 | + // enabling theme for this test case as without theme there is a bug in custom widget css |
| 27 | + before(() => { |
| 28 | + cy.openPage(fmPropertiesUI).then(() => { |
| 29 | + cy.get(themeRef).invoke('val', '').type('/libs/fd/af/themes/canvas', {force: true}).then(() => { |
| 30 | + cy.get(propertiesSaveBtn).click(); |
| 31 | + }) |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + cy.previewForm(pagePath).then(p => { |
| 37 | + formContainer = p; |
| 38 | + }) |
| 39 | + }); |
| 40 | + |
| 41 | + it("should have min and max attributes correctly set on input element", () => { |
| 42 | + const [dateInput12, dateInput12FieldView] = Object.entries(formContainer._fields)[12]; |
| 43 | + |
| 44 | + // Verify min attribute is set correctly (should be formatted as yyyy-MM-dd) |
| 45 | + cy.get(`#${dateInput12}`).find("input").should('have.attr', 'min', '2025-12-02'); |
| 46 | + |
| 47 | + // Verify max attribute is set correctly (should be formatted as yyyy-MM-dd) |
| 48 | + cy.get(`#${dateInput12}`).find("input").should('have.attr', 'max', '2025-12-16'); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should enforce min constraint when date is below minimum", () => { |
| 52 | + const [dateInput12, dateInput12FieldView] = Object.entries(formContainer._fields)[12]; |
| 53 | + const dateBelowMin = "2025-07-09"; // One day before minimum |
| 54 | + |
| 55 | + cy.get(`#${dateInput12}`).find("input").clear().type(dateBelowMin).blur().then(() => { |
| 56 | + // The browser should prevent dates below min, but if entered, validation should catch it |
| 57 | + cy.get(`#${dateInput12}`).find("input").invoke('val').then((val) => { |
| 58 | + // If browser enforces min constraint, the value might be empty or the min date |
| 59 | + // If validation catches it, error message should appear |
| 60 | + if (val === dateBelowMin) { |
| 61 | + cy.get(`#${dateInput12}`).find(".cmp-adaptiveform-datepicker__errormessage") |
| 62 | + .should('not.have.text', ''); |
| 63 | + } |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should enforce max constraint when date is above maximum", () => { |
| 69 | + const [dateInput12, dateInput12FieldView] = Object.entries(formContainer._fields)[12]; |
| 70 | + const dateAboveMax = "2025-12-18"; // One day after maximum |
| 71 | + |
| 72 | + cy.get(`#${dateInput12}`).find("input").clear().type(dateAboveMax).blur().then(() => { |
| 73 | + // The browser should prevent dates above max, but if entered, validation should catch it |
| 74 | + cy.get(`#${dateInput12}`).find("input").invoke('val').then((val) => { |
| 75 | + // If browser enforces max constraint, the value might be empty or the max date |
| 76 | + // If validation catches it, error message should appear |
| 77 | + if (val === dateAboveMax) { |
| 78 | + cy.get(`#${dateInput12}`).find(".cmp-adaptiveform-datepicker__errormessage") |
| 79 | + .should('not.have.text', ''); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should accept dates within min and max range", () => { |
| 86 | + const [dateInput12, dateInput12FieldView] = Object.entries(formContainer._fields)[12]; |
| 87 | + const dateInRange = "2025-12-15"; // Within the range |
| 88 | + |
| 89 | + cy.get(`#${dateInput12}`).find("input").clear().type(dateInRange).blur().then(() => { |
| 90 | + cy.get(`#${dateInput12}`).find("input").should('have.value', dateInRange); |
| 91 | + cy.get(`#${dateInput12}`).find(".cmp-adaptiveform-datepicker__errormessage") |
| 92 | + .should('have.text', ''); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should accept minimum date value", () => { |
| 97 | + const [dateInput12, dateInput12FieldView] = Object.entries(formContainer._fields)[12]; |
| 98 | + const minDate = "2025-12-02"; // Exactly the minimum date |
| 99 | + |
| 100 | + cy.get(`#${dateInput12}`).find("input").clear().type(minDate).blur().then(() => { |
| 101 | + cy.get(`#${dateInput12}`).find("input").should('have.value', minDate); |
| 102 | + // Note: Since excludeMinimum is true, this date should show validation error |
| 103 | + // But the min attribute should still be set correctly |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should accept maximum date value", () => { |
| 108 | + const [dateInput12, dateInput12FieldView] = Object.entries(formContainer._fields)[12]; |
| 109 | + const maxDate = "2025-12-16"; // Exactly the maximum date |
| 110 | + |
| 111 | + cy.get(`#${dateInput12}`).find("input").clear().type(maxDate).blur().then(() => { |
| 112 | + cy.get(`#${dateInput12}`).find("input").should('have.value', maxDate); |
| 113 | + // Note: Since excludeMaximum is true, this date should show validation error |
| 114 | + // But the max attribute should still be set correctly |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should format min and max dates correctly as yyyy-MM-dd", () => { |
| 119 | + const [dateInput12, dateInput12FieldView] = Object.entries(formContainer._fields)[12]; |
| 120 | + |
| 121 | + // Verify the format matches HTML5 date input format (yyyy-MM-dd) |
| 122 | + cy.get(`#${dateInput12}`).find("input").invoke('attr', 'min').then((minValue) => { |
| 123 | + expect(minValue).to.match(/^\d{4}-\d{2}-\d{2}$/); |
| 124 | + expect(minValue).to.equal('2025-12-02'); |
| 125 | + }); |
| 126 | + |
| 127 | + cy.get(`#${dateInput12}`).find("input").invoke('attr', 'max').then((maxValue) => { |
| 128 | + expect(maxValue).to.match(/^\d{4}-\d{2}-\d{2}$/); |
| 129 | + expect(maxValue).to.equal('2025-12-16'); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should not have min or max attributes when constraints are not set", () => { |
| 134 | + // Find a datepicker without min/max constraints (first datepicker typically doesn't have them) |
| 135 | + const [datePicker1, datePicker1FieldView] = Object.entries(formContainer._fields)[0]; |
| 136 | + |
| 137 | + cy.get(`#${datePicker1}`).find("input").should('not.have.attr', 'min'); |
| 138 | + cy.get(`#${datePicker1}`).find("input").should('not.have.attr', 'max'); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
0 commit comments