Skip to content

Commit d6a5b83

Browse files
Added dualListAction command
1 parent c6fe6e2 commit d6a5b83

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

cypress/support/commands/constants/command_constants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ export const BUTTON_CONFIG_KEYS = {
3333
};
3434

3535
/* ========================================================== */
36+
37+
/**
38+
* Constant for dual-list component action types
39+
*/
40+
export const DUAL_LIST_ACTION_TYPE = {
41+
ADD: 'add',
42+
REMOVE: 'remove',
43+
ADD_ALL: 'add-all',
44+
REMOVE_ALL: 'remove-all',
45+
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
});

cypress/support/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
// Commands
4343
import './commands/api_commands.js';
4444
import './commands/custom_logging_commands.js';
45+
import './commands/dual_list_commands.js';
4546
import './commands/element_selectors.js';
4647
import './commands/explorer.js';
4748
import './commands/form_elements_validation_commands.js';

0 commit comments

Comments
 (0)