11import 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
39import { StarRating } from "./StarRating" ;
410
511const 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 = {
1319export default meta ;
1420type Story = StoryObj < typeof meta > ;
1521
22+ const defaultArgs = { rating : 3.7 , productId : "1" } ;
23+
1624export 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} ;
0 commit comments