Skip to content

Commit 8df8485

Browse files
committed
test: 🧪 add initial test cases for Accordion component
1 parent 7396cd3 commit 8df8485

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed
Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,93 @@
1-
21
import "@testing-library/jest-dom/extend-expect"
32
import { render } from "@testing-library/react"
43
import React from "react"
54

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

88
describe("Accordion component", () => {
9-
test("renders properly", () => {})
9+
it("should render the accordion container with the correct styles and classes", () => {
10+
// Arrange
11+
const data = [
12+
{ label: "Item 1", children: "Content 1" },
13+
{ label: "Item 2", children: "Content 2" },
14+
]
15+
const collapseIcon = <IconChevronDown />
16+
const color = "warm"
17+
const size = "md"
18+
const multiActive = false
19+
20+
// Act
21+
const { getByTestId } = render(
22+
<Accordion
23+
data={data}
24+
collapseIcon={collapseIcon}
25+
color={color}
26+
size={size}
27+
multiActive={multiActive}
28+
/>
29+
)
30+
31+
// Assert
32+
const container = getByTestId("accordion-container")
33+
expect(container).toBeInTheDocument()
34+
expect(container).toHaveClass("mx-auto", "flex", "flex-col", "gap-2")
35+
expect(container).toHaveClass("w-[600px]")
36+
})
37+
38+
it("should render each item in the data array with the correct styles and classes", () => {
39+
const data = [
40+
{ label: "Item 1", children: "Content 1" },
41+
{ label: "Item 2", children: "Content 2" },
42+
]
43+
const collapseIcon = <IconChevronDown />
44+
const color = "warm"
45+
const size = "md"
46+
const multiActive = false
47+
48+
const { getAllByTestId } = render(
49+
<Accordion
50+
data={data}
51+
collapseIcon={collapseIcon}
52+
color={color}
53+
size={size}
54+
multiActive={multiActive}
55+
/>
56+
)
57+
58+
const itemHeaders = getAllByTestId("accordion-item-header")
59+
expect(itemHeaders).toHaveLength(2)
60+
expect(itemHeaders[0]).toHaveClass(
61+
"p-3",
62+
"flex",
63+
"items-center",
64+
"justify-between"
65+
)
66+
expect(itemHeaders[1]).toHaveClass(
67+
"p-3",
68+
"flex",
69+
"items-center",
70+
"justify-between"
71+
)
72+
})
73+
74+
it("should handle an empty data array", () => {
75+
const data: any[] = []
76+
const collapseIcon = <IconChevronDown />
77+
const color = "warm"
78+
const size = "md"
79+
const multiActive = false
80+
81+
const { queryByTestId } = render(
82+
<Accordion
83+
data={data}
84+
collapseIcon={collapseIcon}
85+
color={color}
86+
size={size}
87+
multiActive={multiActive}
88+
/>
89+
)
90+
91+
expect(queryByTestId("accordion-item-header")).not.toBeInTheDocument()
92+
})
1093
})

src/components/molecules/Accordion/Accordion.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Accordion = ({
2525
}
2626

2727
return (
28-
<div className={styles.container}>
28+
<div className={styles.container} data-testId="accordion-container">
2929
{data.map((item: AccordionDataShape, index: number) => {
3030
const itemKey = String(index)
3131
const showItem = activePanel.includes(itemKey)
@@ -51,6 +51,7 @@ const Accordion = ({
5151
onClick={() => {
5252
if (!item.disabled) togglePanel(itemKey)
5353
}}
54+
data-testId="accordion-item-header"
5455
>
5556
<span className={styles.haderLabel}>{item.label}</span>
5657
{collapseIcon ? (
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { default } from "./Accordion"
1+
export { default as Accordion } from "./Accordion"
22
export { AccordionDataShape } from "./utils/types"

0 commit comments

Comments
 (0)