test(storefront): phase 2 catalog, multi-currency, cart & coupon E2E tests
This commit is contained in:
parent
4356f8321f
commit
fc7660e672
|
|
@ -0,0 +1,65 @@
|
|||
import { test, expect } from '@playwright/test';
|
||||
import { mockAuthenticatedUser, mockPublicStoreApis } from './fixtures/apiMocks';
|
||||
|
||||
test.describe('Cart, Multi-Currency & Coupons', () => {
|
||||
test('adds/removes items and enforces Complete Pack and Lifetime bundle rules', async ({ page }) => {
|
||||
await mockPublicStoreApis(page);
|
||||
await page.goto('./');
|
||||
|
||||
// Add individual Points: Acu
|
||||
await page.getByText('Acu (EOV+Cat+Ext+MR)').click();
|
||||
await expect(page.getByText('1 Item')).toBeVisible();
|
||||
|
||||
// Selecting Points Complete Pack replaces individual Points: Acu
|
||||
await page.locator('span', { hasText: /^Complete Pack$/ }).first().click();
|
||||
await expect(page.getByText('1 Item')).toBeVisible();
|
||||
await expect(page.getByText('Points: Complete Pack')).toBeVisible();
|
||||
|
||||
// Selecting Lifetime Pass replaces everything else in cart
|
||||
await page.getByText('Lifetime Unlimited Access Pass').nth(1).click();
|
||||
await expect(page.getByText('1 Item')).toBeVisible();
|
||||
await expect(page.getByText('Lifetime Unlimited Access Pass').last()).toBeVisible();
|
||||
|
||||
// Remove item via trash button in CartDrawer
|
||||
await page.getByTitle('Remove item').click();
|
||||
await expect(page.getByText('0 Items')).toBeVisible();
|
||||
await expect(page.getByText('Your checkout cart is empty.')).toBeVisible();
|
||||
});
|
||||
|
||||
test('applies valid coupon, handles invalid coupon, and removes coupon', async ({ page }) => {
|
||||
await mockPublicStoreApis(page);
|
||||
await page.goto('./');
|
||||
|
||||
await page.getByText('Acu (EOV+Cat+Ext+MR)').click();
|
||||
await expect(page.getByText('Base Subtotal')).toBeVisible();
|
||||
|
||||
const couponInput = page.getByPlaceholder('e.g. AGENT40, EVENT_NEW');
|
||||
await couponInput.fill('SAVE20');
|
||||
await page.getByRole('button', { name: 'Apply' }).click();
|
||||
|
||||
await expect(page.getByText(/Coupon Active: SAVE20/)).toBeVisible();
|
||||
await expect(page.getByText('Coupon Discount (SAVE20)')).toBeVisible();
|
||||
|
||||
// Remove coupon
|
||||
await page.getByRole('button', { name: 'Remove', exact: true }).click();
|
||||
await expect(page.getByText(/Coupon Active: SAVE20/)).not.toBeVisible();
|
||||
|
||||
// Try invalid coupon
|
||||
await couponInput.fill('BADCODE');
|
||||
await page.getByRole('button', { name: 'Apply' }).click();
|
||||
await expect(page.getByText(/Invalid or expired coupon code/i)).toBeVisible();
|
||||
});
|
||||
|
||||
test('displays EUR pricing without GST for European user', async ({ page }) => {
|
||||
await mockPublicStoreApis(page);
|
||||
await mockAuthenticatedUser(page, {
|
||||
country: 'Germany',
|
||||
phone_number: '+4915123456789',
|
||||
});
|
||||
|
||||
await page.goto('./');
|
||||
await expect(page.getByText('Billing Currency:')).toBeVisible();
|
||||
await page.getByText('Acu (EOV+Cat+Ext+MR)').click();
|
||||
await expect(page.getByText('€5').first()).toBeVisible();
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
import { test, expect } from '@playwright/test';
|
||||
import { MOCK_PLANS, mockAuthenticatedUser, mockPublicStoreApis } from './fixtures/apiMocks';
|
||||
|
||||
test.describe('Catalog Section', () => {
|
||||
test('renders custom curated bundles when admin plan exists', async ({ page }) => {
|
||||
await mockPublicStoreApis(page);
|
||||
await page.route('**/api/plans', async (route) => {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify([
|
||||
...MOCK_PLANS,
|
||||
{
|
||||
code: 'SUMMER_COMBO',
|
||||
name: 'Summer Special Combo',
|
||||
description: 'Special combo of Acupoints and Nutrition',
|
||||
plan_type: 'bundle',
|
||||
billing_period: 'yearly',
|
||||
price_inr: 700,
|
||||
price_usd: 15,
|
||||
price_eur: 15,
|
||||
is_active: true,
|
||||
badge_text: 'Limited Offer',
|
||||
heading: 'Special Curated Bundles',
|
||||
subtitle: 'Hand-picked bundles',
|
||||
modules: [
|
||||
{ slug: 'acupuncture-points', name: 'Acupuncture Points' },
|
||||
{ slug: 'nutrition', name: 'Nutrition' },
|
||||
],
|
||||
module_slugs: ['acupuncture-points', 'nutrition'],
|
||||
},
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
await page.goto('./');
|
||||
await expect(page.getByText('Special Curated Bundles')).toBeVisible();
|
||||
await expect(page.getByText('Summer Special Combo')).toBeVisible();
|
||||
await expect(page.getByText('Limited Offer').first()).toBeVisible();
|
||||
});
|
||||
|
||||
test('toggles Hide Locked / Show Locked and marks unlocked modules as UNLOCKED', async ({ page }) => {
|
||||
await mockPublicStoreApis(page);
|
||||
await mockAuthenticatedUser(page, {
|
||||
unlocked_module_codes: ['POINTS_ACU'],
|
||||
active_subscriptions: [
|
||||
{
|
||||
id: 'sub-1',
|
||||
plan_code: 'POINTS_ACU',
|
||||
status: 'active',
|
||||
started_at: '2026-01-01T00:00:00Z',
|
||||
expires_at: '2027-01-01T00:00:00Z',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await page.goto('./');
|
||||
|
||||
// POINTS_ACU should display UNLOCKED badge
|
||||
await expect(page.getByText('UNLOCKED').first()).toBeVisible();
|
||||
|
||||
// Hide Locked for Points card -> hides locked items but keeps UNLOCKED item visible
|
||||
const hideLockedBtn = page.getByRole('button', { name: /Hide Locked/i }).first();
|
||||
await hideLockedBtn.click();
|
||||
await expect(page.getByRole('button', { name: /Show Locked/i }).first()).toBeVisible();
|
||||
await expect(page.getByText('Acu (EOV+Cat+Ext+MR)')).toBeVisible();
|
||||
await expect(page.getByText('Ear / Auricular')).not.toBeVisible();
|
||||
|
||||
// Show Locked restores them
|
||||
await page.getByRole('button', { name: /Show Locked/i }).first().click();
|
||||
await expect(page.getByText('Ear / Auricular')).toBeVisible();
|
||||
});
|
||||
|
||||
test('hides inactive locked plans from the catalog', async ({ page }) => {
|
||||
await mockPublicStoreApis(page);
|
||||
await page.route('**/api/plans', async (route) => {
|
||||
const updated = MOCK_PLANS.map((p) =>
|
||||
p.code === 'POINTS_SCALP' ? { ...p, is_active: false } : p
|
||||
);
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify(updated),
|
||||
});
|
||||
});
|
||||
|
||||
await page.goto('./');
|
||||
await expect(page.getByText('Acu (EOV+Cat+Ext+MR)')).toBeVisible();
|
||||
await expect(page.getByText('Scalp', { exact: true })).not.toBeVisible();
|
||||
});
|
||||
});
|
||||
|
|
@ -140,4 +140,104 @@ export async function mockPublicStoreApis(page: Page) {
|
|||
body: JSON.stringify({ modules: [] }),
|
||||
});
|
||||
});
|
||||
|
||||
await page.route('**/api/cart/calculate', async (route) => {
|
||||
const reqBody = route.request().postDataJSON() || {};
|
||||
const currency = (reqBody.currency || 'INR').toUpperCase();
|
||||
const coupon = reqBody.coupon_code ? String(reqBody.coupon_code).trim().toUpperCase() : null;
|
||||
const rawItems = reqBody.cart_item_codes || reqBody.items || [];
|
||||
const itemCodes: string[] = Array.isArray(rawItems)
|
||||
? rawItems.map((i: any) => (typeof i === 'string' ? i : i.code))
|
||||
: [];
|
||||
|
||||
const priceKey = currency === 'INR' ? 'price_inr' : currency === 'EUR' ? 'price_eur' : 'price_usd';
|
||||
const cartItems = itemCodes.map((code) => {
|
||||
const found = MOCK_PLANS.find((p) => p.code === code);
|
||||
return {
|
||||
code,
|
||||
name: found?.name || code,
|
||||
price: found ? (found as any)[priceKey] : 100,
|
||||
};
|
||||
});
|
||||
|
||||
const baseSubtotal = cartItems.reduce((sum, item) => sum + item.price, 0);
|
||||
let couponDiscount = 0;
|
||||
let couponApplied = 'None';
|
||||
let couponError: string | null = null;
|
||||
|
||||
if (coupon === 'SAVE20') {
|
||||
couponDiscount = Math.round(baseSubtotal * 0.2 * 100) / 100;
|
||||
couponApplied = 'Code: SAVE20 - 20% Off';
|
||||
} else if (coupon === 'BADCODE') {
|
||||
couponError = 'Invalid or expired coupon code.';
|
||||
}
|
||||
|
||||
const totalDiscounts = couponDiscount;
|
||||
const grandTotal = Math.round(Math.max(0, baseSubtotal - totalDiscounts) * 100) / 100;
|
||||
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({
|
||||
currency,
|
||||
cart_items: cartItems,
|
||||
base_subtotal: baseSubtotal,
|
||||
bundle_discount_amount: 0,
|
||||
renewal_discount_amount: 0,
|
||||
coupon_discount_amount: couponDiscount,
|
||||
total_discounts: totalDiscounts,
|
||||
convenience_fee_rate: 0,
|
||||
convenience_fee: 0,
|
||||
taxable_amount: 0,
|
||||
gst_amount: 0,
|
||||
grand_total: grandTotal,
|
||||
discount_policy: 'None',
|
||||
coupon_applied: couponApplied,
|
||||
coupon_error: couponError,
|
||||
}),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function mockAuthenticatedUser(page: Page, overrides: Record<string, any> = {}) {
|
||||
const userPayload = {
|
||||
id: 'user-1',
|
||||
email: 'practitioner@acupedia.in',
|
||||
display_name: 'Dr. Test',
|
||||
phone_number: '+919876543210',
|
||||
country: 'India',
|
||||
...overrides,
|
||||
};
|
||||
|
||||
const accessPayload = {
|
||||
user_id: userPayload.id,
|
||||
active_subscriptions: overrides.active_subscriptions || [],
|
||||
active_entitlements: overrides.active_entitlements || [],
|
||||
unlocked_module_codes: overrides.unlocked_module_codes || [],
|
||||
unlocked_module_slugs: overrides.unlocked_module_slugs || [],
|
||||
};
|
||||
|
||||
await page.addInitScript(() => {
|
||||
localStorage.setItem('access_token', 'mock-jwt-token');
|
||||
});
|
||||
|
||||
await page.route('**/api/auth/profile', async (route) => {
|
||||
if (route.request().method() === 'GET') {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify(userPayload),
|
||||
});
|
||||
} else {
|
||||
await route.continue();
|
||||
}
|
||||
});
|
||||
|
||||
await page.route('**/api/access/me', async (route) => {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify(accessPayload),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue