Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(e2e): add testing for Header menu items #852

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"chainable",
"codecov",
"codesandbox",
"combobox",
"flexbugs",
"gemoji",
"instanceof",
Expand Down
122 changes: 122 additions & 0 deletions cypress/integration/header.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import type { Route } from '@config';
import { routes, siteConfig } from '@config';

describe('Header', () => {
const menuItems = ['/', ...routes, 'SearchBar', 'ThemeSwitch', 'Ellipsis'];
const logoIndex = 0;
const linksStartIndex = logoIndex + 1;
const searchBarIndex = linksStartIndex + routes.length;
const themeSwitchIndex = searchBarIndex + 1;

it('should display fully menu items on each route', () => {
cy.wrap(routes).each((route: Route) => {
cy.visitRoute(route.path, route.name);

cy.getByRole('menu')
.findByRole('menuitem')
.should('have.length', menuItems.length);
});
});

it('should route to home page when logo clicked', () => {
cy.wrap(routes).each((route: Route) => {
cy.visitRoute(route.path, route.name);

cy.getByRole('menu').findByRole('menuitem').eq(logoIndex).click();

cy.validRoute('/', siteConfig.title);
});
});

it('should route to pages when links clicked', () => {
cy.wrap(routes).each((route: Route, index) => {
cy.visitRoute(route.path, route.name);

cy.getByRole('menu')
.findByRole('menuitem')
.eq(index + linksStartIndex)
.click();

cy.validRoute(routes[index].path, routes[index].name);
});
});

it('should focus search bar when search bar clicked', () => {
cy.wrap(routes).each((route: Route) => {
cy.visitRoute(route.path, route.name);

cy.getByRole('menu')
.findByRole('menuitem')
.eq(searchBarIndex)
.click()
.should('have.class', 'ant-menu-item-selected');
});
});

it('should search posts when search bar worked', () => {
cy.wrap(routes).each((route: Route) => {
cy.visitRoute(route.path, route.name);

cy.getByRole('menu')
.findByRole('menuitem')
.eq(searchBarIndex)
.findByRole('combobox')
.type('Notes')
.should('have.value', 'Notes');
});
});

it('should clear input when search bar clear button clicked', () => {
cy.wrap(routes).each((route: Route) => {
cy.visitRoute(route.path, route.name);

cy.getByRole('menu')
.findByRole('menuitem')
.eq(searchBarIndex)
.findByRole('combobox')
.type('Notes')
.should('have.value', 'Notes');

cy.getByRole('menu')
.findByRole('menuitem')
.eq(searchBarIndex)
.findByLabel('close-circle')
.click();

cy.getByRole('menu')
.findByRole('menuitem')
.eq(searchBarIndex)
.findByRole('combobox')
.should('have.value', '');
});
});

it('should switch theme when theme switch button clicked', () => {
cy.wrap(routes).each((route: Route) => {
cy.visitRoute(route.path, route.name);
cy.clearLocalStorage();

cy.getByRole('menu')
.findByRole('menuitem')
.eq(themeSwitchIndex)
.findByLabel('Sun')
.click();

cy.window().then(window => {
expect(window.localStorage.getItem('dark-mode')).to.equal('true');
expect(window.document.body.classList.contains('dark')).to.equal(true);
});

cy.getByRole('menu')
.findByRole('menuitem')
.eq(themeSwitchIndex)
.findByLabel('Moon')
.click();

cy.window().then(window => {
expect(window.localStorage.getItem('dark-mode')).to.equal('false');
expect(window.document.body.classList.contains('dark')).to.equal(false);
});
});
});
});
11 changes: 3 additions & 8 deletions cypress/integration/home.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { routes, siteConfig } from '@config';

describe('Home', () => {
describe('Home Page', () => {
beforeEach(() => {
cy.visit('/');
cy.visitRoute('/', siteConfig.title);
});

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

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

Expand Down
13 changes: 0 additions & 13 deletions cypress/integration/routes.spec.ts

This file was deleted.

27 changes: 27 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,37 @@ Cypress.Commands.add(
}
);

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

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

Cypress.Commands.add('getByTestId', (testId, options) => {
return cy.get(`[data-testid="${testId}"]`, options);
});

Cypress.Commands.add('getByLabel', (label, options) => {
return cy.get(`[aria-label="${label}"]`, options);
});

Cypress.Commands.add('validRoute', (path, title) => {
cy.url().should('include', path);

if (title) {
cy.title().should('include', title);
}
});

Cypress.Commands.add('visitRoute', (path, title) => {
cy.visit(path);
cy.validRoute(path, title);
});
13 changes: 13 additions & 0 deletions cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ declare global {
options?: Parameters<Cypress.ChainableMethods['find']>[1]
): Chainable<JQuery<HTMLElement>>;

findByLabel(
label: 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]
Expand All @@ -24,6 +29,14 @@ declare global {
testId: Parameters<Cypress.ChainableMethods['get']>[0],
options?: Parameters<Cypress.ChainableMethods['get']>[1]
): Chainable<JQuery<HTMLElement>>;

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

validRoute(path: string, title?: string): void;
visitRoute(path: string, title?: string): void;
}
}
}
Expand Down