|
1 | 1 | import "@testing-library/jest-dom/extend-expect"
|
2 |
| -import { render } from "@testing-library/react" |
| 2 | +import { fireEvent, render } from "@testing-library/react" |
3 | 3 | import React from "react"
|
4 | 4 |
|
5 | 5 | import { IconChevronDown } from "@tabler/icons-react"
|
6 |
| -import { Accordion } from "." |
| 6 | +import { Accordion, AccordionDataShape } from "." |
7 | 7 |
|
8 | 8 | describe("Accordion component", () => {
|
9 | 9 | it("should render the accordion container with the correct styles and classes", () => {
|
@@ -90,4 +90,79 @@ describe("Accordion component", () => {
|
90 | 90 |
|
91 | 91 | expect(queryByTestId("accordion-item-header")).not.toBeInTheDocument()
|
92 | 92 | })
|
| 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 | + }) |
93 | 168 | })
|
0 commit comments