Skip to content

Commit be29210

Browse files
authored
[patch] small defect fixes (#73)
1 parent 3380baa commit be29210

File tree

4 files changed

+111
-17
lines changed

4 files changed

+111
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
describe('suite', () => {
2+
describe('should pass', () => {
3+
beforeEach(() => {
4+
cy.intercept('mytest.com**', {
5+
body: `<html>
6+
<head></head>
7+
<body>
8+
<a href="#2" data-qa-id="link-2">My link</a>
9+
<a href="#3" data-qa-id="link-3">My link 3</a>
10+
</body>
11+
</html>
12+
`,
13+
});
14+
15+
cy.visit('mytest.com');
16+
});
17+
18+
it('should click when added during chain (no custom commands)', () => {
19+
cy.get('[data-qa-id=link-2]')
20+
.doSyncCommand(s => {
21+
expect(s.text()).eq('My link');
22+
})
23+
.click();
24+
});
25+
26+
it('should click when added during chain', () => {
27+
cy.qaId('link-2')
28+
.doSyncCommand(s => {
29+
expect(s.text()).eq('My link');
30+
})
31+
.click();
32+
});
33+
34+
it('should click when added during chain (several)', () => {
35+
cy.qaId('link-2')
36+
.doSyncCommand(s => {
37+
expect(s.text()).eq('My link');
38+
})
39+
.doSyncCommand(s => {
40+
expect(s.text()).eq('My link');
41+
})
42+
.click();
43+
});
44+
45+
it('should succeed when added before should', () => {
46+
cy.qaId('link-60')
47+
.doSyncCommand(() => {
48+
console.log('hello');
49+
})
50+
.should('not.exist');
51+
});
52+
53+
it('should succeed when cy commands inside', () => {
54+
cy.qaId('link-60')
55+
.doSyncCommand(s => {
56+
cy.log('hello');
57+
cy.wrap(s);
58+
})
59+
.should('not.exist');
60+
});
61+
62+
it('should succeed when between cy commands with cy commands inside', () => {
63+
cy.get('body')
64+
.find('a')
65+
.doSyncCommand(s => {
66+
cy.log('hello');
67+
cy.wrap(s, { log: false });
68+
})
69+
.eq(1)
70+
.should('have.text', 'My link 3');
71+
});
72+
});
73+
});

integration/support/setup.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare namespace Cypress {
1010
myLog(message: string): Chainable<void>;
1111
otherCmd(message: string): Chainable<void>;
1212
fileExists(filePath: string): Chainable<boolean>;
13-
qaId(selector: string): Chainable<Element>;
13+
qaId(selector: string): Chainable<JQuery>;
1414
promiseTest(delay?: number): Chainable<any>;
1515
}
1616
}

src/commands/index.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,8 @@ export const registerCommands = () => {
8181
}
8282

8383
// eslint-disable-next-line @typescript-eslint/no-explicit-any
84-
const getPrevSubjects = (cmd: any, arr: any[] = []): any[] => {
85-
arr.unshift(cmd?.attributes?.subject);
86-
87-
if (cmd?.attributes?.prev) {
88-
return getPrevSubjects(cmd.attributes.prev, arr);
89-
}
90-
91-
return arr;
84+
const getLastSubject = (cmd: any): any => {
85+
return cmd?.get('prev')?.attributes?.subject;
9286
};
9387

9488
// not changing the subject
@@ -98,12 +92,13 @@ export const registerCommands = () => {
9892
// const queue = () => (cy as any).queue.queueables;
9993
// const commandsCount = queue().length;
10094
// eslint-disable-next-line @typescript-eslint/no-explicit-any
101-
const subjs = getPrevSubjects((cy as any).state()?.current);
95+
const subj = getLastSubject((cy as any).state('current'));
10296
let prevSubj = undefined;
10397

104-
if (subjs.length > 1) {
105-
prevSubj = subjs[subjs.length - 2];
98+
if (subj) {
99+
prevSubj = subj;
106100
}
101+
107102
syncFn(prevSubj);
108103

109104
/* if (queue().length > commandsCount) {

tests/test-folder/mocha-events/commands/do-sync-command.test.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,31 @@ describe('do sync command', () => {
3131
cy.visit('mytest.com');
3232
});
3333
34+
it('should click when added during chain (no custom commands)', () => {
35+
cy.get('[data-qa-id=link-2]')
36+
.doSyncCommand(s => {
37+
expect(s.text()).eq('My link');
38+
})
39+
.click();
40+
});
41+
3442
it('should click when added during chain', () => {
35-
cy.qaId('link-2').doSyncCommand(() => {
36-
console.log('hello')
37-
}).click();
43+
cy.qaId('link-2')
44+
.doSyncCommand(s => {
45+
expect(s.text()).eq('My link');
46+
})
47+
.click();
48+
});
49+
50+
it('should click when added during chain (several)', () => {
51+
cy.qaId('link-2')
52+
.doSyncCommand(s => {
53+
expect(s.text()).eq('My link');
54+
})
55+
.doSyncCommand(s => {
56+
expect(s.text()).eq('My link');
57+
})
58+
.click();
3859
});
3960
4061
it('should succeed when added before should', () => {
@@ -70,12 +91,12 @@ describe('do sync command', () => {
7091
it('should have results', () => {
7192
// should not fail run
7293
checkCyResults(res?.result?.res, {
73-
totalPassed: 4,
94+
totalPassed: 6,
7495
totalFailed: 0,
7596
totalPending: 0,
7697
totalSkipped: 0,
7798
totalSuites: 1,
78-
totalTests: 4,
99+
totalTests: 6,
79100
});
80101
});
81102
beforeAll(() => {
@@ -108,6 +129,11 @@ describe('do sync command', () => {
108129
},
109130
],
110131
},
132+
{
133+
name: 'assert: expected **My link** to equal **My link**',
134+
status: 'passed',
135+
steps: [],
136+
},
111137
{
112138
name: 'click',
113139
status: 'passed',

0 commit comments

Comments
 (0)