@@ -233,4 +233,93 @@ describe("Product Review Statistics", () => {
233233 expect ( response . body . reviewRatingStatistics . count ) . toBe ( 1 ) ;
234234 expect ( response . body . reviewRatingStatistics . averageRating ) . toBe ( 4 ) ;
235235 } ) ;
236+
237+ test ( "reviews on other products are excluded from statistics" , async ( ) => {
238+ // Create another product
239+ const otherProductResponse = await supertest ( ctMock . app )
240+ . post ( "/dummy/products" )
241+ . send ( {
242+ name : { en : "Other Product" } ,
243+ slug : { en : "other-product" } ,
244+ productType : {
245+ typeId : "product-type" ,
246+ key : "dummy-product-type" ,
247+ } ,
248+ masterVariant : {
249+ sku : "other-sku" ,
250+ prices : [
251+ {
252+ value : {
253+ currencyCode : "EUR" ,
254+ centAmount : 2000 ,
255+ } ,
256+ } ,
257+ ] ,
258+ } ,
259+ } ) ;
260+ expect ( otherProductResponse . status ) . toBe ( 201 ) ;
261+ const otherProduct = otherProductResponse . body ;
262+
263+ // Create reviews for both products
264+ await supertest ( ctMock . app )
265+ . post ( "/dummy/reviews" )
266+ . send ( {
267+ authorName : "User A" ,
268+ title : "Review for first product" ,
269+ rating : 5 ,
270+ target : {
271+ typeId : "product" ,
272+ id : product . id ,
273+ } ,
274+ } ) ;
275+
276+ await supertest ( ctMock . app )
277+ . post ( "/dummy/reviews" )
278+ . send ( {
279+ authorName : "User B" ,
280+ title : "Review for second product" ,
281+ rating : 1 ,
282+ target : {
283+ typeId : "product" ,
284+ id : otherProduct . id ,
285+ } ,
286+ } ) ;
287+
288+ await supertest ( ctMock . app )
289+ . post ( "/dummy/reviews" )
290+ . send ( {
291+ authorName : "User C" ,
292+ title : "Another review for first product" ,
293+ rating : 3 ,
294+ target : {
295+ typeId : "product" ,
296+ id : product . id ,
297+ } ,
298+ } ) ;
299+
300+ // Check statistics for the first product - should only include its own reviews
301+ const response1 = await supertest ( ctMock . app ) . get ( `/dummy/products/${ product . id } ` ) ;
302+ expect ( response1 . status ) . toBe ( 200 ) ;
303+ expect ( response1 . body . reviewRatingStatistics ) . toBeDefined ( ) ;
304+ expect ( response1 . body . reviewRatingStatistics . count ) . toBe ( 2 ) ; // Only reviews for this product
305+ expect ( response1 . body . reviewRatingStatistics . averageRating ) . toBe ( 4 ) ; // (5 + 3) / 2 = 4
306+ expect ( response1 . body . reviewRatingStatistics . highestRating ) . toBe ( 5 ) ;
307+ expect ( response1 . body . reviewRatingStatistics . lowestRating ) . toBe ( 3 ) ;
308+ expect ( response1 . body . reviewRatingStatistics . ratingsDistribution ) . toEqual ( {
309+ "3" : 1 ,
310+ "5" : 1 ,
311+ } ) ;
312+
313+ // Check statistics for the second product - should only include its own review
314+ const response2 = await supertest ( ctMock . app ) . get ( `/dummy/products/${ otherProduct . id } ` ) ;
315+ expect ( response2 . status ) . toBe ( 200 ) ;
316+ expect ( response2 . body . reviewRatingStatistics ) . toBeDefined ( ) ;
317+ expect ( response2 . body . reviewRatingStatistics . count ) . toBe ( 1 ) ; // Only reviews for this product
318+ expect ( response2 . body . reviewRatingStatistics . averageRating ) . toBe ( 1 ) ;
319+ expect ( response2 . body . reviewRatingStatistics . highestRating ) . toBe ( 1 ) ;
320+ expect ( response2 . body . reviewRatingStatistics . lowestRating ) . toBe ( 1 ) ;
321+ expect ( response2 . body . reviewRatingStatistics . ratingsDistribution ) . toEqual ( {
322+ "1" : 1 ,
323+ } ) ;
324+ } ) ;
236325} ) ;
0 commit comments