Skip to content

Commit 97a499b

Browse files
authored
Merge pull request #9705 from asirvadAbrahamVarghese/enhance-select-accordion-item
Enhance select accordion item
2 parents 03e55ca + 83e0642 commit 97a499b

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

cypress/support/commands/api_commands.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Custom command to get the intercepted API aliases stored in Cypress environment variables.
55
* This command returns the object containing all registered API interception aliases.
6-
*
6+
*
77
* @returns {Object} An object where keys are in format method-alias(e.g. post-myApiAlias) and values are typically the same alias names
88
* @example
99
* cy.getInterceptedApiAliases().then((aliases) => {
@@ -127,14 +127,12 @@ Cypress.Commands.add(
127127
/* ======================================================= */
128128

129129
// Check if this request is already registered
130-
cy.getInterceptedApiAliases().then((interceptedAliasesMap) => {
130+
return cy.getInterceptedApiAliases().then((interceptedAliasesMap) => {
131131
const aliasObjectKey = `${method.toLowerCase()}-${alias}`;
132132
// Check if this request is already registered
133133
const isAlreadyRegistered = !!interceptedAliasesMap[aliasObjectKey];
134134
// Setting wasRequestIntercepted flag to false initially
135-
if (waitOnlyIfRequestIntercepted) {
136-
setRequestIntercepted(false);
137-
}
135+
setRequestIntercepted(false);
138136
// Register the intercept if not already done
139137
if (!isAlreadyRegistered) {
140138
cy.intercept(method, urlPattern, (req) => {

cypress/support/commands/explorer.js

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-loop-func */
12
/* eslint-disable no-undef */
23

34
// title: String of the accordion title for the accordian panel to open.
@@ -35,10 +36,18 @@ Cypress.Commands.add('accordionItem', (name) => {
3536
* If the path is not found, it will throw an error.
3637
*/
3738
Cypress.Commands.add('selectAccordionItem', (accordionPath) => {
38-
cy.get('div.panel-collapse.collapse.in').then((expandedAccordion) => {
39+
cy.get('div.panel-collapse.collapse.in').then((accordionJqueryObject) => {
40+
/**
41+
* This variable stores the expanded accordion jquery object. This will be reassigned to the latest,
42+
* if the DOM updates after a node expansion(tree_autoload)
43+
*/
44+
let expandedAccordion = accordionJqueryObject;
3945
// Converting the list-items jQuery collection to an array for easier manipulation
40-
const listItems = [...expandedAccordion.find('li.list-group-item')];
41-
46+
/**
47+
* This variable stores the list items of the expanded accordion. This will be reassigned,
48+
* if the DOM updates after a node expansion(tree_autoload)
49+
*/
50+
let listItems = [...expandedAccordion.find('li.list-group-item')];
4251
/**
4352
* Function to recursively expand the accordion and click the target item.
4453
* @param {number} accordionPathIndex: The current index in the accordionPath array.
@@ -47,6 +56,12 @@ Cypress.Commands.add('selectAccordionItem', (accordionPath) => {
4756
* @returns {void}
4857
*/
4958
const expandAndClickPath = (accordionPathIndex, searchStartIndex) => {
59+
/* TODO: Remove logger once the command is confirmed to be stable */
60+
Cypress.log({
61+
name: 'selectAccordionItem',
62+
message: `Found ${listItems.length} list items, searching from index ${searchStartIndex}`,
63+
});
64+
5065
const accordionLabel = accordionPath[accordionPathIndex];
5166
const isClickableNode = accordionPathIndex === accordionPath.length - 1;
5267

@@ -89,21 +104,47 @@ Cypress.Commands.add('selectAccordionItem', (accordionPath) => {
89104
return;
90105
}
91106

92-
const isExpandable =
93-
currentLiElement.find('span.fa-angle-right').length > 0;
107+
const expandButton = currentLiElement.find('span.fa-angle-right');
108+
const isExpandable = expandButton.length > 0;
94109

95110
// If it's not the last label in the path, either expand the node
96111
// or move to the next label in the given path
97112
if (isExpandable) {
98113
// Expand the node
99-
cy.wrap(currentLiElement)
100-
.find('span.fa-angle-right')
101-
.click()
102-
.then(() => {
103-
// Recurse to the next label in the given path array and
104-
// start iteration from the current index
105-
expandAndClickPath(accordionPathIndex + 1, i + 1);
106-
});
114+
/* TODO: Remove logger once the command is confirmed to be stable */
115+
Cypress.log({
116+
name: 'selectAccordionItem',
117+
message: `Expanding node "${liText}"`,
118+
});
119+
cy.interceptApi({
120+
alias: 'treeAutoLoadApi',
121+
urlPattern: '/*/tree_autoload',
122+
triggerFn: () => cy.wrap(expandButton).click(),
123+
waitOnlyIfRequestIntercepted: true,
124+
onApiResponse: (interception) => {
125+
expect(interception.response.statusCode).to.equal(200);
126+
cy.get('div.panel-collapse.collapse.in').then(
127+
(latestAccordionJqueryObject) => {
128+
// Update the expanded accordion reference to the latest one
129+
expandedAccordion = latestAccordionJqueryObject;
130+
const updatedListItems = [
131+
...expandedAccordion.find('li.list-group-item'),
132+
];
133+
/* TODO: Remove logger once the command is confirmed to be stable */
134+
Cypress.log({
135+
name: 'selectAccordionItem',
136+
message: `Re-queried accordion - new list items count: ${updatedListItems.length}`,
137+
});
138+
// Update list items
139+
listItems = [...updatedListItems];
140+
}
141+
);
142+
},
143+
}).then(() => {
144+
// Recurse to the next label in the given path array and
145+
// start iteration from the current index
146+
expandAndClickPath(accordionPathIndex + 1, i + 1);
147+
});
107148
} else {
108149
// If it's already expanded, continue to the next label
109150
// start iteration from the current index

0 commit comments

Comments
 (0)