Skip to content

Commit 83e0642

Browse files
Enhanced to re-query DOM when a node is expanded with tree_autoload
1 parent f576d9f commit 83e0642

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

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)