Skip to content

Commit

Permalink
✅ Add remaining test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mariush2 committed Nov 4, 2024
1 parent accd71c commit 7ac5380
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
85 changes: 85 additions & 0 deletions src/molecules/Stepper/Stepper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ test('Clicking a previous step via the label works as expected', async () => {
expect(screen.getByText(`Current step: 0`)).toBeInTheDocument();
});

test('maxWidth works as expected', () => {
const steps = fakeSteps();
const maxWidth = '800px';
render(<Stepper maxWidth={maxWidth} />, {
wrapper: ({ children }) => (
<StepperProvider steps={steps}>{children}</StepperProvider>
),
});

expect(screen.getByTestId('stepper-container')).toHaveStyleRule(
'max-width',
maxWidth
);
});

test('onlyShowCurrentLabel works as expected', () => {
const steps = fakeSteps();
render(<Stepper onlyShowCurrentStepLabel />, {
Expand All @@ -171,3 +186,73 @@ test('onlyShowCurrentLabel works as expected', () => {
}
}
});

test('Works as expected with substeps', async () => {
const steps: StepperProviderProps['steps'] = [
{
label: 'Step 1',
subSteps: [
{
title: faker.animal.dog(),
description: faker.lorem.sentence(),
},
{
title: faker.animal.cat(),
},
],
},
{
label: 'Step 2',
},
];
render(<StepperTestComponent />, {
wrapper: ({ children }) => (
<StepperProvider steps={steps}>{children}</StepperProvider>
),
});

const user = userEvent.setup();

expect(screen.getByText(`1 of 2`)).toBeInTheDocument();
expect(screen.getByText(steps[0].subSteps![0].title)).toBeInTheDocument();
expect(
screen.getByText(steps[0].subSteps![0].description!)
).toBeInTheDocument();

await user.click(screen.getByText('Next'));

expect(screen.getByText(steps[0].subSteps![1].title)).toBeInTheDocument();

await user.click(screen.getByText('Next'));

expect(screen.getByText(`Current step: 1`)).toBeInTheDocument();

await user.click(screen.getByText('Previous'));

expect(screen.getByText(steps[0].subSteps![1].title)).toBeInTheDocument();

await user.click(screen.getByText('Previous'));

expect(screen.getByText(steps[0].subSteps![0].title)).toBeInTheDocument();
});

test('Works as expected with title in steps', () => {
const steps: StepperProviderProps['steps'] = [
{
label: 'Step 1',
title: faker.animal.bear(),
description: faker.animal.dog(),
},
{
label: 'Step 2',
},
];
render(<StepperTestComponent />, {
wrapper: ({ children }) => (
<StepperProvider steps={steps}>{children}</StepperProvider>
),
});

expect(screen.getByText(steps[0].title!)).toBeInTheDocument();
expect(screen.getByText(steps[0].description!)).toBeInTheDocument();
});
5 changes: 1 addition & 4 deletions src/molecules/Stepper/SubTitle/SubStepIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ const Line = styled.span<LineProps>`

export const SubStepIndicator: FC = () => {
const { currentSubStep, steps, currentStep } = useStepper();
const amountOfSubSteps =
'subSteps' in steps[currentStep] ? steps[currentStep].subSteps.length : 0;

if (!amountOfSubSteps) return null;
const amountOfSubSteps = steps[currentStep].subSteps!.length;

return (
<Container $amountOfSubSteps={amountOfSubSteps}>
Expand Down
5 changes: 4 additions & 1 deletion src/molecules/Stepper/SubTitle/SubTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export const SubTitle: FC = () => {
'subSteps' in steps[currentStep] ? steps[currentStep].subSteps : [];
const currentSubStep = currentSubSteps?.at(subStepIndex);

if (!currentSubStep && steps[currentStep].title === undefined) {
if (
(!currentSubStep && steps[currentStep].title === undefined) ||
!currentSubSteps
) {
return null;
}

Expand Down
7 changes: 5 additions & 2 deletions src/providers/StepperProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface StepWithoutSubSteps {
label: string;
title?: string;
description?: string;
subSteps?: undefined;
}

type Step = StepWithSubSteps | StepWithoutSubSteps;
Expand Down Expand Up @@ -66,6 +67,7 @@ export const StepperProvider: FC<StepperProviderProps> = ({
const goToNextStep = () => {
if (
'subSteps' in steps[currentStep] &&
steps[currentStep].subSteps &&
currentSubStep < steps[currentStep].subSteps.length - 1
) {
setCurrentSubStep((prev) => prev + 1);
Expand All @@ -82,6 +84,7 @@ export const StepperProvider: FC<StepperProviderProps> = ({
if (
currentSubStep > 0 &&
'subSteps' in steps[currentStep] &&
steps[currentStep].subSteps &&
steps[currentStep].subSteps.length > 0
) {
setCurrentSubStep((prev) => prev - 1);
Expand All @@ -92,8 +95,8 @@ export const StepperProvider: FC<StepperProviderProps> = ({

setCurrentStep((prev) => prev - 1);

const previousStep = steps[currentStep - 1];
if ('subSteps' in previousStep) {
const previousStep = steps.at(currentStep - 1);
if (previousStep && 'subSteps' in previousStep && previousStep.subSteps) {
// Set substeps to the last substep of the previous step
setCurrentSubStep(previousStep.subSteps.length - 1);
}
Expand Down

0 comments on commit 7ac5380

Please sign in to comment.