|
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 | * Validates form field labels based on provided configurations |
@@ -50,3 +54,113 @@ Cypress.Commands.add('validateFormLabels', (labelConfigs) => { |
50 | 54 | } |
51 | 55 | }); |
52 | 56 | }); |
| 57 | + |
| 58 | +/** |
| 59 | + * Validates form input fields based on provided configurations |
| 60 | + * |
| 61 | + * @param {Array} fieldConfigs - Array of field configuration objects |
| 62 | + * @param {string} fieldConfigs[].id - The ID of the form field |
| 63 | + * @param {string} [fieldConfigs[].fieldType='input'] - The type of field ('input', 'select', 'textarea') |
| 64 | + * @param {string} [fieldConfigs[].inputFieldType='text'] - The type of input field ('text', 'password', 'number') |
| 65 | + * @param {boolean} [fieldConfigs[].shouldBeDisabled=false] - Whether the field should be disabled |
| 66 | + * @param {string} [fieldConfigs[].expectedValue] - The expected value of the field |
| 67 | + * |
| 68 | + * Example: |
| 69 | + * cy.validateFormFields([ |
| 70 | + * { [FIELD_CONFIG_KEYS.ID]: 'name', [FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED]: true }, |
| 71 | + * { [FIELD_CONFIG_KEYS.ID]: 'email', [FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE]: 'email' }, |
| 72 | + * { |
| 73 | + * [FIELD_CONFIG_KEYS.ID]: 'role', |
| 74 | + * [FIELD_CONFIG_KEYS.FIELD_TYPE]: FIELD_TYPES.SELECT, |
| 75 | + * [FIELD_CONFIG_KEYS.EXPECTED_VALUE]: 'admin' |
| 76 | + * } |
| 77 | + * ]); |
| 78 | + * |
| 79 | + * Or using regular object keys: |
| 80 | + * cy.validateFormFields([ |
| 81 | + * { id: 'name', shouldBeDisabled: true }, |
| 82 | + * { id: 'email' }, |
| 83 | + * { id: 'role', fieldType: 'select', expectedValue: 'admin' } |
| 84 | + * ]); |
| 85 | + * |
| 86 | + * Both approaches work but using config-keys object(FIELD_CONFIG_KEYS) is recommended to avoid typos and unknown keys |
| 87 | + */ |
| 88 | +Cypress.Commands.add('validateFormFields', (fieldConfigs) => { |
| 89 | + if (!Array.isArray(fieldConfigs)) { |
| 90 | + cy.logAndThrowError('fieldConfigs must be an array'); |
| 91 | + } |
| 92 | + |
| 93 | + if (!fieldConfigs.length) { |
| 94 | + cy.logAndThrowError('fieldConfigs array cannot be empty'); |
| 95 | + } |
| 96 | + |
| 97 | + fieldConfigs.forEach((config) => { |
| 98 | + const id = config[FIELD_CONFIG_KEYS.ID]; |
| 99 | + const fieldType = config[FIELD_CONFIG_KEYS.FIELD_TYPE] || FIELD_TYPES.INPUT; |
| 100 | + const inputFieldType = config[FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE] || 'text'; |
| 101 | + const shouldBeDisabled = |
| 102 | + config[FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED] || false; |
| 103 | + const expectedValue = config[FIELD_CONFIG_KEYS.EXPECTED_VALUE]; |
| 104 | + |
| 105 | + if (!id) { |
| 106 | + cy.logAndThrowError( |
| 107 | + `${FIELD_CONFIG_KEYS.ID} is required for each field config` |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + // Check field based on type |
| 112 | + switch (fieldType) { |
| 113 | + case FIELD_TYPES.INPUT: |
| 114 | + cy.getFormInputFieldByIdAndType({ |
| 115 | + inputId: id, |
| 116 | + inputType: inputFieldType, |
| 117 | + }) |
| 118 | + .should('be.visible') |
| 119 | + .then((field) => { |
| 120 | + if (shouldBeDisabled) { |
| 121 | + expect(field).to.be.disabled; |
| 122 | + } else { |
| 123 | + expect(field).to.not.be.disabled; |
| 124 | + } |
| 125 | + |
| 126 | + if (expectedValue) { |
| 127 | + cy.wrap(field).should('have.value', expectedValue); |
| 128 | + } |
| 129 | + }); |
| 130 | + break; |
| 131 | + case FIELD_TYPES.SELECT: |
| 132 | + cy.getFormSelectFieldById({ selectId: id }) |
| 133 | + .should('be.visible') |
| 134 | + .then((field) => { |
| 135 | + if (shouldBeDisabled) { |
| 136 | + expect(field).to.be.disabled; |
| 137 | + } else { |
| 138 | + expect(field).to.not.be.disabled; |
| 139 | + } |
| 140 | + |
| 141 | + if (expectedValue) { |
| 142 | + cy.wrap(field).should('have.value', expectedValue); |
| 143 | + } |
| 144 | + }); |
| 145 | + break; |
| 146 | + case FIELD_TYPES.TEXTAREA: |
| 147 | + cy.getFormTextareaById({ textareaId: id }) |
| 148 | + .should('be.visible') |
| 149 | + .then((field) => { |
| 150 | + if (shouldBeDisabled) { |
| 151 | + expect(field).to.be.disabled; |
| 152 | + } else { |
| 153 | + expect(field).to.not.be.disabled; |
| 154 | + } |
| 155 | + |
| 156 | + if (expectedValue) { |
| 157 | + cy.wrap(field).should('have.value', expectedValue); |
| 158 | + } |
| 159 | + }); |
| 160 | + break; |
| 161 | + |
| 162 | + default: |
| 163 | + cy.logAndThrowError(`Unsupported field type: ${fieldType}`); |
| 164 | + } |
| 165 | + }); |
| 166 | +}); |
0 commit comments