Skip to content

Commit 44ca0af

Browse files
committed
chore: refactor
1 parent 2691777 commit 44ca0af

5 files changed

Lines changed: 539 additions & 180 deletions

File tree

src/components/core/sections/ExpandableSection/ExpandableSection.snapshot.spec.tsx

Lines changed: 144 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,197 @@
11
import { describe, expect, it } from 'vitest';
2+
import { fireEvent, screen } from '@testing-library/react';
23
import { render } from '../../../../../vitest.setup';
34
import { ExpandableSection } from './ExpandableSection';
45
import Typography from '@mui/material/Typography';
56

6-
const defaultHeader = () => <Typography>Section Title</Typography>;
7-
const defaultChildren = <Typography>Section Content</Typography>;
7+
const defaultHeader = <Typography>Section Title</Typography>;
8+
const defaultItems = ['Section Content'];
9+
const renderItem = (item: string) => <Typography>{item}</Typography>;
10+
11+
const getAccordionSummaryButton = () =>
12+
screen.getByRole('button', { name: /section title/i });
13+
14+
describe('ExpandableSection behavior', () => {
15+
it('does not toggle expansion when the header is clicked and shouldExpand is false', () => {
16+
render(
17+
<ExpandableSection
18+
header={defaultHeader}
19+
items={defaultItems}
20+
renderItem={renderItem}
21+
shouldExpand={false}
22+
/>,
23+
);
24+
25+
const summary = getAccordionSummaryButton();
26+
expect(summary).toHaveAttribute('aria-expanded', 'false');
27+
28+
fireEvent.click(summary);
29+
30+
expect(summary).toHaveAttribute('aria-expanded', 'false');
31+
});
32+
33+
it('toggles expansion when the header is clicked and shouldExpand is true', () => {
34+
render(
35+
<ExpandableSection
36+
header={defaultHeader}
37+
items={defaultItems}
38+
renderItem={renderItem}
39+
shouldExpand={true}
40+
/>,
41+
);
42+
43+
const summary = getAccordionSummaryButton();
44+
expect(summary).toHaveAttribute('aria-expanded', 'false');
45+
46+
fireEvent.click(summary);
47+
expect(summary).toHaveAttribute('aria-expanded', 'true');
48+
49+
fireEvent.click(summary);
50+
expect(summary).toHaveAttribute('aria-expanded', 'false');
51+
});
52+
53+
it('uses the default cursor on detail rows when isDetailsItemClickable is false', () => {
54+
render(
55+
<ExpandableSection
56+
header={defaultHeader}
57+
items={defaultItems}
58+
renderItem={renderItem}
59+
isExpanded={true}
60+
isDetailsItemClickable={false}
61+
/>,
62+
);
63+
64+
const detailRow = screen.getByText('Section Content').parentElement;
65+
expect(detailRow).toBeTruthy();
66+
expect(detailRow).toHaveStyle({ cursor: 'default' });
67+
});
68+
69+
it('uses the pointer cursor on detail rows when isDetailsItemClickable is true', () => {
70+
render(
71+
<ExpandableSection
72+
header={defaultHeader}
73+
items={defaultItems}
74+
renderItem={renderItem}
75+
isExpanded={true}
76+
isDetailsItemClickable={true}
77+
/>,
78+
);
79+
80+
const detailRow = screen.getByText('Section Content').parentElement;
81+
expect(detailRow).toHaveStyle({ cursor: 'pointer' });
82+
});
83+
});
884

985
describe('ExpandableSection snapshot', () => {
1086
it('matches snapshot with default props', () => {
1187
const { container } = render(
12-
<ExpandableSection renderHeader={defaultHeader}>
13-
{defaultChildren}
14-
</ExpandableSection>,
88+
<ExpandableSection
89+
header={defaultHeader}
90+
items={defaultItems}
91+
renderItem={renderItem}
92+
/>,
1593
);
1694
expect(container).toMatchSnapshot();
1795
});
1896

1997
it('matches snapshot when initially expanded', () => {
2098
const { container } = render(
21-
<ExpandableSection renderHeader={defaultHeader} isExpanded={true}>
22-
{defaultChildren}
23-
</ExpandableSection>,
99+
<ExpandableSection
100+
header={defaultHeader}
101+
items={defaultItems}
102+
renderItem={renderItem}
103+
isExpanded={true}
104+
/>,
24105
);
25106
expect(container).toMatchSnapshot();
26107
});
27108

28109
it('matches snapshot when initially collapsed', () => {
29110
const { container } = render(
30-
<ExpandableSection renderHeader={defaultHeader} isExpanded={false}>
31-
{defaultChildren}
32-
</ExpandableSection>,
111+
<ExpandableSection
112+
header={defaultHeader}
113+
items={defaultItems}
114+
renderItem={renderItem}
115+
isExpanded={false}
116+
/>,
33117
);
34118
expect(container).toMatchSnapshot();
35119
});
36120

37121
it('matches snapshot without expand icon', () => {
38122
const { container } = render(
39123
<ExpandableSection
40-
renderHeader={defaultHeader}
124+
header={defaultHeader}
125+
items={defaultItems}
126+
renderItem={renderItem}
41127
showExpandIcon={false}
42128
isExpanded={true}
43-
>
44-
{defaultChildren}
45-
</ExpandableSection>,
129+
/>,
46130
);
47131
expect(container).toMatchSnapshot();
48132
});
49133

50134
it('matches snapshot with expanded end divider', () => {
51135
const { container } = render(
52136
<ExpandableSection
53-
renderHeader={defaultHeader}
137+
header={defaultHeader}
138+
items={defaultItems}
139+
renderItem={renderItem}
54140
isExpanded={true}
55141
showExpandedEndDivider={true}
56-
>
57-
{defaultChildren}
58-
</ExpandableSection>,
142+
/>,
59143
);
60144
expect(container).toMatchSnapshot();
61145
});
62146

63147
it('matches snapshot when expansion is disabled', () => {
64148
const { container } = render(
65-
<ExpandableSection renderHeader={defaultHeader} shouldExpand={false}>
66-
{defaultChildren}
67-
</ExpandableSection>,
149+
<ExpandableSection
150+
header={defaultHeader}
151+
items={defaultItems}
152+
renderItem={renderItem}
153+
shouldExpand={false}
154+
/>,
155+
);
156+
expect(container).toMatchSnapshot();
157+
});
158+
159+
it('matches snapshot when detail rows are not clickable', () => {
160+
const { container } = render(
161+
<ExpandableSection
162+
header={defaultHeader}
163+
items={defaultItems}
164+
renderItem={renderItem}
165+
isExpanded={true}
166+
isDetailsItemClickable={false}
167+
/>,
168+
);
169+
expect(container).toMatchSnapshot();
170+
});
171+
172+
it('matches snapshot with custom sx, detailsListSx, and detailsItemSx', () => {
173+
const { container } = render(
174+
<ExpandableSection
175+
header={defaultHeader}
176+
items={defaultItems}
177+
renderItem={renderItem}
178+
isExpanded={true}
179+
sx={{ maxWidth: 320 }}
180+
detailsListSx={{ gap: 3 }}
181+
detailsItemSx={{ padding: 2 }}
182+
/>,
68183
);
69184
expect(container).toMatchSnapshot();
70185
});
71186

72-
it('matches snapshot with multiple children', () => {
187+
it('matches snapshot with multiple items', () => {
73188
const { container } = render(
74-
<ExpandableSection renderHeader={defaultHeader} isExpanded={true}>
75-
<Typography>Child One</Typography>
76-
<Typography>Child Two</Typography>
77-
<Typography>Child Three</Typography>
78-
</ExpandableSection>,
189+
<ExpandableSection
190+
header={defaultHeader}
191+
items={['Child One', 'Child Two', 'Child Three']}
192+
renderItem={renderItem}
193+
isExpanded={true}
194+
/>,
79195
);
80196
expect(container).toMatchSnapshot();
81197
});

0 commit comments

Comments
 (0)