|
| 1 | +import React from "react"; |
| 2 | +import { render } from "@testing-library/react"; |
| 3 | +import { Accordion } from ".."; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | + |
| 6 | +describe("Accordion", () => { |
| 7 | + it("renders correctly", () => { |
| 8 | + const { getByText } = render( |
| 9 | + <Accordion title="Test Title">Test Content</Accordion>, |
| 10 | + ); |
| 11 | + |
| 12 | + expect(getByText("Test Title")).toBeInTheDocument(); |
| 13 | + }); |
| 14 | + |
| 15 | + it("opens and closes when the header is clicked", async () => { |
| 16 | + const { getByRole } = render( |
| 17 | + <Accordion title="Test Title">Test Content</Accordion>, |
| 18 | + ); |
| 19 | + const button = getByRole("button"); |
| 20 | + |
| 21 | + expect(button).toHaveAttribute("aria-expanded", "false"); |
| 22 | + |
| 23 | + await userEvent.click(button); |
| 24 | + expect(button).toHaveAttribute("aria-expanded", "true"); |
| 25 | + |
| 26 | + await userEvent.click(button); |
| 27 | + expect(button).toHaveAttribute("aria-expanded", "false"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("opens by default when defaultOpen is passed", async () => { |
| 31 | + const { getByRole } = render( |
| 32 | + <Accordion title="Test Title" defaultOpen> |
| 33 | + Test Content |
| 34 | + </Accordion>, |
| 35 | + ); |
| 36 | + const button = getByRole("button"); |
| 37 | + expect(button).toHaveAttribute("aria-expanded", "true"); |
| 38 | + |
| 39 | + await userEvent.click(button); |
| 40 | + expect(button).toHaveAttribute("aria-expanded", "false"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("opens by default when defaultOpen is passed", async () => { |
| 44 | + const { getByRole } = render( |
| 45 | + <Accordion title="Test Title" defaultOpen> |
| 46 | + Test Content |
| 47 | + </Accordion>, |
| 48 | + ); |
| 49 | + const button = getByRole("button"); |
| 50 | + expect(button).toHaveAttribute("aria-expanded", "true"); |
| 51 | + |
| 52 | + await userEvent.click(button); |
| 53 | + expect(button).toHaveAttribute("aria-expanded", "false"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("renders correctly with underline variant", () => { |
| 57 | + const { container } = render( |
| 58 | + <Accordion title="Test Title" variant="underline"> |
| 59 | + Test Content |
| 60 | + </Accordion>, |
| 61 | + ); |
| 62 | + |
| 63 | + expect(container.firstChild).toHaveClass("deriv-accordion--underline"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("renders correctly with bordered variant", () => { |
| 67 | + const { container } = render( |
| 68 | + <Accordion title="Test Title" variant="bordered"> |
| 69 | + Test Content |
| 70 | + </Accordion>, |
| 71 | + ); |
| 72 | + |
| 73 | + expect(container.firstChild).toHaveClass("deriv-accordion--bordered"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("renders correctly with shadow variant", () => { |
| 77 | + const { container } = render( |
| 78 | + <Accordion title="Test Title" variant="shadow"> |
| 79 | + Test Content |
| 80 | + </Accordion>, |
| 81 | + ); |
| 82 | + |
| 83 | + expect(container.firstChild).toHaveClass("deriv-accordion--shadow"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("renders correctly when isCompact is true", () => { |
| 87 | + const { container } = render( |
| 88 | + <Accordion title="Test Title" isCompact={true}> |
| 89 | + Test Content |
| 90 | + </Accordion>, |
| 91 | + ); |
| 92 | + |
| 93 | + expect(container.firstChild).toHaveClass("deriv-accordion--compact"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("renders correctly when isCompact is false", () => { |
| 97 | + const { container } = render( |
| 98 | + <Accordion title="Test Title" isCompact={false}> |
| 99 | + Test Content |
| 100 | + </Accordion>, |
| 101 | + ); |
| 102 | + |
| 103 | + expect(container.firstChild).not.toHaveClass( |
| 104 | + "deriv-accordion--compact", |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments