|
1 | 1 | import { describe, expect, it } from 'vitest'; |
| 2 | +import { fireEvent, screen } from '@testing-library/react'; |
2 | 3 | import { render } from '../../../../../vitest.setup'; |
3 | 4 | import { ExpandableSection } from './ExpandableSection'; |
4 | 5 | import Typography from '@mui/material/Typography'; |
5 | 6 |
|
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 | +}); |
8 | 84 |
|
9 | 85 | describe('ExpandableSection snapshot', () => { |
10 | 86 | it('matches snapshot with default props', () => { |
11 | 87 | const { container } = render( |
12 | | - <ExpandableSection renderHeader={defaultHeader}> |
13 | | - {defaultChildren} |
14 | | - </ExpandableSection>, |
| 88 | + <ExpandableSection |
| 89 | + header={defaultHeader} |
| 90 | + items={defaultItems} |
| 91 | + renderItem={renderItem} |
| 92 | + />, |
15 | 93 | ); |
16 | 94 | expect(container).toMatchSnapshot(); |
17 | 95 | }); |
18 | 96 |
|
19 | 97 | it('matches snapshot when initially expanded', () => { |
20 | 98 | 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 | + />, |
24 | 105 | ); |
25 | 106 | expect(container).toMatchSnapshot(); |
26 | 107 | }); |
27 | 108 |
|
28 | 109 | it('matches snapshot when initially collapsed', () => { |
29 | 110 | 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 | + />, |
33 | 117 | ); |
34 | 118 | expect(container).toMatchSnapshot(); |
35 | 119 | }); |
36 | 120 |
|
37 | 121 | it('matches snapshot without expand icon', () => { |
38 | 122 | const { container } = render( |
39 | 123 | <ExpandableSection |
40 | | - renderHeader={defaultHeader} |
| 124 | + header={defaultHeader} |
| 125 | + items={defaultItems} |
| 126 | + renderItem={renderItem} |
41 | 127 | showExpandIcon={false} |
42 | 128 | isExpanded={true} |
43 | | - > |
44 | | - {defaultChildren} |
45 | | - </ExpandableSection>, |
| 129 | + />, |
46 | 130 | ); |
47 | 131 | expect(container).toMatchSnapshot(); |
48 | 132 | }); |
49 | 133 |
|
50 | 134 | it('matches snapshot with expanded end divider', () => { |
51 | 135 | const { container } = render( |
52 | 136 | <ExpandableSection |
53 | | - renderHeader={defaultHeader} |
| 137 | + header={defaultHeader} |
| 138 | + items={defaultItems} |
| 139 | + renderItem={renderItem} |
54 | 140 | isExpanded={true} |
55 | 141 | showExpandedEndDivider={true} |
56 | | - > |
57 | | - {defaultChildren} |
58 | | - </ExpandableSection>, |
| 142 | + />, |
59 | 143 | ); |
60 | 144 | expect(container).toMatchSnapshot(); |
61 | 145 | }); |
62 | 146 |
|
63 | 147 | it('matches snapshot when expansion is disabled', () => { |
64 | 148 | 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 | + />, |
68 | 183 | ); |
69 | 184 | expect(container).toMatchSnapshot(); |
70 | 185 | }); |
71 | 186 |
|
72 | | - it('matches snapshot with multiple children', () => { |
| 187 | + it('matches snapshot with multiple items', () => { |
73 | 188 | 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 | + />, |
79 | 195 | ); |
80 | 196 | expect(container).toMatchSnapshot(); |
81 | 197 | }); |
|
0 commit comments