File tree Expand file tree Collapse file tree 6 files changed +78
-5
lines changed Expand file tree Collapse file tree 6 files changed +78
-5
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ export function Wizard({
54
54
return (
55
55
< WizardContext . Provider
56
56
value = { {
57
+ isWizardContext : true ,
57
58
activeStep,
58
59
updateStep,
59
60
} }
Original file line number Diff line number Diff line change @@ -3,11 +3,13 @@ import { createContext, useContext } from 'react';
3
3
import { Direction } from '@leafygreen-ui/descendants' ;
4
4
5
5
interface WizardContextData {
6
+ isWizardContext : boolean ;
6
7
activeStep : number ;
7
8
updateStep : ( direction : Direction ) => void ;
8
9
}
9
10
10
11
export const WizardContext = createContext < WizardContextData > ( {
12
+ isWizardContext : false ,
11
13
activeStep : 0 ,
12
14
updateStep : ( ) => { } ,
13
15
} ) ;
Original file line number Diff line number Diff line change @@ -2,7 +2,33 @@ import React from 'react';
2
2
import { render } from '@testing-library/react' ;
3
3
4
4
import { WizardFooter } from '.' ;
5
+ import { Wizard } from '../Wizard/Wizard' ;
5
6
6
7
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
+ } ) ;
8
34
} ) ;
Original file line number Diff line number Diff line change @@ -7,14 +7,15 @@ import { useWizardContext } from '../WizardContext/WizardContext';
7
7
8
8
import { WizardFooterProps } from './WizardFooter.types' ;
9
9
import { WizardSubComponentProperties } from '../constants' ;
10
+ import { consoleOnce } from '@leafygreen-ui/lib' ;
10
11
11
12
export const WizardFooter = ( {
12
13
backButtonProps,
13
14
cancelButtonProps,
14
15
primaryButtonProps,
15
16
...rest
16
17
} : WizardFooterProps ) => {
17
- const { activeStep, updateStep } = useWizardContext ( ) ;
18
+ const { isWizardContext , activeStep, updateStep } = useWizardContext ( ) ;
18
19
19
20
const handleBackButtonClick : MouseEventHandler < HTMLButtonElement > = e => {
20
21
updateStep ( Direction . Prev ) ;
@@ -26,6 +27,13 @@ export const WizardFooter = ({
26
27
primaryButtonProps . onClick ?.( e ) ;
27
28
} ;
28
29
30
+ if ( ! isWizardContext ) {
31
+ consoleOnce . error (
32
+ 'Wizard.Footer component must be used within a Wizard context.' ,
33
+ ) ;
34
+ return null ;
35
+ }
36
+
29
37
return (
30
38
< FormFooter
31
39
{ ...rest }
Original file line number Diff line number Diff line change @@ -2,7 +2,27 @@ import React from 'react';
2
2
import { render } from '@testing-library/react' ;
3
3
4
4
import { WizardStep } from '.' ;
5
+ import { Wizard } from '../Wizard/Wizard' ;
5
6
6
7
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
+ } ) ;
8
28
} ) ;
Original file line number Diff line number Diff line change @@ -6,10 +6,26 @@ import { stepStyles } from './WizardStep.styles';
6
6
import { WizardStepProps } from './WizardStep.types' ;
7
7
import { WizardSubComponentProperties } from '../constants' ;
8
8
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
+ }
9
26
10
- export function WizardStep ( { title, description, children } : WizardStepProps ) {
11
27
return (
12
- < div className = { stepStyles } >
28
+ < div className = { stepStyles } { ... rest } >
13
29
< TextNode as = { H3 } > { title } </ TextNode >
14
30
{ description && < TextNode as = { Description } > { description } </ TextNode > }
15
31
< div > { children } </ div >
You can’t perform that action at this time.
0 commit comments