Skip to content

Commit a5080db

Browse files
committed
chore: add tests for StarRating component
1 parent 6b12e7e commit a5080db

4 files changed

Lines changed: 91 additions & 5 deletions

File tree

src/features/marketing/rating/components/ProductRating.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getMarketingProductHandler } from "@/test-lib/handlers/get-marketing-pr
55
import { ProductRating } from "./ProductRating";
66

77
const meta = {
8-
title: "modules/Marketing/ProductRating",
8+
title: "modules/Marketing/Rating/ProductRating",
99
component: ProductRating,
1010
parameters: {
1111
layout: "centered",
Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import type { Meta, StoryObj } from "@storybook/react-vite";
2+
import { HttpResponse } from "msw";
3+
import { userEvent, screen, expect } from "storybook/test";
4+
5+
import { patchRateProductHandler } from "@/test-lib/handlers/patch-rate-product-handler";
6+
import { withAuth } from "@/test-lib/storybook/with-auth";
7+
import { withoutAuth } from "@/test-lib/storybook/without-auth";
28

39
import { StarRating } from "./StarRating";
410

511
const meta = {
6-
title: "modules/Marketing/StarRating",
12+
title: "modules/Marketing/Rating/StarRating",
713
component: StarRating,
814
parameters: {
915
layout: "centered",
@@ -13,9 +19,76 @@ const meta = {
1319
export default meta;
1420
type Story = StoryObj<typeof meta>;
1521

22+
const defaultArgs = { rating: 3.7, productId: "1" };
23+
1624
export const Default: Story = {
17-
args: {
18-
rating: 3.7,
19-
productId: "1",
25+
args: defaultArgs,
26+
};
27+
28+
export const SubmitRating: Story = {
29+
args: defaultArgs,
30+
decorators: [withAuth],
31+
parameters: {
32+
msw: { handlers: [patchRateProductHandler()] },
33+
},
34+
play: async ({ canvasElement, step }) => {
35+
await step("Hover over the 4th star", async () => {
36+
const stars = canvasElement.querySelectorAll("svg");
37+
await userEvent.hover(stars[3]);
38+
});
39+
40+
await step("Click the 4th star to submit rating", async () => {
41+
const stars = canvasElement.querySelectorAll("svg");
42+
await userEvent.click(stars[3]);
43+
});
44+
45+
await step("Success notification is shown", async () => {
46+
await expect(
47+
await screen.findByText("Thank you! Your rating has been submitted.")
48+
).toBeInTheDocument();
49+
});
50+
},
51+
};
52+
53+
export const SubmitRatingFailure: Story = {
54+
args: defaultArgs,
55+
decorators: [withAuth],
56+
parameters: {
57+
msw: {
58+
handlers: [
59+
patchRateProductHandler(() => HttpResponse.json({}, { status: 500 })),
60+
],
61+
},
62+
},
63+
play: async ({ canvasElement, step }) => {
64+
await step("Click the 3rd star", async () => {
65+
const stars = canvasElement.querySelectorAll("svg");
66+
await userEvent.click(stars[2]);
67+
});
68+
69+
await step("Error notification is shown", async () => {
70+
await expect(
71+
await screen.findByText(
72+
"Something went wrong with submitting your rating. Please try again or contact us."
73+
)
74+
).toBeInTheDocument();
75+
});
76+
},
77+
};
78+
79+
export const RatingWhileUnauthenticated: Story = {
80+
args: defaultArgs,
81+
decorators: [withoutAuth],
82+
play: async ({ canvasElement, step }) => {
83+
await step("Click a star while not logged in", async () => {
84+
const stars = canvasElement.querySelectorAll("svg");
85+
await userEvent.click(stars[2]);
86+
});
87+
88+
await step("Not-authenticated notification is shown", async () => {
89+
await expect(
90+
await screen.findByText("You have to log in to rate the product")
91+
).toBeInTheDocument();
92+
});
2093
},
2194
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { http, HttpResponse } from "msw";
2+
3+
import { host } from "@/lib/http";
4+
5+
import type { PatchResolver } from "./resolvers";
6+
7+
export const patchRateProductHandler = (resolver?: PatchResolver) =>
8+
http.patch(`${host}/marketing/products/:productId/rate`, (req) => {
9+
if (resolver) return resolver(req);
10+
11+
return HttpResponse.json({ id: "1", rating: { rate: 4, count: 100 } });
12+
});

src/test-lib/handlers/resolvers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export type GetResolver = Parameters<typeof http.get>[1];
44
export type DeleteResolver = Parameters<typeof http.delete>[1];
55
export type PutResolver = Parameters<typeof http.put>[1];
66
export type PostResolver = Parameters<typeof http.post>[1];
7+
export type PatchResolver = Parameters<typeof http.patch>[1];

0 commit comments

Comments
 (0)