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.

22.0 KB
Raw Blame
import { expect, type Page, test } from '@playwright/test'
import {
  FIXTURE_PROMO_CUSTOMER_GROUP,
  FIXTURE_PROMO_PRODUCT,
  fillAddressForm,
  gotoIndex,
  login,
} from './helpers'

const CUSTOMERS_PATH = (storeId: string) => `/${storeId}/customers`
const CTA = /new customer/i

async function createCustomer(page: Page, email: string) {
  await page.getByRole('button', { name: /new customer/i }).click()
  await expect(page.getByRole('heading', { name: /^new customer$/i })).toBeVisible()
  await page.locator('#email').fill(email)
  await page.getByRole('button', { name: /^create customer$/i }).click()
  await expect(page.getByRole('heading', { name: email })).toBeVisible({ timeout: 15_000 })
}

test.describe('customers', () => {
  test('lists customers', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
  })

  test('creates a new customer', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-create-${Date.now()}@example.com`
    await createCustomer(page, email)

    // Back to the index, the row appears.
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
    await expect(page.getByRole('link', { name: email })).toBeVisible({ timeout: 15_000 })
  })

  test('shows no orders on the profile of a customer who has not ordered', async ({ page }) => {
    const creds = await login(page)

    // Somebody else's order, so an unfiltered list would have something to show
    await page.goto(`/${creds.store_id}/orders/new`)
    await expect(page.getByRole('heading', { name: /new order/i })).toBeVisible({ timeout: 15_000 })
    await page.locator('#order-email').fill(`e2e-other-buyer-${Date.now()}@example.com`)
    await page.getByPlaceholder(/search variant/i).fill(FIXTURE_PROMO_PRODUCT)
    await page
      .getByRole('option', { name: new RegExp(FIXTURE_PROMO_PRODUCT, 'i') })
      .first()
      .click()
    await page.locator('button[type="submit"]').click()
    await expect(page).toHaveURL(new RegExp(`/${creds.store_id}/orders/or_[^/]+$`), {
      timeout: 15_000,
    })

    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
    await createCustomer(page, `e2e-no-orders-${Date.now()}@example.com`)

    const ordersCard = page.locator('[data-slot="card"]').filter({
      has: page.locator('[data-slot="card-title"]', { hasText: /Orders/ }),
    })
    await expect(ordersCard.getByText(/no orders yet/i)).toBeVisible({ timeout: 15_000 })
    await expect(ordersCard.getByRole('row')).toHaveCount(0)
    await expect(ordersCard.locator('[data-slot="card-title"]')).toHaveText('Orders')
  })

  // Filled by the probes in src/testing/slot-probes.tsx.
  test('renders the plugin slot at the end of the main column', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
    await createCustomer(page, `e2e-slots-${Date.now()}@example.com`)

    await expect(page.getByText('Slot probe: customer.form_main')).toBeVisible()
  })

  test('edits a customer profile', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-edit-${Date.now()}@example.com`
    await createCustomer(page, email)

    // Scoped to the Profile card itself: other cards on the page (custom
    // fields, once a customer field is defined) carry an Edit button too.
    const profileCard = page
      .locator('[data-slot="card"]')
      .filter({ has: page.locator('[data-slot="card-title"]', { hasText: /^Profile$/ }) })
    await profileCard.getByRole('button', { name: /^edit$/i }).click()
    await expect(page.getByRole('heading', { name: /^edit customer$/i })).toBeVisible()

    await page.locator('#first_name').fill('Pat')
    await page.locator('#last_name').fill('Smith')
    await page.getByRole('button', { name: /^save$/i }).click()

    await expect(page.getByRole('heading', { name: 'Pat Smith' })).toBeVisible({ timeout: 15_000 })
  })

  test('adds an address', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-addr-${Date.now()}@example.com`
    await createCustomer(page, email)

    await page.getByRole('button', { name: /add address/i }).click()
    await expect(page.getByRole('heading', { name: /^add address$/i })).toBeVisible()

    await fillAddressForm(page, {
      firstName: 'Pat',
      lastName: 'Smith',
      address1: '1 Main St',
      city: 'Anytown',
      postalCode: '12345',
      phone: '555-0100',
      country: 'United States',
      state: 'California',
    })
    await page.getByRole('button', { name: /^save$/i }).click()

    await expect(page.getByText('1 Main St')).toBeVisible({ timeout: 15_000 })
  })

  test('issues store credit', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-credit-${Date.now()}@example.com`
    await createCustomer(page, email)

    await page.getByRole('button', { name: /issue store credit/i }).click()
    await expect(page.getByRole('heading', { name: /issue store credit/i })).toBeVisible()

    await page.locator('#sc-amount').fill('25.00')
    await page.locator('#sc-memo').fill('E2E test credit')

    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^issue store credit$/i })
      .click()

    await expect(page.getByText(/25\.00/).first()).toBeVisible({ timeout: 15_000 })
  })

  // The amount is typed in the admin's own number format whatever the
  // currency: an English-speaking admin writes `1,234.56` for euros too. It is
  // normalized to canonical form before sending, so it persists as 1234.56 and
  // renders €1,234.56, not a mangled 1.23 or 123456.
  test('issues store credit in EUR typed in the admin’s own number format', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-credit-eur-${Date.now()}@example.com`
    await createCustomer(page, email)

    await page.getByRole('button', { name: /issue store credit/i }).click()
    await expect(page.getByRole('heading', { name: /issue store credit/i })).toBeVisible()

    await page.locator('#sc-currency').click()
    await page.getByRole('option', { name: /EUR/ }).click()
    await page.locator('#sc-amount').fill('1,234.56')
    await page.locator('#sc-memo').fill('E2E EUR localized credit')

    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^issue store credit$/i })
      .click()

    await expect(page.getByText(/€\s?1,234\.56/).first()).toBeVisible({ timeout: 15_000 })
    await expect(page.getByText(/123,456/)).toHaveCount(0)
  })

  // Switching the currency after typing keeps the amount as typed: the format
  // follows the admin, not the currency, so `25.00` persists as €25.00, never
  // €2,500.00.
  test('keeps the amount when the store-credit currency switches', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-credit-switch-${Date.now()}@example.com`
    await createCustomer(page, email)

    await page.getByRole('button', { name: /issue store credit/i }).click()
    await expect(page.getByRole('heading', { name: /issue store credit/i })).toBeVisible()

    // Type a period-decimal amount under the USD default, THEN switch to EUR.
    await page.locator('#sc-amount').fill('25.00')
    await page.locator('#sc-currency').click()
    await page.getByRole('option', { name: /EUR/ }).click()
    await page.locator('#sc-memo').fill('E2E currency switch')

    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^issue store credit$/i })
      .click()

    // €25.00, not €2,500.00.
    await expect(page.getByText(/€\s?25\.00/).first()).toBeVisible({ timeout: 15_000 })
    await expect(page.getByText(/2,500/)).toHaveCount(0)
  })

  test('edits a store credit amount', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-credit-edit-${Date.now()}@example.com`
    await createCustomer(page, email)

    await page.getByRole('button', { name: /issue store credit/i }).click()
    await expect(page.getByRole('heading', { name: /issue store credit/i })).toBeVisible()
    await page.locator('#sc-amount').fill('25.00')
    await page.locator('#sc-memo').fill('E2E credit to edit')
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^issue store credit$/i })
      .click()
    await expect(page.getByText(/25\.00/).first()).toBeVisible({ timeout: 15_000 })

    // Open the credit row's actions menu → Edit, change the amount, and save.
    // The trigger is an icon-only button with no accessible name, so scope to
    // the table row that shows the amount and click its button. Covers the
    // EditStoreCreditDialog raw-string submit path.
    await page
      .getByRole('row', { name: /25\.00/ })
      .getByRole('button')
      .click()
    await page.getByRole('menuitem', { name: /^edit$/i }).click()
    await expect(page.getByRole('heading', { name: /edit store credit/i })).toBeVisible()

    const amount = page.locator('#edit-sc-amount')
    await expect(amount).toBeEnabled()
    await amount.fill('40.00')
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^save$/i })
      .click()

    await expect(page.getByText(/40\.00/).first()).toBeVisible({ timeout: 15_000 })
  })

  // Editing an EUR credit reads the new amount in the admin's own number
  // format. Issue in EUR, then edit to a comma-decimal value and confirm it
  // round-trips.
  test('edits an EUR store credit with a localized amount', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-credit-eur-edit-${Date.now()}@example.com`
    await createCustomer(page, email)

    // Issue an EUR credit at 50,00 (€50.00).
    await page.getByRole('button', { name: /issue store credit/i }).click()
    await expect(page.getByRole('heading', { name: /issue store credit/i })).toBeVisible()
    await page.locator('#sc-currency').click()
    await page.getByRole('option', { name: /EUR/ }).click()
    await page.locator('#sc-amount').fill('50,00')
    await page.locator('#sc-memo').fill('E2E EUR credit to edit')
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^issue store credit$/i })
      .click()
    await expect(page.getByText(/€\s?50\.00/).first()).toBeVisible({ timeout: 15_000 })

    // Edit the amount to a comma-decimal 73,45 → must persist as €73.45.
    await page
      .getByRole('row', { name: /50\.00/ })
      .getByRole('button')
      .click()
    await page.getByRole('menuitem', { name: /^edit$/i }).click()
    await expect(page.getByRole('heading', { name: /edit store credit/i })).toBeVisible()

    const amount = page.locator('#edit-sc-amount')
    await expect(amount).toBeEnabled()
    await amount.fill('73,45')
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^save$/i })
      .click()

    await expect(page.getByText(/€\s?73\.45/).first()).toBeVisible({ timeout: 15_000 })
    await expect(page.getByText(/7,345/)).toHaveCount(0)
  })

  test('bulk-assigns selected customers to a group', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const suffix = Date.now()
    const emailA = `e2e-bulk-a-${suffix}@example.com`
    const emailB = `e2e-bulk-b-${suffix}@example.com`
    await createCustomer(page, emailA)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
    await createCustomer(page, emailB)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    // Locate the two rows we just created and tick their checkboxes. The row
    // selection markup is `<input type="checkbox" aria-label="Select row">`
    // scoped to the row whose cell text contains the email.
    for (const email of [emailA, emailB]) {
      await page
        .locator('tr')
        .filter({ hasText: email })
        .getByRole('checkbox', { name: /select row/i })
        .check()
    }

    // The bulk action bar appears once any row is selected.
    await expect(page.getByText(/2 selected/i)).toBeVisible()
    await page.getByRole('button', { name: /add to group/i }).click()

    // Picker sheet opens — pick the seeded group, then confirm.
    await expect(page.getByRole('heading', { name: /add to groups/i })).toBeVisible()
    await page
      .getByRole('dialog')
      .getByPlaceholder(/search customer groups/i)
      .fill(FIXTURE_PROMO_CUSTOMER_GROUP)
    await page
      .getByRole('option', { name: new RegExp(FIXTURE_PROMO_CUSTOMER_GROUP, 'i') })
      .first()
      .click()
    await page.getByRole('dialog').getByRole('button', { name: /^add$/i }).click()

    // After success, the toast confirms the count and the row chips render
    // the group name once the table refetches.
    await expect(page.getByText(/added 2 customers to groups/i)).toBeVisible({ timeout: 15_000 })

    // Visit /customers/groups WITHIN the QueryClient's staleTime window (60s).
    // If the bulk action didn't invalidate the groups cache, the stale
    // snapshot (showing 0 customers in the seeded group) would render here.
    // Regression guard for the TanStack Query cross-resource invalidation bug.
    await page.goto(`/${creds.store_id}/customers/groups`)
    const groupRow = page.locator('tr').filter({ hasText: FIXTURE_PROMO_CUSTOMER_GROUP })
    // The customers_count is column index 1 (0=name, 1=count, trailing kebab
    // doesn't count toward the data columns we care about). Poll until ≥ 2.
    await expect
      .poll(async () => Number((await groupRow.locator('td').nth(1).innerText()).trim()), {
        timeout: 15_000,
      })
      .toBeGreaterThanOrEqual(2)
  })

  test('bulk-adds tags to selected customers', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const suffix = Date.now()
    const emailA = `e2e-bulk-tag-a-${suffix}@example.com`
    const emailB = `e2e-bulk-tag-b-${suffix}@example.com`
    // Unique tag per run so the "already tagged" path doesn't surface on
    // a local re-run of the suite.
    const tagName = `e2e-bulk-${suffix}`

    await createCustomer(page, emailA)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
    await createCustomer(page, emailB)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    for (const email of [emailA, emailB]) {
      await page
        .locator('tr')
        .filter({ hasText: email })
        .getByRole('checkbox', { name: /select row/i })
        .check()
    }

    await expect(page.getByText(/2 selected/i)).toBeVisible()
    await page.getByRole('button', { name: /^add tags…$/i }).click()
    await expect(page.getByRole('heading', { name: /^add tags$/i })).toBeVisible()

    // TagCombobox: type and press Enter to confirm the new tag as a chip,
    // then submit the dialog.
    const tagInput = page.getByRole('dialog').getByPlaceholder(/type to add tags/i)
    await tagInput.fill(tagName)
    await tagInput.press('Enter')
    await expect(page.getByRole('dialog').getByText(tagName)).toBeVisible()

    await page.getByRole('dialog').getByRole('button', { name: /^add$/i }).click()

    await expect(page.getByText(/added tags to 2 customers/i)).toBeVisible({ timeout: 15_000 })

    // The new tag is offered by the list's Tags filter.
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
    await page.getByRole('button', { name: /add filter/i }).click()
    await page
      .locator('[data-slot="filter-panel-item"]')
      .getByText(/^tags$/i)
      .click()
    await expect(page.getByText(tagName, { exact: true })).toBeVisible({ timeout: 15_000 })

    // The customer tag vocabulary offers it back as an existing tag.
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
    await page
      .locator('tr')
      .filter({ hasText: emailA })
      .getByRole('checkbox', { name: /select row/i })
      .check()
    await page.getByRole('button', { name: /^add tags…$/i }).click()
    await page
      .getByRole('dialog')
      .getByPlaceholder(/type to add tags/i)
      .fill(tagName)
    await expect(page.getByRole('option', { name: tagName, exact: true })).toBeVisible({
      timeout: 15_000,
    })
  })

  // The detail page's Customer Groups card edits one customer's membership by
  // diffing the picked set against current membership. The group list is
  // preloaded (no typing required to discover it), mirroring the bulk dialog.
  test('assigns and removes a customer group from the detail page', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-detail-group-${Date.now()}@example.com`
    await createCustomer(page, email)

    // Open the Groups card editor. Scope to the card element itself (not any
    // wrapper div) by matching the `data-slot="card"` whose title is "Customer
    // groups", so the Edit lookup can't stray onto the Profile card's button.
    const groupsCard = page.locator('[data-slot="card"]').filter({
      has: page.getByText('Customer groups', { exact: true }),
    })
    await groupsCard
      .getByRole('button', { name: /^edit$/i })
      .first()
      .click()
    await expect(page.getByRole('heading', { name: /^edit customer groups$/i })).toBeVisible()

    // The seeded group is preloaded — open the picker and select it without
    // having to type a query first.
    await page.getByPlaceholder(/search customer groups/i).click()
    await page
      .getByRole('option', { name: new RegExp(FIXTURE_PROMO_CUSTOMER_GROUP, 'i') })
      .first()
      .click()
    // The selection registers as a chip; close the still-open combobox listbox
    // (Base UI keeps it open + focused after select, overlaying the Save button).
    // Escape closes only the popup — the Sheet stays open.
    await expect(
      page.getByRole('dialog').locator('[data-slot="combobox-chip"]', {
        hasText: FIXTURE_PROMO_CUSTOMER_GROUP,
      }),
    ).toBeVisible()
    await page.keyboard.press('Escape')
    await expect(page.getByRole('listbox')).toBeHidden()
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^save$/i })
      .click()

    // Back on the detail page, the card renders the group as a badge. The group
    // name is unique on this page, so a page-level assertion is unambiguous.
    await expect(page.getByText(FIXTURE_PROMO_CUSTOMER_GROUP, { exact: true })).toBeVisible({
      timeout: 15_000,
    })

    // Re-open and remove it. The selected group renders as a chip whose remove
    // affordance is an icon-only button (no accessible name), so scope to the
    // chip carrying the group name and click its remove control.
    await groupsCard
      .getByRole('button', { name: /^edit$/i })
      .first()
      .click()
    await expect(page.getByRole('heading', { name: /^edit customer groups$/i })).toBeVisible()
    await page
      .getByRole('dialog')
      .locator('[data-slot="combobox-chip"]', { hasText: FIXTURE_PROMO_CUSTOMER_GROUP })
      .locator('[data-slot="combobox-chip-remove"]')
      .click()
    // Removing the chip doesn't open the listbox (no popup overlays Save), so
    // just confirm the chip is gone, then save.
    await expect(
      page.getByRole('dialog').locator('[data-slot="combobox-chip"]', {
        hasText: FIXTURE_PROMO_CUSTOMER_GROUP,
      }),
    ).toHaveCount(0)
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /^save$/i })
      .click()

    await expect(page.getByText(/not in any groups/i)).toBeVisible({ timeout: 15_000 })
  })

  // Erasing is the only destructive action the dashboard offers on a customer.
  // Deleting the row and erasing the person were two buttons that never both
  // applied — deletion is refused once someone has bought anything, which is
  // exactly when a real erasure request arrives.
  test('erases a customer and says so afterwards', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-erase-${Date.now()}@example.com`
    await createCustomer(page, email)

    await page.getByRole('button', { name: /erase personal data/i }).click()
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /erase personal data/i })
      .click()

    // The page stays — the record survives an erasure, its identity does not.
    await expect(page.getByText(/^erased$/i).first()).toBeVisible({ timeout: 15_000 })
    await expect(page.getByRole('heading', { name: email })).toHaveCount(0)
  })

  test('keeps erased customers out of the list until asked for', async ({ page }) => {
    const creds = await login(page)
    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)

    const email = `e2e-hidden-${Date.now()}@example.com`
    await createCustomer(page, email)

    await page.getByRole('button', { name: /erase personal data/i }).click()
    await page
      .getByRole('dialog')
      .getByRole('button', { name: /erase personal data/i })
      .click()
    await expect(page.getByText(/^erased$/i).first()).toBeVisible({ timeout: 15_000 })

    await gotoIndex(page, CUSTOMERS_PATH(creds.store_id), CTA)
    await expect(page.getByRole('link', { name: email })).toHaveCount(0, { timeout: 15_000 })

    // Scoped to the panel's list rows: once a field is picked, its name also
    // appears on the header button that goes back to the field list, so a bare
    // name match finds both. The field and its `true` value are both "Erased".
    await page.getByRole('button', { name: /add filter/i }).click()
    const filterOption = page.locator('[data-slot="filter-panel-item"]')
    await filterOption.getByText(/^erased$/i).click()
    await filterOption.getByText(/^erased$/i).click()

    await expect(page.getByText('(erased)').first()).toBeVisible({ timeout: 15_000 })
  })
})