Skip to content

Commit

Permalink
test(e2e-commands): add options to custom traversal commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sabertazimi committed May 12, 2022
1 parent 1d3682c commit c2bb94a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
18 changes: 10 additions & 8 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@
Cypress.Commands.add(
'findByRole',
{ prevSubject: 'element' },
(subject, role) => {
return cy.wrap(subject, { log: false }).find(`[role="${role}"]`);
(subject, role, options) => {
return cy.wrap(subject, { log: false }).find(`[role="${role}"]`, options);
}
);

Cypress.Commands.add(
'findByTestId',
{ prevSubject: 'element' },
(subject, testId) => {
return cy.wrap(subject, { log: false }).find(`[data-testid="${testId}"]`);
(subject, testId, options) => {
return cy
.wrap(subject, { log: false })
.find(`[data-testid="${testId}"]`, options);
}
);

Cypress.Commands.add('getByRole', role => {
return cy.get(`[role="${role}"]`);
Cypress.Commands.add('getByRole', (role, options) => {
return cy.get(`[role="${role}"]`, options);
});

Cypress.Commands.add('getByTestId', testId => {
return cy.get(`[data-testid="${testId}"]`);
Cypress.Commands.add('getByTestId', (testId, options) => {
return cy.get(`[data-testid="${testId}"]`, options);
});
23 changes: 19 additions & 4 deletions cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@
declare global {
namespace Cypress {
interface Chainable {
findByRole(role: string): Chainable<JQuery<HTMLElement>>;
findByTestId(testId: string): Chainable<JQuery<HTMLElement>>;
getByRole(role: string): Chainable<JQuery<HTMLElement>>;
getByTestId(testId: string): Chainable<JQuery<HTMLElement>>;
findByRole(
role: Parameters<Cypress.ChainableMethods['find']>[0],
options?: Parameters<Cypress.ChainableMethods['find']>[1]
): Chainable<JQuery<HTMLElement>>;

findByTestId(
testId: Parameters<Cypress.ChainableMethods['find']>[0],
options?: Parameters<Cypress.ChainableMethods['find']>[1]
): Chainable<JQuery<HTMLElement>>;

getByRole(
role: Parameters<Cypress.ChainableMethods['get']>[0],
options?: Parameters<Cypress.ChainableMethods['get']>[1]
): Chainable<JQuery<HTMLElement>>;

getByTestId(
testId: Parameters<Cypress.ChainableMethods['get']>[0],
options?: Parameters<Cypress.ChainableMethods['get']>[1]
): Chainable<JQuery<HTMLElement>>;
}
}
}
Expand Down

0 comments on commit c2bb94a

Please sign in to comment.