|
| 1 | +import { expect, test } from '~/tests/fixtures'; |
| 2 | +import { getFormatter } from '~/tests/lib/formatter'; |
| 3 | +import { getTranslations } from '~/tests/lib/i18n'; |
| 4 | + |
| 5 | +test('Validate compare page', async ({ page, catalog, currency }) => { |
| 6 | + const format = getFormatter(); |
| 7 | + const defaultCurrency = await currency.getDefaultCurrency(); |
| 8 | + |
| 9 | + const product = await catalog.getDefaultOrCreateSimpleProduct(); |
| 10 | + const productWithVariants = await catalog.getDefaultOrCreateComplexProduct(); |
| 11 | + |
| 12 | + await page.goto(`/compare/?ids=${product.id},${productWithVariants.id}`); |
| 13 | + await expect(page.getByRole('heading', { name: 'Compare products 2' })).toBeVisible(); |
| 14 | + |
| 15 | + // Products names |
| 16 | + await expect(page.getByText(product.name, { exact: true })).toBeVisible(); |
| 17 | + await expect(page.getByText(productWithVariants.name, { exact: true })).toBeVisible(); |
| 18 | + |
| 19 | + // Products CTAs |
| 20 | + await expect(page.getByRole('button', { name: 'Add to cart' })).toBeVisible(); |
| 21 | + await expect(page.getByRole('link', { name: 'View options' })).toBeVisible(); |
| 22 | + |
| 23 | + // Product prices |
| 24 | + const productPrice = format.number(product.price, { |
| 25 | + style: 'currency', |
| 26 | + currency: defaultCurrency, |
| 27 | + }); |
| 28 | + |
| 29 | + const productWithVariantsPrice = format.number(productWithVariants.price, { |
| 30 | + style: 'currency', |
| 31 | + currency: defaultCurrency, |
| 32 | + }); |
| 33 | + |
| 34 | + await expect(page.getByText(productPrice)).toBeVisible(); |
| 35 | + await expect(page.getByText(productWithVariantsPrice)).toBeVisible(); |
| 36 | +}); |
| 37 | + |
| 38 | +test('Validate compare page with alternate currency', async ({ page, catalog, currency }) => { |
| 39 | + const format = getFormatter(); |
| 40 | + const defaultCurrency = await currency.getDefaultCurrency(); |
| 41 | + const alternateCurrency = (await currency.getEnabledCurrencies()).find( |
| 42 | + (c) => c !== defaultCurrency, |
| 43 | + ); |
| 44 | + |
| 45 | + if (!alternateCurrency) { |
| 46 | + test.skip(true, 'No alternative currencies found.'); |
| 47 | + |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const product = await catalog.getDefaultOrCreateSimpleProduct(); |
| 52 | + const productWithVariants = await catalog.getDefaultOrCreateComplexProduct(); |
| 53 | + |
| 54 | + await page.goto(`/compare/?ids=${product.id},${productWithVariants.id}`); |
| 55 | + await page.waitForLoadState('networkidle'); |
| 56 | + await expect( |
| 57 | + page.getByText( |
| 58 | + format.number(product.price, { |
| 59 | + style: 'currency', |
| 60 | + currency: defaultCurrency, |
| 61 | + }), |
| 62 | + ), |
| 63 | + ).toBeVisible(); |
| 64 | + |
| 65 | + await currency.selectCurrency(alternateCurrency); |
| 66 | + |
| 67 | + const productPriceConverted = await currency.convertWithExchangeRate( |
| 68 | + alternateCurrency, |
| 69 | + product.price, |
| 70 | + ); |
| 71 | + |
| 72 | + const formattedProductPrice = format.number(productPriceConverted, { |
| 73 | + style: 'currency', |
| 74 | + currency: alternateCurrency, |
| 75 | + }); |
| 76 | + |
| 77 | + await expect(page.getByText(formattedProductPrice)).toBeVisible(); |
| 78 | + |
| 79 | + const productWithVariantsPriceConverted = await currency.convertWithExchangeRate( |
| 80 | + alternateCurrency, |
| 81 | + productWithVariants.price, |
| 82 | + ); |
| 83 | + |
| 84 | + const formattedProductWithVariantsPrice = format.number(productWithVariantsPriceConverted, { |
| 85 | + style: 'currency', |
| 86 | + currency: alternateCurrency, |
| 87 | + }); |
| 88 | + |
| 89 | + await expect(page.getByText(formattedProductWithVariantsPrice)).toBeVisible(); |
| 90 | +}); |
| 91 | + |
| 92 | +test('Can add simple product to cart', async ({ page, catalog }) => { |
| 93 | + const t = await getTranslations('Compare'); |
| 94 | + |
| 95 | + const product = await catalog.getDefaultOrCreateSimpleProduct(); |
| 96 | + |
| 97 | + await page.goto(`/compare/?ids=${product.id}`); |
| 98 | + |
| 99 | + await page.getByRole('button', { name: 'Add to cart' }).click(); |
| 100 | + |
| 101 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 102 | + const addToCartSuccessMessage = t.rich('successMessage', { |
| 103 | + cartItems: 1, |
| 104 | + cartLink: (chunks: React.ReactNode) => chunks, |
| 105 | + }) as string; |
| 106 | + |
| 107 | + await expect(page.getByText(addToCartSuccessMessage)).toBeVisible(); |
| 108 | +}); |
| 109 | + |
| 110 | +test("Product with variants 'View options' redirect to product page", async ({ page, catalog }) => { |
| 111 | + const product = await catalog.getDefaultOrCreateComplexProduct(); |
| 112 | + |
| 113 | + await page.goto(`/compare/?ids=${product.id}`); |
| 114 | + await page.getByRole('link', { name: 'View options' }).click(); |
| 115 | + |
| 116 | + await expect(page.getByRole('heading', { name: product.name })).toBeVisible(); |
| 117 | +}); |
| 118 | + |
| 119 | +test('Disabled add to cart for out of stock products', async ({ page, catalog }) => { |
| 120 | + const product = await catalog.createSimpleProduct({ |
| 121 | + inventoryTracking: 'product', |
| 122 | + inventoryLevel: 0, |
| 123 | + }); |
| 124 | + |
| 125 | + const productWithVariants = await catalog.createComplexProduct({ |
| 126 | + inventoryTracking: 'product', |
| 127 | + inventoryLevel: 0, |
| 128 | + }); |
| 129 | + |
| 130 | + await page.goto(`/compare/?ids=${product.id},${productWithVariants.id}`); |
| 131 | + |
| 132 | + // Simple products should have the add to cart button disabled |
| 133 | + await expect(page.getByRole('button', { name: 'Add to Cart' })).toBeDisabled(); |
| 134 | + // Product with variants should have the view options link even when OOS |
| 135 | + await expect(page.getByRole('link', { name: 'View options' })).toBeVisible(); |
| 136 | +}); |
| 137 | + |
| 138 | +test('Show empty state when no products are selected', async ({ page }) => { |
| 139 | + await page.goto('/compare'); |
| 140 | + |
| 141 | + await expect(page.getByText('No products to compare')).toBeVisible(); |
| 142 | +}); |
0 commit comments