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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(project): Cover unit and E2E test for disabled step #30 #31

Merged
merged 2 commits into from
Jun 14, 2023
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
describe('headless-stepper: HeadlessStepper component', () => {
beforeEach(() => cy.visit('/iframe.html?id=headlessstepper--primary'));

it('should render the component', () => {
cy.get('h1').should('contain', 'Welcome to HeadlessStepper!');
beforeEach(() =>
cy.visit('/iframe.html?id=headlessstepper-usestepper--use-stepper')
raisiqueira marked this conversation as resolved.
Show resolved Hide resolved
);

it('should render the component', () => {
cy.get('h1').should('contain', 'Welcome to HeadlessStepper!');
raisiqueira marked this conversation as resolved.
Show resolved Hide resolved
});

it('should navigate by clicking on the menu unless it is disabled', () => {
cy.get('p').should('contain.text', 'Current step: 0');

cy.get('ol > a').each((element) => {
const { attributes, textContent } = element[0];
const [, step] = textContent.split(' ');

const ariaDisabled = attributes.getNamedItem('aria-disabled');
const stepNumber = Number(step) - 1; // If step is called "Step 1" the index is 0

cy.wrap(element).click();

if (ariaDisabled) {
/**
* -1 means the step was not changed since it is disabled.
*/
cy.get('p').should('contain.text', `Current step: ${stepNumber - 1}`);
} else {
cy.get('p').should('contain.text', `Current step: ${stepNumber}`);
}
raisiqueira marked this conversation as resolved.
Show resolved Hide resolved

// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(200);
});

cy.get('p').should('contain.text', 'Current step: 5');
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';

import HeadlessStepper from './HeadlessStepper';

Expand All @@ -7,4 +7,22 @@ describe('HeadlessStepper', () => {
const { baseElement } = render(<HeadlessStepper />);
expect(baseElement).toBeTruthy();
});

it('should navigate by clicking on the menu unless it is disabled', () => {
const { queryByText, getByText } = render(<HeadlessStepper />);
expect(queryByText('Current step: 0')).toBeTruthy();

const secondStep = getByText('Step 2');
const thirdStep = getByText('Step 3');
const fourthStep = getByText('Step 4'); // step disabled

fireEvent.click(secondStep);
expect(queryByText('Current step: 1')).toBeTruthy();

fireEvent.click(thirdStep);
expect(queryByText('Current step: 2')).toBeTruthy();

fireEvent.click(fourthStep);
expect(queryByText('Current step: 3')).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function HeadlessStepper(props: HeadlessStepperProps) {
});
return (
<div className={styles['container']}>
<h1>Welcome to HeadlessStepper!</h1>
<div>
<nav style={{ display: 'flex' }} {...stepperProps}>
{stepsProps?.map((step, index) => (
Expand Down
2 changes: 1 addition & 1 deletion packages/headless-stepper/src/lib/hooks/useStepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const useStepper = ({
'aria-current': index === _currentStep ? 'step' : undefined,
'aria-selected': index === _currentStep,
onKeyDown: (event) => handleKeydown(event, index),
onClick: () => setCurrentStep(index),
onClick: () => (step.disabled ? null : setCurrentStep(index)),
ref: (element: HTMLElement) =>
(stepElementsRef.current[index] = element),
} as HTMLAttributes<HTMLElement>;
Expand Down
Loading