You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
2.3 KiB
55 lines
2.3 KiB
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const singlePage = fs.readFileSync(
|
|
path.join(process.cwd(), 'src/pages/catalog/ShopSinglePage.tsx'),
|
|
'utf8'
|
|
);
|
|
const shopApiSrc = fs.readFileSync(
|
|
path.join(process.cwd(), 'src/lib/shopApi.ts'),
|
|
'utf8'
|
|
);
|
|
|
|
// 1. ShopSinglePage uses reviewsCount backed by liveProduct.reviews_count instead of displayReviews.length
|
|
assert.ok(singlePage.includes('liveProduct.reviews_count'));
|
|
assert.ok(!singlePage.includes('Review(${displayReviews.length})'));
|
|
assert.ok(!singlePage.includes('Customer Reviews (${displayReviews.length})'));
|
|
assert.ok(singlePage.includes('resolveMediaUrl(r.image_url)'));
|
|
assert.ok(singlePage.includes('review.imageUrl &&'));
|
|
|
|
// 2. reviewsCount logic handles >20 cap, seeded count, and fallback
|
|
function computeReviewsCount(liveProduct, displayReviewsLen) {
|
|
return liveProduct
|
|
? Math.max(liveProduct.reviews_count ?? 0, displayReviewsLen)
|
|
: displayReviewsLen;
|
|
}
|
|
assert.equal(computeReviewsCount({ reviews_count: 47 }, 20), 47);
|
|
assert.equal(computeReviewsCount({ reviews_count: 12 }, 0), 12);
|
|
assert.equal(computeReviewsCount({ reviews_count: 0 }, 2), 2);
|
|
assert.equal(computeReviewsCount(null, 3), 3);
|
|
|
|
// 3. createProductReview builds FormData when File is provided and keeps JSON otherwise
|
|
assert.ok(shopApiSrc.includes("form.append('image', payload.image)"));
|
|
function buildReviewBody(payload) {
|
|
if (payload.image) {
|
|
const form = new FormData();
|
|
form.append('rating', String(payload.rating));
|
|
form.append('comment', payload.comment);
|
|
form.append('image', payload.image);
|
|
if (payload.author_name) form.append('author_name', payload.author_name);
|
|
return form;
|
|
}
|
|
return payload;
|
|
}
|
|
const jsonBody = buildReviewBody({ rating: 5, comment: 'Great!' });
|
|
assert.ok(!(jsonBody instanceof FormData));
|
|
const file = new File([new Uint8Array([137, 80, 78, 71])], 'photo.png', { type: 'image/png' });
|
|
const multipartBody = buildReviewBody({ rating: 4, comment: 'Nice!', image: file, author_name: 'Ali' });
|
|
assert.ok(multipartBody instanceof FormData);
|
|
assert.equal(multipartBody.get('rating'), '4');
|
|
assert.equal(multipartBody.get('comment'), 'Nice!');
|
|
assert.equal(multipartBody.get('author_name'), 'Ali');
|
|
assert.equal(multipartBody.get('image')?.name, 'photo.png');
|
|
|
|
console.log('PASS: Product review count & multipart image assertions passed.');
|