Skip to content

Commit 09789c9

Browse files
Move review statistics to lib directory and add test for product separation
Co-authored-by: mvantellingen <[email protected]>
1 parent 154b978 commit 09789c9

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

src/services/product-review-statistics.test.ts renamed to src/lib/product-review-statistics.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});

src/product-projection-search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
resolveVariantValue,
2424
} from "./lib/projectionSearchFilter";
2525
import { applyPriceSelector } from "./priceSelector";
26-
import { ReviewStatisticsService } from "./services/review-statistics";
26+
import { ReviewStatisticsService } from "./lib/review-statistics";
2727
import type { AbstractStorage } from "./storage";
2828
import type { Writable } from "./types";
2929

src/repositories/product/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { Config } from "~src/config";
1414
import { CommercetoolsError } from "~src/exceptions";
1515
import { getBaseResourceProperties } from "~src/helpers";
1616
import { ProductSearch } from "~src/product-search";
17-
import { ReviewStatisticsService } from "~src/services/review-statistics";
17+
import { ReviewStatisticsService } from "~src/lib/review-statistics";
1818
import type { RepositoryContext, GetParams } from "../abstract";
1919
import { AbstractResourceRepository } from "../abstract";
2020
import { getReferenceFromResourceIdentifier } from "../helpers";

0 commit comments

Comments
 (0)