Skip to content

Commit

Permalink
fix(cypress-commands): add testing-library style query commands (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabertazimi authored May 11, 2022
1 parent 8534399 commit d9edb46
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"cSpell.words": [
"antd",
"browserslist",
"chainable",
"codecov",
"codesandbox",
"flexbugs",
Expand Down
26 changes: 13 additions & 13 deletions cypress/integration/home.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ describe('Home', () => {
cy.visit('/');
});

it('should display home page', () => {
it('should contain home page route', () => {
cy.url().should('include', '/');
cy.title().should('include', siteConfig.title);
});

it('should display home page title', () => {
cy.get('[role="main"]').should('be.visible');
cy.getByRole('main').should('be.visible');
});

it('should toggle navigation menu when navigation button toggled', () => {
cy.get('[data-testid="hamburger-button"]').click();
cy.get('[role="banner"]').should('be.visible');
cy.get('[role="navigation"]')
cy.getByTestId('hamburger-button').click();
cy.getByRole('banner').should('be.visible');
cy.getByRole('navigation')
.should('be.visible')
.find('[role="link"]')
.findByRole('link')
.should('have.length', routes.length)
.and('be.visible');

cy.get('[data-testid="hamburger-icon"]').type('{enter}');
cy.get('[role="banner"]').should('not.be.visible');
cy.get('[role="navigation"]')
cy.getByTestId('hamburger-icon').type('{enter}');
cy.getByRole('banner').should('not.be.visible');
cy.getByRole('navigation')
.should('not.be.visible')
.find('[role="link"]')
.findByRole('link')
.should('not.be.visible');
});

it('should contain navigation links to pages', () => {
cy.get('[data-testid="hamburger-button"]').click();
cy.get('[role="navigation"]')
.find('[role="link"]')
cy.getByTestId('hamburger-button').click();
cy.getByRole('navigation')
.findByRole('link')
.each((link, index) => {
cy.wrap(link)
.contains(routes[index].name)
Expand Down
30 changes: 24 additions & 6 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/**
* -- This is a parent command --
* Cypress.Commands.add('login', (email, password) => { ... })
*
* -- This is a child command --
* Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
*
* -- This is a dual command --
* Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
*
Expand All @@ -13,3 +7,27 @@
*
* @see https://on.cypress.io/custom-commands
*/

Cypress.Commands.add(
'findByRole',
{ prevSubject: 'element' },
(subject, role) => {
return cy.wrap(subject).find(`[role="${role}"]`);
}
);

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

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

Cypress.Commands.add('getByTestId', testId => {
return cy.get(`[data-testid="${testId}"]`);
});
12 changes: 12 additions & 0 deletions cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
/**
* @see https://on.cypress.io/configuration
*/

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>>;
}
}
}

import './commands';

1 comment on commit d9edb46

@vercel
Copy link

@vercel vercel bot commented on d9edb46 May 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blog – ./

blog-sabertaz.vercel.app
blog.tazimi.dev
blog-git-main-sabertaz.vercel.app

Please sign in to comment.