Skip to content

Commit aaa8881

Browse files
refactor the code
1 parent 5a9cf59 commit aaa8881

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed

accessibility/scanner/index.js

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,39 @@ const commandsToOverride = [
1313
const commandsToWrap = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scroll', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];
1414

1515
const performModifiedScan = (originalFn, Subject, stateType, ...args) => {
16-
// Initialize custom chaining
17-
const customChaining = cy.wrap(null).performScan();
18-
19-
// Function to modify arguments based on stateType
20-
const updateArgs = (args, stateType, newSubject) => {
21-
return stateType !== 'parent' ? [newSubject, ...args.slice(1)] : args;
22-
};
23-
24-
// Extract timeout from args if present
25-
const getTimeout = (args) => {
26-
const timeoutArg = args.find(arg => arg && arg.timeout !== undefined);
27-
return timeoutArg?.timeout;
28-
};
29-
30-
// Get the current Cypress subject
31-
const getCurrentSubject = () => {
32-
return cy.subject?.() || null;
33-
};
34-
35-
// Get the current Cypress subject chain
36-
const getSubjectChain = () => {
37-
return cy.subjectChain?.() || null;
38-
};
39-
40-
// Execute the command pipeline
41-
if (!Subject) {
42-
// Handle case without Subject
43-
const cypressCommandSubject = getCurrentSubject();
44-
45-
customChaining
46-
.then(() => cypressCommandSubject)
47-
.then(() => {
48-
originalFn(...args);
49-
});
50-
} else {
51-
// Handle case with Subject
52-
const cypressCommandChain = getSubjectChain();
53-
const timeout = getTimeout(args);
54-
55-
customChaining
56-
.performScanSubjectQuery(cypressCommandChain, timeout)
57-
.then({ timeout: 30000 }, (newSubject) => {
58-
originalFn(...updateArgs(args, stateType, newSubject));
59-
});
60-
}
61-
};
16+
let customChaining = cy.wrap(null).performScan();
17+
const updateSubj = (args, stateType, newSubject) =>
18+
stateType === 'parent' ? args : [newSubject, ...args.slice(1)];
19+
20+
const runCustomizedChainingCommand = () => {
21+
if (!Subject) {
22+
let cypressCommandSubject = null;
23+
const subjectFn = cy && cy.subject;
24+
if (subjectFn !== null && subjectFn !== void 0) {
25+
cypressCommandSubject = subjectFn.call(cy);
26+
}
6227

28+
customChaining
29+
.then(() => cypressCommandSubject)
30+
.then(() => {
31+
originalFn(...args);
32+
});
33+
} else {
34+
let cypressCommandChain = null, setTimeout = null;
35+
// Extract timeout value if present
36+
const timeoutArg = args.find(arg => arg !== null && arg !== void 0 ? arg.timeout : null);
37+
if (timeoutArg !== null && timeoutArg !== void 0) {
38+
setTimeout = timeoutArg.timeout;
39+
}
40+
const subjectChainFn = cy && cy.subjectChain;
41+
if (subjectChainFn !== null && subjectChainFn !== void 0) {
42+
cypressCommandChain = subjectChainFn.call(cy);
43+
}
44+
customChaining.performScanSubjectQuery(cypressCommandChain, setTimeout).then({timeout: 30000}, (newSubject) => originalFn(...updateSubj(args, stateType, newSubject)));
45+
}
46+
}
47+
runCustomizedChainingCommand();
48+
}
6349

6450
Cypress.Commands.add('processAccessibilityReport', () => {
6551
try {

0 commit comments

Comments
 (0)