Skip to content

Commit

Permalink
[patch] small defect fixes (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmisty committed Nov 8, 2023
1 parent 3380baa commit be29210
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 17 deletions.
73 changes: 73 additions & 0 deletions integration/e2e/commands/do-sync-command.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
describe('suite', () => {
describe('should pass', () => {
beforeEach(() => {
cy.intercept('mytest.com**', {
body: `<html>
<head></head>
<body>
<a href="#2" data-qa-id="link-2">My link</a>
<a href="#3" data-qa-id="link-3">My link 3</a>
</body>
</html>
`,
});

cy.visit('mytest.com');
});

it('should click when added during chain (no custom commands)', () => {
cy.get('[data-qa-id=link-2]')
.doSyncCommand(s => {
expect(s.text()).eq('My link');
})
.click();
});

it('should click when added during chain', () => {
cy.qaId('link-2')
.doSyncCommand(s => {
expect(s.text()).eq('My link');
})
.click();
});

it('should click when added during chain (several)', () => {
cy.qaId('link-2')
.doSyncCommand(s => {
expect(s.text()).eq('My link');
})
.doSyncCommand(s => {
expect(s.text()).eq('My link');
})
.click();
});

it('should succeed when added before should', () => {
cy.qaId('link-60')
.doSyncCommand(() => {
console.log('hello');
})
.should('not.exist');
});

it('should succeed when cy commands inside', () => {
cy.qaId('link-60')
.doSyncCommand(s => {
cy.log('hello');
cy.wrap(s);
})
.should('not.exist');
});

it('should succeed when between cy commands with cy commands inside', () => {
cy.get('body')
.find('a')
.doSyncCommand(s => {
cy.log('hello');
cy.wrap(s, { log: false });
})
.eq(1)
.should('have.text', 'My link 3');
});
});
});
2 changes: 1 addition & 1 deletion integration/support/setup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare namespace Cypress {
myLog(message: string): Chainable<void>;
otherCmd(message: string): Chainable<void>;
fileExists(filePath: string): Chainable<boolean>;
qaId(selector: string): Chainable<Element>;
qaId(selector: string): Chainable<JQuery>;
promiseTest(delay?: number): Chainable<any>;
}
}
17 changes: 6 additions & 11 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,8 @@ export const registerCommands = () => {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getPrevSubjects = (cmd: any, arr: any[] = []): any[] => {
arr.unshift(cmd?.attributes?.subject);

if (cmd?.attributes?.prev) {
return getPrevSubjects(cmd.attributes.prev, arr);
}

return arr;
const getLastSubject = (cmd: any): any => {
return cmd?.get('prev')?.attributes?.subject;
};

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

if (subjs.length > 1) {
prevSubj = subjs[subjs.length - 2];
if (subj) {
prevSubj = subj;
}

syncFn(prevSubj);

/* if (queue().length > commandsCount) {
Expand Down
36 changes: 31 additions & 5 deletions tests/test-folder/mocha-events/commands/do-sync-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,31 @@ describe('do sync command', () => {
cy.visit('mytest.com');
});
it('should click when added during chain (no custom commands)', () => {
cy.get('[data-qa-id=link-2]')
.doSyncCommand(s => {
expect(s.text()).eq('My link');
})
.click();
});
it('should click when added during chain', () => {
cy.qaId('link-2').doSyncCommand(() => {
console.log('hello')
}).click();
cy.qaId('link-2')
.doSyncCommand(s => {
expect(s.text()).eq('My link');
})
.click();
});
it('should click when added during chain (several)', () => {
cy.qaId('link-2')
.doSyncCommand(s => {
expect(s.text()).eq('My link');
})
.doSyncCommand(s => {
expect(s.text()).eq('My link');
})
.click();
});
it('should succeed when added before should', () => {
Expand Down Expand Up @@ -70,12 +91,12 @@ describe('do sync command', () => {
it('should have results', () => {
// should not fail run
checkCyResults(res?.result?.res, {
totalPassed: 4,
totalPassed: 6,
totalFailed: 0,
totalPending: 0,
totalSkipped: 0,
totalSuites: 1,
totalTests: 4,
totalTests: 6,
});
});
beforeAll(() => {
Expand Down Expand Up @@ -108,6 +129,11 @@ describe('do sync command', () => {
},
],
},
{
name: 'assert: expected **My link** to equal **My link**',
status: 'passed',
steps: [],
},
{
name: 'click',
status: 'passed',
Expand Down

0 comments on commit be29210

Please sign in to comment.