|
| 1 | +// TODO: Use aliased import(@cypress-dir) once #9631 is merged |
| 2 | +import { LABEL_CONFIG_KEYS } from './constants/command_constants.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Helper function to validate that config objects only contain valid keys |
| 6 | + * |
| 7 | + * @param {Object} config - The configuration object to validate |
| 8 | + * @param {Object} validKeysObject - The object containing valid keys (e.g., LABEL_CONFIG_KEYS) |
| 9 | + * @param {string} configType - The type of configuration being validated (for error messages) |
| 10 | + */ |
| 11 | +const validateConfigKeys = (config, validKeysObject, configType) => { |
| 12 | + const validKeys = Object.values(validKeysObject); |
| 13 | + |
| 14 | + Object.keys(config).forEach(key => { |
| 15 | + if (!validKeys.includes(key)) { |
| 16 | + cy.logAndThrowError( |
| 17 | + `Unknown key "${key}" in ${configType} config. Valid keys are: ${validKeys.join(', ')}` |
| 18 | + ); |
| 19 | + } |
| 20 | + }); |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Validates form field labels based on provided configurations |
| 25 | + * |
| 26 | + * @param {Array} labelConfigs - Array of label configuration objects |
| 27 | + * @param {string} labelConfigs[].forValue - The 'for' attribute value of the label |
| 28 | + * @param {string} [labelConfigs[].expectedText] - The expected text content of the label |
| 29 | + * |
| 30 | + * Example: |
| 31 | + * cy.validateFormLabels([ |
| 32 | + * { [LABEL_CONFIG_KEYS.FOR_VALUE]: 'name', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Name' }, |
| 33 | + * { [LABEL_CONFIG_KEYS.FOR_VALUE]: 'email', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Email Address' } |
| 34 | + * ]); |
| 35 | + * |
| 36 | + * Or using regular object keys: |
| 37 | + * cy.validateFormLabels([ |
| 38 | + * { forValue: 'name', expectedText: 'Name' }, |
| 39 | + * { forValue: 'email', expectedText: 'Email Address' } |
| 40 | + * ]); |
| 41 | + * |
| 42 | + * Both approaches work but using config-keys object(LABEL_CONFIG_KEYS) is recommended to avoid typos and unknown keys |
| 43 | + */ |
| 44 | +Cypress.Commands.add('validateFormLabels', (labelConfigs) => { |
| 45 | + if (!Array.isArray(labelConfigs)) { |
| 46 | + cy.logAndThrowError('labelConfigs must be an array'); |
| 47 | + } |
| 48 | + |
| 49 | + if (!labelConfigs.length) { |
| 50 | + cy.logAndThrowError('labelConfigs array cannot be empty'); |
| 51 | + } |
| 52 | + |
| 53 | + labelConfigs.forEach((config) => { |
| 54 | + validateConfigKeys(config, LABEL_CONFIG_KEYS, 'label'); |
| 55 | + |
| 56 | + const forValue = config[LABEL_CONFIG_KEYS.FOR_VALUE]; |
| 57 | + const expectedText = config[LABEL_CONFIG_KEYS.EXPECTED_TEXT]; |
| 58 | + |
| 59 | + if (!forValue) { |
| 60 | + cy.logAndThrowError( |
| 61 | + `${LABEL_CONFIG_KEYS.FOR_VALUE} is required for each label config` |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + const labelCheck = cy |
| 66 | + .getFormLabelByForAttribute({ forValue }) |
| 67 | + .should('be.visible'); |
| 68 | + |
| 69 | + if (expectedText) { |
| 70 | + labelCheck.and('contain.text', expectedText); |
| 71 | + } |
| 72 | + }); |
| 73 | +}); |
0 commit comments