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.

6.5 KB
Raw Blame
import { expect, type Page, test } from '@playwright/test'
import { csvFile, login } from './helpers'

const PRODUCTS_PATH = (storeId: string) => `/${storeId}/products`

async function openImportSheet(page: Page, storeId: string) {
  await page.goto(PRODUCTS_PATH(storeId))
  await page.getByRole('button', { name: /^import$/i }).click()
  await expect(page.getByRole('heading', { name: /import from csv/i })).toBeVisible()
}

test.describe('csv import', () => {
  test('imports products through the upload → mapping → progress → results wizard', async ({
    page,
  }) => {
    // Upload + background row processing legitimately take a while.
    test.setTimeout(180_000)

    const creds = await login(page)
    const suffix = Date.now()
    const goodName = `E2E Imported Product ${suffix}`

    await openImportSheet(page, creds.store_id)

    // Rows without name/price are guaranteed row-level failures that feed the
    // failed-rows report without failing the whole import. Thirty of them
    // paginate the failure report (25 per page) so the "next page" control has
    // something to page through.
    const badRows = Array.from({ length: 30 }, (_, i) => {
      const n = String(i + 1).padStart(2, '0')
      return `e2e-import-bad-${n}-${suffix},E2E-IMP-BAD-${n}-${suffix},,`
    })
    await page
      .getByRole('dialog')
      .locator('input[type="file"]')
      .setInputFiles(
        csvFile([
          'slug,sku,name,price',
          `e2e-import-ok-${suffix},E2E-IMP-OK-${suffix},${goodName},10.00`,
          ...badRows,
        ]),
      )
    await page.getByRole('button', { name: /^continue$/i }).click()

    // Mapping step — canonical headers auto-assign, so required fields are
    // already satisfied and the sample value from the file is shown.
    // (CardTitle renders a div, so match by text rather than heading role.)
    await expect(page.getByText('Map columns')).toBeVisible({ timeout: 15_000 })
    await expect(page.getByText(`e2e-import-ok-${suffix}`)).toBeVisible()

    await page.getByRole('button', { name: /start import/i }).click()

    // Progress → results, driven entirely by the 2s poll.
    await expect(page.getByText(/import completed/i)).toBeVisible({ timeout: 120_000 })
    await expect(page.getByText(/30 failed/i).first()).toBeVisible()

    // Failure report: broken rows are listed (paginated — 30 rows, 2 pages)
    // and their raw data is inspectable.
    // exact: true — "Retry failed rows (30)" would otherwise also match.
    await expect(page.getByText('Failed rows', { exact: true })).toBeVisible()
    await expect(page.getByRole('button', { name: /next page/i })).toBeVisible()
    await page
      .getByRole('button', { name: /view row data/i })
      .first()
      .click()
    await expect(page.getByText(`E2E-IMP-BAD-01-${suffix}`)).toBeVisible()

    // Retry re-runs the still-broken row: the wizard flips back into the
    // processing state (retry-pass copy) and completes again with the
    // failure intact.
    await page.getByRole('button', { name: /retry failed rows/i }).click()
    await expect(page.getByText(/retrying failed rows/i)).toBeVisible({ timeout: 15_000 })
    await expect(page.getByText(/import completed/i)).toBeVisible({ timeout: 120_000 })
    await expect(page.getByRole('button', { name: /retry failed rows/i })).toBeVisible({
      timeout: 15_000,
    })

    // The good row became a real product, visible through in-app navigation:
    // completion invalidates the products cache, so the list mounted under
    // the wizard is already fresh. (A full-page goto would reset the query
    // cache and mask a stale list.)
    await page.getByRole('button', { name: /view products/i }).click()
    await expect(page.getByText(goodName)).toBeVisible({ timeout: 15_000 })
  })

  // The column separator travels as the import's `delimiter` setting; a
  // semicolon file only maps and imports if the server read it back.
  test('imports a semicolon-separated file', async ({ page }) => {
    test.setTimeout(180_000)

    const creds = await login(page)
    const suffix = Date.now()
    const name = `E2E Semicolon Product ${suffix}`

    await openImportSheet(page, creds.store_id)
    const sheet = page.getByRole('dialog')
    await sheet
      .locator('[data-slot="field"]')
      .filter({ hasText: /^column separator/i })
      .getByRole('combobox')
      .click()
    await page.getByRole('option', { name: /^semicolon/i }).click()
    await sheet
      .locator('input[type="file"]')
      .setInputFiles(
        csvFile([
          'slug;sku;name;price',
          `e2e-semicolon-${suffix};E2E-SEMI-${suffix};${name};12.00`,
        ]),
      )
    await page.getByRole('button', { name: /^continue$/i }).click()

    // Split on the semicolon, every column is its own field with its own
    // sample — read as commas, the whole line would be a single column.
    await expect(page.getByText('Map columns')).toBeVisible({ timeout: 15_000 })
    await expect(page.getByText(`E2E-SEMI-${suffix}`, { exact: true })).toBeVisible()

    await page.getByRole('button', { name: /start import/i }).click()
    await expect(page.getByText(/import completed/i)).toBeVisible({ timeout: 120_000 })

    await page.getByRole('button', { name: /view products/i }).click()
    await expect(page.getByText(name)).toBeVisible({ timeout: 15_000 })
  })

  test('blocks starting until required fields are mapped', async ({ page }) => {
    const creds = await login(page)
    const suffix = Date.now()

    await openImportSheet(page, creds.store_id)

    // No `price` column at all — a required schema field stays unmapped.
    await page
      .getByRole('dialog')
      .locator('input[type="file"]')
      .setInputFiles(
        csvFile([
          'slug,sku,name',
          `e2e-import-nomap-${suffix},E2E-IMP-NM-${suffix},No Price ${suffix}`,
        ]),
      )
    await page.getByRole('button', { name: /^continue$/i }).click()

    await expect(page.getByText('Map columns')).toBeVisible({ timeout: 15_000 })
    await expect(page.getByRole('button', { name: /start import/i })).toBeDisabled()
    await expect(page.getByText(/map the required fields to continue/i)).toBeVisible()

    // The import history lists the abandoned mapping-state import too, and a
    // row click re-opens the wizard dialog right where the user left off.
    await page.goto(`/${creds.store_id}/settings/imports`)
    await expect(page.getByText(/^IM\d+/).first()).toBeVisible({ timeout: 15_000 })
    await page
      .getByText(/^IM\d+/)
      .first()
      .click()
    await expect(page.getByText('Map columns')).toBeVisible({ timeout: 15_000 })
  })
})