Skip to content

Commit 63ca416

Browse files
Added command to validate form footer buttons
1 parent 948fb0f commit 63ca416

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

cypress/support/commands/form_elements_validation_commands.js

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// TODO: Use aliased import(@cypress-dir) once #9631 is merged
2-
import { LABEL_CONFIG_KEYS } from './constants/command_constants.js';
2+
import {
3+
LABEL_CONFIG_KEYS,
4+
FIELD_CONFIG_KEYS,
5+
FIELD_TYPES,
6+
BUTTON_CONFIG_KEYS,
7+
} from './constants/command_constants.js';
38

49
/**
510
* Helper function to validate that config objects only contain valid keys
@@ -10,11 +15,13 @@ import { LABEL_CONFIG_KEYS } from './constants/command_constants.js';
1015
*/
1116
const validateConfigKeys = (config, validKeysObject, configType) => {
1217
const validKeys = Object.values(validKeysObject);
13-
14-
Object.keys(config).forEach(key => {
18+
19+
Object.keys(config).forEach((key) => {
1520
if (!validKeys.includes(key)) {
1621
cy.logAndThrowError(
17-
`Unknown key "${key}" in ${configType} config. Valid keys are: ${validKeys.join(', ')}`
22+
`Unknown key "${key}" in ${configType} config. Valid keys are: ${validKeys.join(
23+
', '
24+
)}`
1825
);
1926
}
2027
});
@@ -183,3 +190,65 @@ Cypress.Commands.add('validateFormFields', (fieldConfigs) => {
183190
}
184191
});
185192
});
193+
194+
/**
195+
* Validates form buttons based on provided configurations
196+
*
197+
* @param {Array} buttonConfigs - Array of button configuration objects
198+
* @param {string} buttonConfigs[].buttonText - The text of the button
199+
* @param {string} [buttonConfigs[].buttonType='button'] - The type of button (e.g., 'submit', 'reset')
200+
* @param {boolean} [buttonConfigs[].shouldBeDisabled=false] - Whether the button should be disabled
201+
*
202+
* Example:
203+
* cy.validateFormFooterButtons([
204+
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Cancel' },
205+
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Reset', [BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED]: true },
206+
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Submit', [BUTTON_CONFIG_KEYS.BUTTON_TYPE]: 'submit' }
207+
* ]);
208+
*
209+
* Or using regular object keys:
210+
* cy.validateFormFooterButtons([
211+
* { buttonText: 'Cancel' },
212+
* { buttonText: 'Reset', shouldBeDisabled: true },
213+
* { buttonText: 'Submit', buttonType: 'submit' }
214+
* ]);
215+
*
216+
* Both approaches work but using config-keys object(BUTTON_CONFIG_KEYS) is recommended to avoid typos and unknown keys
217+
*/
218+
Cypress.Commands.add('validateFormFooterButtons', (buttonConfigs) => {
219+
if (!Array.isArray(buttonConfigs)) {
220+
cy.logAndThrowError('buttonConfigs must be an array');
221+
}
222+
223+
if (!buttonConfigs.length) {
224+
cy.logAndThrowError('buttonConfigs array cannot be empty');
225+
}
226+
227+
buttonConfigs.forEach((config) => {
228+
validateConfigKeys(config, BUTTON_CONFIG_KEYS, 'button');
229+
230+
const buttonText = config[BUTTON_CONFIG_KEYS.BUTTON_TEXT];
231+
const buttonType = config[BUTTON_CONFIG_KEYS.BUTTON_TYPE] || 'button';
232+
const shouldBeDisabled =
233+
config[BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED] || false;
234+
235+
if (!buttonText) {
236+
cy.logAndThrowError(
237+
`${BUTTON_CONFIG_KEYS.BUTTON_TEXT} is required for each button config`
238+
);
239+
}
240+
241+
const buttonCheck = cy
242+
.getFormFooterButtonByTypeWithText({
243+
buttonText,
244+
buttonType,
245+
})
246+
.should('be.visible');
247+
248+
if (shouldBeDisabled) {
249+
buttonCheck.and('be.disabled');
250+
} else {
251+
buttonCheck.and('be.enabled');
252+
}
253+
});
254+
});

0 commit comments

Comments
 (0)