|
| 1 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 2 | +import "@testing-library/jest-dom"; |
| 3 | +import Story from "@/components/cards/Story"; |
| 4 | + |
| 5 | +jest.mock("next/image", () => ({ |
| 6 | + __esModule: true, |
| 7 | + default: (props: any) => <img {...props} />, |
| 8 | +})); |
| 9 | + |
| 10 | +describe("Story Component", () => { |
| 11 | + const mockStory = { |
| 12 | + content: "Test story content", |
| 13 | + icon: "/test-icon.png", |
| 14 | + }; |
| 15 | + |
| 16 | + const defaultProps = { |
| 17 | + story: mockStory, |
| 18 | + position: 0, |
| 19 | + gridPosition: 0, |
| 20 | + count: 5, |
| 21 | + showingBubble: { card: null, grid: null }, |
| 22 | + onShowBubble: jest.fn(), |
| 23 | + onHideBubble: jest.fn(), |
| 24 | + }; |
| 25 | + |
| 26 | + it("renders the story icon", () => { |
| 27 | + render(<Story {...defaultProps} />); |
| 28 | + expect(screen.getByTestId("storyId")).toBeInTheDocument(); |
| 29 | + const icon = screen.getByAltText("story icon"); |
| 30 | + expect(icon).toBeInTheDocument(); |
| 31 | + expect(icon).toHaveAttribute("src", "/test-icon.png"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("shows the bubble when clicked", () => { |
| 35 | + render(<Story {...defaultProps} />); |
| 36 | + fireEvent.click(screen.getByTestId("storyId")); |
| 37 | + expect(screen.getByText("Test story content")).toBeInTheDocument(); |
| 38 | + expect(defaultProps.onShowBubble).toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("hides the bubble when clicked again", () => { |
| 42 | + render(<Story {...defaultProps} />); |
| 43 | + fireEvent.click(screen.getByTestId("storyId")); |
| 44 | + fireEvent.click(screen.getByTestId("storyId")); |
| 45 | + expect(screen.queryByText("Test story content")).not.toBeInTheDocument(); |
| 46 | + expect(defaultProps.onHideBubble).toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("shows the bubble initially when showingBubble matches position and gridPosition", () => { |
| 50 | + const props = { |
| 51 | + ...defaultProps, |
| 52 | + showingBubble: { card: 0, grid: 0 }, |
| 53 | + }; |
| 54 | + render(<Story {...props} />); |
| 55 | + expect(screen.getByText("Test story content")).toBeInTheDocument(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments