Skip to content

Commit 66bf36b

Browse files
committed
adds wizard context assertions
1 parent 805ba2f commit 66bf36b

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-5
lines changed

packages/wizard/src/Wizard/Wizard.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function Wizard({
5454
return (
5555
<WizardContext.Provider
5656
value={{
57+
isWizardContext: true,
5758
activeStep,
5859
updateStep,
5960
}}

packages/wizard/src/WizardContext/WizardContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { createContext, useContext } from 'react';
33
import { Direction } from '@leafygreen-ui/descendants';
44

55
interface WizardContextData {
6+
isWizardContext: boolean;
67
activeStep: number;
78
updateStep: (direction: Direction) => void;
89
}
910

1011
export const WizardContext = createContext<WizardContextData>({
12+
isWizardContext: false,
1113
activeStep: 0,
1214
updateStep: () => {},
1315
});

packages/wizard/src/WizardFooter/WizardFooter.spec.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,33 @@ import React from 'react';
22
import { render } from '@testing-library/react';
33

44
import { WizardFooter } from '.';
5+
import { Wizard } from '../Wizard/Wizard';
56

67
describe('packages/wizard-footer', () => {
7-
test('condition', () => {});
8+
test('does not render outside WizardContext', () => {
9+
const { container } = render(
10+
<WizardFooter
11+
data-testid="footer"
12+
primaryButtonProps={{ children: 'Next' }}
13+
>
14+
Content
15+
</WizardFooter>,
16+
);
17+
18+
expect(container.firstChild).toBeNull();
19+
});
20+
test('renders in WizardContext', () => {
21+
const { getByTestId } = render(
22+
<Wizard>
23+
<WizardFooter
24+
data-testid="footer"
25+
primaryButtonProps={{ children: 'Next' }}
26+
>
27+
Content
28+
</WizardFooter>
29+
</Wizard>,
30+
);
31+
32+
expect(getByTestId('footer')).toBeInTheDocument();
33+
});
834
});

packages/wizard/src/WizardFooter/WizardFooter.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import { useWizardContext } from '../WizardContext/WizardContext';
77

88
import { WizardFooterProps } from './WizardFooter.types';
99
import { WizardSubComponentProperties } from '../constants';
10+
import { consoleOnce } from '@leafygreen-ui/lib';
1011

1112
export const WizardFooter = ({
1213
backButtonProps,
1314
cancelButtonProps,
1415
primaryButtonProps,
1516
...rest
1617
}: WizardFooterProps) => {
17-
const { activeStep, updateStep } = useWizardContext();
18+
const { isWizardContext, activeStep, updateStep } = useWizardContext();
1819

1920
const handleBackButtonClick: MouseEventHandler<HTMLButtonElement> = e => {
2021
updateStep(Direction.Prev);
@@ -26,6 +27,13 @@ export const WizardFooter = ({
2627
primaryButtonProps.onClick?.(e);
2728
};
2829

30+
if (!isWizardContext) {
31+
consoleOnce.error(
32+
'Wizard.Footer component must be used within a Wizard context.',
33+
);
34+
return null;
35+
}
36+
2937
return (
3038
<FormFooter
3139
{...rest}

packages/wizard/src/WizardStep/WizardStep.spec.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,27 @@ import React from 'react';
22
import { render } from '@testing-library/react';
33

44
import { WizardStep } from '.';
5+
import { Wizard } from '../Wizard/Wizard';
56

67
describe('packages/wizard-step', () => {
7-
test('condition', () => {});
8+
test('does not render outside WizardContext', () => {
9+
const { container } = render(
10+
<WizardStep data-testid="step-1" title="Step">
11+
Content
12+
</WizardStep>,
13+
);
14+
15+
expect(container.firstChild).toBeNull();
16+
});
17+
test('renders in WizardContext', () => {
18+
const { getByTestId } = render(
19+
<Wizard>
20+
<WizardStep data-testid="step-1" title="Step">
21+
Content
22+
</WizardStep>
23+
</Wizard>,
24+
);
25+
26+
expect(getByTestId('step-1')).toBeInTheDocument();
27+
});
828
});

packages/wizard/src/WizardStep/WizardStep.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,26 @@ import { stepStyles } from './WizardStep.styles';
66
import { WizardStepProps } from './WizardStep.types';
77
import { WizardSubComponentProperties } from '../constants';
88
import { TextNode } from './TextNode';
9+
import { useWizardContext } from '../WizardContext/WizardContext';
10+
import { consoleOnce } from '@leafygreen-ui/lib';
11+
12+
export function WizardStep({
13+
title,
14+
description,
15+
children,
16+
...rest
17+
}: WizardStepProps) {
18+
const { isWizardContext } = useWizardContext();
19+
20+
if (!isWizardContext) {
21+
consoleOnce.error(
22+
'Wizard.Step component must be used within a Wizard context.',
23+
);
24+
return null;
25+
}
926

10-
export function WizardStep({ title, description, children }: WizardStepProps) {
1127
return (
12-
<div className={stepStyles}>
28+
<div className={stepStyles} {...rest}>
1329
<TextNode as={H3}>{title}</TextNode>
1430
{description && <TextNode as={Description}>{description}</TextNode>}
1531
<div>{children}</div>

0 commit comments

Comments
 (0)