|
1 | 1 | // TODO: Use aliased import(@cypress-dir) once #9631 is merged |
2 | | -import { LABEL_CONFIG_KEYS } from './command_constants.js'; |
| 2 | +import { |
| 3 | + LABEL_CONFIG_KEYS, |
| 4 | + FIELD_CONFIG_KEYS, |
| 5 | + FIELD_TYPES, |
| 6 | +} from './command_constants.js'; |
3 | 7 |
|
4 | 8 | /** |
5 | 9 | * Helper function to validate that config objects only contain valid keys |
@@ -71,3 +75,115 @@ Cypress.Commands.add('validateFormLabels', (labelConfigs) => { |
71 | 75 | } |
72 | 76 | }); |
73 | 77 | }); |
| 78 | + |
| 79 | +/** |
| 80 | + * Validates form input fields based on provided configurations |
| 81 | + * |
| 82 | + * @param {Array} fieldConfigs - Array of field configuration objects |
| 83 | + * @param {string} fieldConfigs[].id - The ID of the form field |
| 84 | + * @param {string} [fieldConfigs[].fieldType='input'] - The type of field ('input', 'select', 'textarea') |
| 85 | + * @param {string} [fieldConfigs[].inputFieldType='text'] - The type of input field ('text', 'password', 'number') |
| 86 | + * @param {boolean} [fieldConfigs[].shouldBeDisabled=false] - Whether the field should be disabled |
| 87 | + * @param {string} [fieldConfigs[].expectedValue] - The expected value of the field |
| 88 | + * |
| 89 | + * Example: |
| 90 | + * cy.validateFormFields([ |
| 91 | + * { [FIELD_CONFIG_KEYS.ID]: 'name', [FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED]: true }, |
| 92 | + * { [FIELD_CONFIG_KEYS.ID]: 'email', [FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE]: 'email' }, |
| 93 | + * { |
| 94 | + * [FIELD_CONFIG_KEYS.ID]: 'role', |
| 95 | + * [FIELD_CONFIG_KEYS.FIELD_TYPE]: FIELD_TYPES.SELECT, |
| 96 | + * [FIELD_CONFIG_KEYS.EXPECTED_VALUE]: 'admin' |
| 97 | + * } |
| 98 | + * ]); |
| 99 | + * |
| 100 | + * Or using regular object keys: |
| 101 | + * cy.validateFormFields([ |
| 102 | + * { id: 'name', shouldBeDisabled: true }, |
| 103 | + * { id: 'email' }, |
| 104 | + * { id: 'role', fieldType: 'select', expectedValue: 'admin' } |
| 105 | + * ]); |
| 106 | + * |
| 107 | + * Both approaches work but using config-keys object(FIELD_CONFIG_KEYS) is recommended to avoid typos and unknown keys |
| 108 | + */ |
| 109 | +Cypress.Commands.add('validateFormFields', (fieldConfigs) => { |
| 110 | + if (!Array.isArray(fieldConfigs)) { |
| 111 | + cy.logAndThrowError('fieldConfigs must be an array'); |
| 112 | + } |
| 113 | + |
| 114 | + if (!fieldConfigs.length) { |
| 115 | + cy.logAndThrowError('fieldConfigs array cannot be empty'); |
| 116 | + } |
| 117 | + |
| 118 | + fieldConfigs.forEach((config) => { |
| 119 | + validateConfigKeys(config, FIELD_CONFIG_KEYS, 'field'); |
| 120 | + |
| 121 | + const id = config[FIELD_CONFIG_KEYS.ID]; |
| 122 | + const fieldType = config[FIELD_CONFIG_KEYS.FIELD_TYPE] || FIELD_TYPES.INPUT; |
| 123 | + const inputFieldType = config[FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE] || 'text'; |
| 124 | + const shouldBeDisabled = |
| 125 | + config[FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED] || false; |
| 126 | + const expectedValue = config[FIELD_CONFIG_KEYS.EXPECTED_VALUE]; |
| 127 | + |
| 128 | + if (!id) { |
| 129 | + cy.logAndThrowError( |
| 130 | + `${FIELD_CONFIG_KEYS.ID} is required for each field config` |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + // Check field based on type |
| 135 | + switch (fieldType) { |
| 136 | + case FIELD_TYPES.INPUT: |
| 137 | + cy.getFormInputFieldByIdAndType({ |
| 138 | + inputId: id, |
| 139 | + inputType: inputFieldType, |
| 140 | + }) |
| 141 | + .should('be.visible') |
| 142 | + .then((field) => { |
| 143 | + if (shouldBeDisabled) { |
| 144 | + expect(field).to.be.disabled; |
| 145 | + } else { |
| 146 | + expect(field).to.not.be.disabled; |
| 147 | + } |
| 148 | + |
| 149 | + if (expectedValue) { |
| 150 | + cy.wrap(field).should('have.value', expectedValue); |
| 151 | + } |
| 152 | + }); |
| 153 | + break; |
| 154 | + case FIELD_TYPES.SELECT: |
| 155 | + cy.getFormSelectFieldById({ selectId: id }) |
| 156 | + .should('be.visible') |
| 157 | + .then((field) => { |
| 158 | + if (shouldBeDisabled) { |
| 159 | + expect(field).to.be.disabled; |
| 160 | + } else { |
| 161 | + expect(field).to.not.be.disabled; |
| 162 | + } |
| 163 | + |
| 164 | + if (expectedValue) { |
| 165 | + cy.wrap(field).should('have.value', expectedValue); |
| 166 | + } |
| 167 | + }); |
| 168 | + break; |
| 169 | + case FIELD_TYPES.TEXTAREA: |
| 170 | + cy.getFormTextareaById({ textareaId: id }) |
| 171 | + .should('be.visible') |
| 172 | + .then((field) => { |
| 173 | + if (shouldBeDisabled) { |
| 174 | + expect(field).to.be.disabled; |
| 175 | + } else { |
| 176 | + expect(field).to.not.be.disabled; |
| 177 | + } |
| 178 | + |
| 179 | + if (expectedValue) { |
| 180 | + cy.wrap(field).should('have.value', expectedValue); |
| 181 | + } |
| 182 | + }); |
| 183 | + break; |
| 184 | + |
| 185 | + default: |
| 186 | + cy.logAndThrowError(`Unsupported field type: ${fieldType}`); |
| 187 | + } |
| 188 | + }); |
| 189 | +}); |
0 commit comments