Skip to content

Commit 5e53707

Browse files
committed
Add unit tests for AccordionItemState component
1 parent 79c5d11 commit 5e53707

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
});

src/components/AccordionItemState.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { DivAttributes } from '../helpers/types';
33
import { Consumer as ItemConsumer, ItemContext } from './ItemContext';
44

55
type Props = Pick<DivAttributes, Exclude<keyof DivAttributes, 'children'>> & {
6-
expanded: boolean;
7-
children(expanded: boolean): JSX.Element;
6+
children(expanded?: boolean): React.ReactNode;
87
};
98

10-
export default class AccordionItemStateWrapper extends React.Component<Props> {
9+
export default class AccordionItemState extends React.Component<Props> {
1110
renderChildren = (itemContext: ItemContext): JSX.Element => {
1211
const { expanded } = itemContext;
1312

0 commit comments

Comments
 (0)