Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

11.0 KB
Raw Blame
import { expect, test } from '@playwright/test'
import {
  FIXTURE_BULK_CATEGORY,
  FIXTURE_BULK_CHANNEL_NAME,
  FIXTURE_PROMO_TAXON,
  login,
} from './helpers'
import {
  categorizationCard,
  createProduct,
  publishingCard,
  typeDescription,
} from './products-helpers'

test.describe('product edit', () => {
  test('creates a product and lands on the edit page', async ({ page }) => {
    const creds = await login(page)
    const name = `E2E Product Create ${Date.now()}`

    await createProduct(page, creds.store_id, name, 'Initial description')

    // The Name input on the edit page mirrors the saved name.
    await expect(page.getByLabel(/^name$/i)).toHaveValue(name)
  })

  test('reflects list, quote and link formatting in the description editor', async ({ page }) => {
    const creds = await login(page)
    const name = `E2E Product Editor Styles ${Date.now()}`

    await createProduct(page, creds.store_id, name)

    const description = page.locator('#product-description')
    await typeDescription(page, 'Formatted line')

    const toolbar = page.locator('[data-slot="rich-text-editor-toolbar"]')
    const bulletBtn = toolbar.getByRole('button', { name: /^bullet list$/i })
    await bulletBtn.click()
    const bulletList = description.locator('ul')
    await expect(bulletList).toBeVisible()
    await expect(bulletList).toHaveCSS('list-style-type', 'disc')
    await expect(bulletBtn).toHaveAttribute('aria-pressed', 'true')
    await bulletBtn.click()

    const orderedBtn = toolbar.getByRole('button', { name: /^ordered list$/i })
    await orderedBtn.click()
    const orderedList = description.locator('ol')
    await expect(orderedList).toBeVisible()
    await expect(orderedList).toHaveCSS('list-style-type', 'decimal')
    await expect(orderedBtn).toHaveAttribute('aria-pressed', 'true')
    await orderedBtn.click()

    const quoteBtn = toolbar.getByRole('button', { name: /^blockquote$/i })
    await quoteBtn.click()
    const quote = description.locator('blockquote')
    await expect(quote).toBeVisible()
    await expect(quote).toHaveCSS('border-inline-start-width', '3px')
    await expect(quoteBtn).toHaveAttribute('aria-pressed', 'true')

    await quote.click({ clickCount: 3 })
    page.once('dialog', (dialog) => dialog.accept('https://example.com'))
    await toolbar.getByRole('button', { name: /^link$/i }).click()
    const link = description.locator('a[href="https://example.com"]')
    await expect(link).toBeVisible()
    await expect(link).toHaveCSS('text-decoration-line', 'underline')
    await expect(page.getByRole('button', { name: /^link$/i })).toHaveAttribute(
      'aria-pressed',
      'true',
    )
  })

  test('cmd+b bolds description text without collapsing the sidebar', async ({ page }) => {
    const creds = await login(page)
    const name = `E2E Product Editor Bold ${Date.now()}`

    await createProduct(page, creds.store_id, name)

    const sidebar = page.locator('[data-slot="sidebar"][data-state]').first()
    await expect(sidebar).toHaveAttribute('data-state', 'expanded')

    await typeDescription(page, 'Bold this')
    await page.keyboard.press('ControlOrMeta+A')
    await page.keyboard.press('ControlOrMeta+B')

    const boldText = page.locator('#product-description strong')
    await expect(boldText).toHaveText('Bold this')
    await boldText.click()
    await expect(page.getByRole('button', { name: /^bold$/i })).toHaveAttribute(
      'aria-pressed',
      'true',
    )
    await expect(sidebar).toHaveAttribute('data-state', 'expanded')
  })

  test('preserves multi-paragraph description formatting across reload', async ({ page }) => {
    const creds = await login(page)
    const name = `E2E Product Rich Text ${Date.now()}`

    await createProduct(page, creds.store_id, name)

    // Type two paragraphs on the edit page so the save goes through the
    // update path (the reported bug scenario), not create.
    await typeDescription(page, 'First paragraph\nSecond paragraph')

    await page.getByRole('button', { name: /save product/i }).click()
    await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled({
      timeout: 15_000,
    })

    await page.reload()

    // The saved HTML must round-trip as two separate paragraphs. Before the
    // fix the form hydrated from the tag-stripped, whitespace-squished
    // plain-text `description`, collapsing both lines into a single block.
    const description = page.locator('#product-description')
    await expect(description.locator('p')).toHaveCount(2)
    await expect(description.locator('p').first()).toHaveText('First paragraph')
    await expect(description.locator('p').nth(1)).toHaveText('Second paragraph')
  })

  test('updates name on an existing product', async ({ page }) => {
    const creds = await login(page)
    const original = `E2E Product Update ${Date.now()}`
    const updated = `${original} (updated)`

    await createProduct(page, creds.store_id, original)

    await page.getByLabel(/^name$/i).fill(updated)
    await page.getByRole('button', { name: /save product/i }).click()

    // After a successful save the button disables again (RHF `isDirty`
    // resets via `reset(updatedValues)`). Asserting on disabled state avoids
    // coupling to toast strings, which aren't stable across i18n updates.
    await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled({
      timeout: 15_000,
    })

    await page.reload()
    await expect(page.getByLabel(/^name$/i)).toHaveValue(updated)
  })

  test('keeps picked categories when Escape dismisses the search', async ({ page }) => {
    const creds = await login(page)
    await createProduct(page, creds.store_id, `E2E Product Categories ${Date.now()}`)

    const categoriesField = page
      .locator('[data-slot="field"]')
      .filter({ has: page.getByText('Categories', { exact: true }) })
    const search = categoriesField.getByRole('combobox')
    const chips = categoriesField.locator('[data-slot="combobox-chip"]')

    // Picking a searched result closes the list, so the Escape that follows
    // lands on a closed picker.
    for (const category of [FIXTURE_PROMO_TAXON, FIXTURE_BULK_CATEGORY]) {
      await search.fill(category)
      await page
        .getByRole('option', { name: new RegExp(category, 'i') })
        .first()
        .click()
    }
    await expect(chips).toHaveCount(2)
    await expect(page.getByRole('listbox')).toBeHidden()
    await expect(search).toBeFocused()
    await page.keyboard.press('Escape')
    await expect(chips).toHaveCount(2)

    await page.getByRole('button', { name: /save product/i }).click()
    await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled({
      timeout: 15_000,
    })
    await page.reload()
    await expect(chips).toHaveCount(2)
  })

  test('lists a product on an additional channel via the publishing card', async ({ page }) => {
    const creds = await login(page)
    const name = `E2E Product Publish ${Date.now()}`

    await createProduct(page, creds.store_id, name)

    // New products auto-publish on the store's default channel ("Online
    // Store"), so the card is non-empty from the start. Assert the bulk
    // channel isn't there yet, then add it via Manage.
    const card = publishingCard(page)
    await expect(card.getByText(new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i'))).not.toBeVisible()

    await card.getByRole('button', { name: /^manage$/i }).click()
    await expect(page.getByRole('heading', { name: /^manage sales channels$/i })).toBeVisible()

    await page
      .getByRole('dialog')
      .getByRole('button', { name: new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i') })
      .click()
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^done$/i })
      .click()

    await expect(card.getByText(new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i'))).toBeVisible()

    await page.getByRole('button', { name: /save product/i }).click()
    await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled({
      timeout: 15_000,
    })

    // After reload the publication is still attached.
    await page.reload()
    await expect(
      publishingCard(page).getByText(new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i')),
    ).toBeVisible()
  })

  test('unlists a product from a channel via the publishing card', async ({ page }) => {
    const creds = await login(page)
    const name = `E2E Product Unpublish ${Date.now()}`

    await createProduct(page, creds.store_id, name)
    const card = publishingCard(page)

    // Attach the bulk channel first so we have something to detach.
    await card.getByRole('button', { name: /^manage$/i }).click()
    await page
      .getByRole('dialog')
      .getByRole('button', { name: new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i') })
      .click()
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^done$/i })
      .click()
    await expect(card.getByText(new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i'))).toBeVisible()
    await page.getByRole('button', { name: /save product/i }).click()
    await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled({
      timeout: 15_000,
    })

    // Re-open Manage and toggle the same channel off — exercises the
    // re-sync write path (POST product_publications without that channel)
    // and locks in the idempotent-channel + remove-missing behaviour
    // covered by the controller spec.
    await card.getByRole('button', { name: /^manage$/i }).click()
    await page
      .getByRole('dialog')
      .getByRole('button', { name: new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i') })
      .click()
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^done$/i })
      .click()

    await expect(card.getByText(new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i'))).not.toBeVisible()

    await page.getByRole('button', { name: /save product/i }).click()
    await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled({
      timeout: 15_000,
    })

    await page.reload()
    await expect(
      publishingCard(page).getByText(new RegExp(FIXTURE_BULK_CHANNEL_NAME, 'i')),
    ).not.toBeVisible()
  })

  // The product tag vocabulary is fetched by its short name (`product`); a
  // tag saved on one product is offered as an existing tag on the next.
  test('tags a product and offers the tag on another product', async ({ page }) => {
    const creds = await login(page)
    const suffix = Date.now()
    const tagName = `e2e-product-tag-${suffix}`

    await createProduct(page, creds.store_id, `E2E Tagged A ${suffix}`)
    const input = categorizationCard(page).getByPlaceholder(/type to add tags/i)
    await input.fill(tagName)
    await input.press('Enter')
    await page.getByRole('button', { name: /save product/i }).click()
    await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled({
      timeout: 30_000,
    })

    await createProduct(page, creds.store_id, `E2E Tagged B ${suffix}`)
    await categorizationCard(page)
      .getByPlaceholder(/type to add tags/i)
      .fill(tagName)
    await expect(page.getByRole('option', { name: tagName, exact: true })).toBeVisible({
      timeout: 15_000,
    })
  })
})