|
| 1 | +import { customRender, screen, fireEvent } from "@/jest/utils/testUtils"; |
| 2 | + |
| 3 | +import ReadMore from "./readMore"; |
| 4 | + |
| 5 | +describe("ReadMore Component", () => { |
| 6 | + const text = |
| 7 | + // eslint-disable-next-line max-len |
| 8 | + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum accumsan arcu in nunc pharetra, ac consectetur nulla bibendum."; |
| 9 | + |
| 10 | + it("renders the initial text correctly", () => { |
| 11 | + customRender(<ReadMore text={text} amountOfWords={5} showMoreText="Read more" showLessText="Read less" />); |
| 12 | + |
| 13 | + expect(screen.getByTestId("hidden-text")).not.toHaveTextContent(/Lorem ipsum dolor sit amet/); |
| 14 | + expect(screen.getByTestId("hidden-text")).toHaveTextContent(/consectetur adipiscing elit/); |
| 15 | + expect(screen.getByTestId("hidden-text")).toHaveClass("hidden"); |
| 16 | + const showMoreButton = screen.getByTestId("show-more-button"); |
| 17 | + expect(showMoreButton).toHaveTextContent("Read more"); |
| 18 | + expect(showMoreButton).not.toHaveTextContent("Read less"); |
| 19 | + }); |
| 20 | + |
| 21 | + it("expands and collapses text when clicking 'Read more' and 'Read less'", () => { |
| 22 | + customRender(<ReadMore text={text} amountOfWords={5} showMoreText="Read more" showLessText="Read less" />); |
| 23 | + |
| 24 | + const showMoreButton = screen.getByText("Read more"); |
| 25 | + expect(showMoreButton).toBeInTheDocument(); |
| 26 | + |
| 27 | + fireEvent.click(showMoreButton); |
| 28 | + expect(screen.getByTestId("hidden-text")).not.toHaveClass("hidden"); |
| 29 | + expect(screen.getByText("Read less")).toBeInTheDocument(); |
| 30 | + |
| 31 | + fireEvent.click(screen.getByText("Read less")); |
| 32 | + expect(screen.getByTestId("hidden-text")).toHaveClass("hidden"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("toggles expansion with keyboard (Enter and Space keys)", () => { |
| 36 | + customRender(<ReadMore text={text} amountOfWords={5} showMoreText="Read more" showLessText="Read less" />); |
| 37 | + |
| 38 | + const showMoreButton = screen.getByTestId("show-more-button"); |
| 39 | + |
| 40 | + fireEvent.keyDown(showMoreButton, { key: "Enter" }); |
| 41 | + expect(screen.getByTestId("hidden-text")).not.toHaveClass("hidden"); |
| 42 | + |
| 43 | + fireEvent.keyDown(showMoreButton, { key: " " }); |
| 44 | + expect(screen.getByTestId("hidden-text")).toHaveClass("hidden"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("does not show 'Read more' button if text is shorter than amountOfWords", () => { |
| 48 | + customRender(<ReadMore text="Short text" amountOfWords={10} showMoreText="Read more" showLessText="Read less" />); |
| 49 | + |
| 50 | + expect(screen.getByText("Short text")).toBeInTheDocument(); |
| 51 | + expect(screen.queryByText("Read more")).not.toBeInTheDocument(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should match snapshot", () => { |
| 55 | + const { asFragment } = customRender( |
| 56 | + <ReadMore amountOfWords={10} text={text} showMoreText="Read more" showLessText="Read less" /> |
| 57 | + ); |
| 58 | + expect(asFragment()).toMatchSnapshot(); |
| 59 | + }); |
| 60 | +}); |
0 commit comments