Skip to content

Commit 4933bed

Browse files
committed
feat: add tests for Story component
1 parent 5947862 commit 4933bed

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
});

src/components/cards/Story.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface StoryProps {
2424
card: number | null;
2525
grid: number | null;
2626
};
27+
testId?: string;
2728
}
2829

2930
/**
@@ -42,7 +43,7 @@ interface StoryProps {
4243
* @returns {ReactElement}
4344
*/
4445

45-
export default function Story({ story, position, gridPosition, count, showingBubble, onShowBubble, onHideBubble }: StoryProps): ReactElement {
46+
export default function Story({ story, position, gridPosition, count, showingBubble, onShowBubble, onHideBubble, testId = "storyId" }: StoryProps): ReactElement {
4647
const [showBubble, setShowBubble] = useState(() => position === showingBubble.card && gridPosition === showingBubble.grid);
4748
const [height, setHeight] = useState(0);
4849
const bubbleRef = useRef<HTMLDivElement>(null);
@@ -108,6 +109,7 @@ export default function Story({ story, position, gridPosition, count, showingBub
108109
style={{
109110
transform: getPosition(),
110111
}}
112+
data-testid={testId}
111113
>
112114
<Image className="object-cover h-14 w-14 rounded-full" src={story.icon as string} alt="story icon" width={43} height={43} />
113115

0 commit comments

Comments
 (0)