File tree 2 files changed +58
-3
lines changed
2 files changed +58
-3
lines changed Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+ import { render } from 'react-testing-library' ;
3
+ import Accordion from './Accordion' ;
4
+ import AccordionItem from './AccordionItem' ;
5
+ import AccordionItemState from './AccordionItemState' ;
6
+
7
+ enum UUIDS {
8
+ FOO = 'FOO' ,
9
+ BAR = 'BAR' ,
10
+ }
11
+
12
+ describe ( 'ItemContext' , ( ) => {
13
+ describe ( 'render prop' , ( ) => {
14
+ it ( 'renders' , ( ) => {
15
+ const { getByText } = render (
16
+ < Accordion >
17
+ < AccordionItem >
18
+ < AccordionItemState >
19
+ { ( ) : string => 'Hello World' }
20
+ </ AccordionItemState >
21
+ </ AccordionItem >
22
+ </ Accordion > ,
23
+ ) ;
24
+
25
+ expect ( getByText ( 'Hello World' ) ) . toBeTruthy ( ) ;
26
+ } ) ;
27
+
28
+ it ( 'invokes the render prop with the contextual item’s expanded state' , ( ) => {
29
+ const { getByTestId } = render (
30
+ < Accordion preExpanded = { [ UUIDS . FOO ] } >
31
+ < AccordionItem uuid = { UUIDS . FOO } >
32
+ < AccordionItemState >
33
+ { ( expanded : boolean ) : JSX . Element => (
34
+ < div data-testid = { UUIDS . FOO } >
35
+ { expanded && 'expanded' }
36
+ </ div >
37
+ ) }
38
+ </ AccordionItemState >
39
+ </ AccordionItem >
40
+ < AccordionItem uuid = { UUIDS . BAR } >
41
+ < AccordionItemState >
42
+ { ( expanded : boolean ) : JSX . Element => (
43
+ < div data-testid = { UUIDS . BAR } >
44
+ { expanded && 'expanded' }
45
+ </ div >
46
+ ) }
47
+ </ AccordionItemState >
48
+ </ AccordionItem >
49
+ </ Accordion > ,
50
+ ) ;
51
+
52
+ expect ( getByTestId ( UUIDS . FOO ) . textContent ) . toEqual ( 'expanded' ) ;
53
+ expect ( getByTestId ( UUIDS . BAR ) . textContent ) . toEqual ( '' ) ;
54
+ } ) ;
55
+ } ) ;
56
+ } ) ;
Original file line number Diff line number Diff line change @@ -3,11 +3,10 @@ import { DivAttributes } from '../helpers/types';
3
3
import { Consumer as ItemConsumer , ItemContext } from './ItemContext' ;
4
4
5
5
type Props = Pick < DivAttributes , Exclude < keyof DivAttributes , 'children' > > & {
6
- expanded : boolean ;
7
- children ( expanded : boolean ) : JSX . Element ;
6
+ children ( expanded ?: boolean ) : React . ReactNode ;
8
7
} ;
9
8
10
- export default class AccordionItemStateWrapper extends React . Component < Props > {
9
+ export default class AccordionItemState extends React . Component < Props > {
11
10
renderChildren = ( itemContext : ItemContext ) : JSX . Element => {
12
11
const { expanded } = itemContext ;
13
12
You can’t perform that action at this time.
0 commit comments