|
| 1 | +/* eslint-disable no-undef */ |
| 2 | +/** |
| 3 | + * @fileoverview |
| 4 | + * This file contains Cypress commands for interacting with dual-list components. |
| 5 | + * A dual-list component consists of two lists where items can be moved between them |
| 6 | + * using add/remove buttons. |
| 7 | + */ |
| 8 | +import { DUAL_LIST_ACTION_TYPE } from './constants/command_constants'; |
| 9 | + |
| 10 | +// CSS selectors for dual-list component elements |
| 11 | +const DUAL_LIST_BODY_SELECTOR = |
| 12 | + 'fieldset.bx--fieldset .bx--structured-list-tbody'; |
| 13 | +const DUAL_LIST_OPTION_ROW_SELECTOR = '.bx--structured-list-row'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Command to perform actions on a dual list component. |
| 17 | + * This command allows you to: |
| 18 | + * - Add selected items from the left list to the right list |
| 19 | + * - Remove selected items from the right list to the left list |
| 20 | + * - Add all items from the left list to the right list |
| 21 | + * - Remove all items from the right list to the left list |
| 22 | + * |
| 23 | + * @param {Object} options - Parameters for the action |
| 24 | + * @param {string} options.actionType - Type of action to perform. Use values from DUAL_LIST_ACTION_TYPE: |
| 25 | + * - DUAL_LIST_ACTION_TYPE.ADD: Move selected items from left to right |
| 26 | + * - DUAL_LIST_ACTION_TYPE.REMOVE: Move selected items from right to left |
| 27 | + * - DUAL_LIST_ACTION_TYPE.ADD_ALL: Move all items from left to right |
| 28 | + * - DUAL_LIST_ACTION_TYPE.REMOVE_ALL: Move all items from right to left |
| 29 | + * @param {Array<string>} [options.optionsToSelect] - Array of option texts to select |
| 30 | + * (required for 'add' and 'remove' actions, not needed for 'add-all' and 'remove-all') |
| 31 | + * |
| 32 | + * @example |
| 33 | + * // Add specific items from left to right |
| 34 | + * cy.dualListAction({ |
| 35 | + * actionType: DUAL_LIST_ACTION_TYPE.ADD, |
| 36 | + * optionsToSelect: ['Option 1', 'Option 2'] |
| 37 | + * }); |
| 38 | + * |
| 39 | + * @example |
| 40 | + * // Remove specific items from right to left |
| 41 | + * cy.dualListAction({ |
| 42 | + * actionType: DUAL_LIST_ACTION_TYPE.REMOVE, |
| 43 | + * optionsToSelect: ['Option 3'] |
| 44 | + * }); |
| 45 | + * |
| 46 | + * @example |
| 47 | + * // Add all items from left to right |
| 48 | + * cy.dualListAction({ |
| 49 | + * actionType: DUAL_LIST_ACTION_TYPE.ADD_ALL |
| 50 | + * }); |
| 51 | + * |
| 52 | + * @example |
| 53 | + * // Remove all items from right to left |
| 54 | + * cy.dualListAction({ |
| 55 | + * actionType: DUAL_LIST_ACTION_TYPE.REMOVE_ALL |
| 56 | + * }); |
| 57 | + */ |
| 58 | +Cypress.Commands.add('dualListAction', ({ actionType, optionsToSelect }) => { |
| 59 | + if (!actionType) { |
| 60 | + cy.logAndThrowError('actionType is required'); |
| 61 | + } |
| 62 | + |
| 63 | + const validActionTypes = Object.values(DUAL_LIST_ACTION_TYPE); |
| 64 | + if (!validActionTypes.includes(actionType)) { |
| 65 | + cy.logAndThrowError( |
| 66 | + `Action type "${actionType}" not supported. Valid action types are: ${validActionTypes.join( |
| 67 | + ', ' |
| 68 | + )}` |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + const isActionTypeAddAll = actionType === DUAL_LIST_ACTION_TYPE.ADD_ALL; |
| 73 | + if (isActionTypeAddAll || actionType === DUAL_LIST_ACTION_TYPE.REMOVE_ALL) { |
| 74 | + const targetButtonId = isActionTypeAddAll |
| 75 | + ? '#move-all-right' |
| 76 | + : '#move-all-left'; |
| 77 | + |
| 78 | + return cy.get(targetButtonId).click(); |
| 79 | + } |
| 80 | + |
| 81 | + const isActionTypeAdd = actionType === DUAL_LIST_ACTION_TYPE.ADD; |
| 82 | + if (isActionTypeAdd || actionType === DUAL_LIST_ACTION_TYPE.REMOVE) { |
| 83 | + if (!optionsToSelect || !optionsToSelect.length) { |
| 84 | + cy.logAndThrowError( |
| 85 | + 'optionsToSelect array is required for add and remove actions' |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + const targetListIndex = isActionTypeAdd ? 'first' : 'last'; |
| 90 | + const targetButtonId = isActionTypeAdd ? '#move-right' : '#move-left'; |
| 91 | + optionsToSelect.forEach((option) => { |
| 92 | + cy.get(DUAL_LIST_BODY_SELECTOR) |
| 93 | + [targetListIndex]() |
| 94 | + .contains(DUAL_LIST_OPTION_ROW_SELECTOR, option) |
| 95 | + .click(); |
| 96 | + }); |
| 97 | + cy.get(targetButtonId).click(); |
| 98 | + } |
| 99 | +}); |
0 commit comments