Skip to content

Commit e436e22

Browse files
committed
test: 🧪 add test for accordion toggle
1 parent 8df8485 commit e436e22

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

src/components/molecules/Accordion/Accordion.test.tsx

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import "@testing-library/jest-dom/extend-expect"
2-
import { render } from "@testing-library/react"
2+
import { fireEvent, render } from "@testing-library/react"
33
import React from "react"
44

55
import { IconChevronDown } from "@tabler/icons-react"
6-
import { Accordion } from "."
6+
import { Accordion, AccordionDataShape } from "."
77

88
describe("Accordion component", () => {
99
it("should render the accordion container with the correct styles and classes", () => {
@@ -90,4 +90,79 @@ describe("Accordion component", () => {
9090

9191
expect(queryByTestId("accordion-item-header")).not.toBeInTheDocument()
9292
})
93+
94+
it("should toggle the accordion item content only if it's not disabled", () => {
95+
// Arrange
96+
const data: AccordionDataShape[] = [
97+
{ label: "Item 1", children: "Content 1" },
98+
{ label: "Item 2", children: "Content 2", disabled: true },
99+
]
100+
const collapseIcon = <IconChevronDown />
101+
const color = "warm"
102+
const size = "md"
103+
const multiActive = false
104+
105+
// Act
106+
const { getAllByTestId, getByText, queryByText } = render(
107+
<Accordion
108+
data={data}
109+
collapseIcon={collapseIcon}
110+
color={color}
111+
size={size}
112+
multiActive={multiActive}
113+
/>
114+
)
115+
116+
// Assert
117+
const itemHeaders = getAllByTestId("accordion-item-header")
118+
fireEvent.click(itemHeaders[0])
119+
expect(getByText("Content 1")).toBeInTheDocument()
120+
121+
fireEvent.click(itemHeaders[1])
122+
expect(queryByText("Content 2")).not.toBeInTheDocument()
123+
})
124+
125+
// Toggles the accordion item content when clicking on the header.
126+
it("should toggle accordion item content when clicking on the header", () => {
127+
// Arrange
128+
const data = [
129+
{ label: "Item 1", children: "Content 1" },
130+
{ label: "Item 2", children: "Content 2" },
131+
]
132+
const collapseIcon = <IconChevronDown />
133+
const color = "warm"
134+
const size = "md"
135+
const multiActive = false
136+
137+
// Act
138+
const { getAllByTestId } = render(
139+
<Accordion
140+
data={data}
141+
collapseIcon={collapseIcon}
142+
color={color}
143+
size={size}
144+
multiActive={multiActive}
145+
/>
146+
)
147+
148+
// Assert
149+
const itemHeaders = getAllByTestId("accordion-item-header")
150+
const contentContainers = getAllByTestId("accordion-item-content")
151+
152+
expect(itemHeaders).toHaveLength(2)
153+
expect(contentContainers).toHaveLength(2)
154+
155+
// Click first item, Assert the first item visible
156+
fireEvent.click(itemHeaders[0])
157+
expect(contentContainers[0]).toBeVisible()
158+
159+
// Click second item, Assert the first item is hidden
160+
fireEvent.click(itemHeaders[1])
161+
expect(contentContainers[0]).toHaveClass("max-h-0")
162+
163+
// Click first item header again, assert first item visible & second item hidden
164+
fireEvent.click(itemHeaders[0])
165+
expect(contentContainers[1]).toHaveClass("max-h-0")
166+
expect(contentContainers[0]).not.toHaveClass("max-h-0")
167+
})
93168
})

src/components/molecules/Accordion/Accordion.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const Accordion = ({
5151
onClick={() => {
5252
if (!item.disabled) togglePanel(itemKey)
5353
}}
54-
data-testId="accordion-item-header"
54+
data-testid="accordion-item-header"
5555
>
5656
<span className={styles.haderLabel}>{item.label}</span>
5757
{collapseIcon ? (
@@ -68,6 +68,7 @@ const Accordion = ({
6868
activeClass.contentContainer,
6969
styles.contentContainer,
7070
])}
71+
data-testid="accordion-item-content"
7172
>
7273
{item.children}
7374
</div>

0 commit comments

Comments
 (0)