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.

4.8 KB
Raw Blame
import { HttpResponse, http } from 'msw'
import { beforeAll, describe, expect, it } from 'vitest'
import type { Client } from '../src'
import { createTestClient, TEST_BASE_URL } from './helpers'
import { fixtures } from './mocks/handlers'
import { server } from './mocks/server'

const API_PREFIX = `${TEST_BASE_URL}/api/v3/store`

describe('customer', () => {
  let client: Client
  beforeAll(() => {
    client = createTestClient()
  })
  const opts = { token: 'user-jwt' }

  describe('get', () => {
    it('returns customer profile', async () => {
      const result = await client.customer.get(opts)
      expect(result.email).toBe('[email protected]')
    })
  })

  describe('update', () => {
    it('updates customer profile', async () => {
      const result = await client.customer.update({ first_name: 'Updated' }, opts)
      expect(result.first_name).toBe('Updated')
    })

    it('sends phone, accepts_email_marketing, and metadata', async () => {
      let capturedBody: Record<string, unknown> = {}
      server.use(
        http.patch(`${API_PREFIX}/customers/me`, async ({ request }) => {
          capturedBody = (await request.json()) as Record<string, unknown>
          return HttpResponse.json({
            ...fixtures.user,
            phone: '+1234567890',
            accepts_email_marketing: true,
          })
        }),
      )

      const result = await client.customer.update(
        { phone: '+1234567890', accepts_email_marketing: true, metadata: { source: 'app' } },
        opts,
      )

      expect(capturedBody.phone).toBe('+1234567890')
      expect(capturedBody.accepts_email_marketing).toBe(true)
      expect(capturedBody.metadata).toEqual({ source: 'app' })
      expect(result.phone).toBe('+1234567890')
      expect(result.accepts_email_marketing).toBe(true)
    })
  })

  describe('addresses', () => {
    it('lists addresses', async () => {
      const result = await client.customer.addresses.list(undefined, opts)
      expect(result.data).toHaveLength(1)
    })

    it('gets an address by ID', async () => {
      const result = await client.customer.addresses.get('addr_1', opts)
      expect(result.city).toBe('New York')
    })

    it('creates an address', async () => {
      const result = await client.customer.addresses.create(
        {
          first_name: 'Test',
          last_name: 'User',
          address1: '123 Main St',
          city: 'New York',
          postal_code: '10001',
          country_code: 'US',
        },
        opts,
      )
      expect(result.id).toBeDefined()
    })

    it('updates an address', async () => {
      const result = await client.customer.addresses.update(
        'addr_1',
        { city: 'Updated City' },
        opts,
      )
      expect(result.city).toBe('Updated City')
    })

    it('deletes an address', async () => {
      await expect(client.customer.addresses.delete('addr_1', opts)).resolves.toBeUndefined()
    })
  })

  describe('creditCards', () => {
    it('lists credit cards', async () => {
      const result = await client.customer.creditCards.list(undefined, opts)
      expect(result.data).toBeDefined()
    })

    it('gets a credit card', async () => {
      const result = await client.customer.creditCards.get('cc_1', opts)
      expect(result).toBeDefined()
    })

    it('deletes a credit card', async () => {
      await expect(client.customer.creditCards.delete('cc_1', opts)).resolves.toBeUndefined()
    })
  })

  describe('orders', () => {
    it('lists customer orders', async () => {
      const result = await client.customer.orders.list(undefined, opts)
      expect(result.data).toHaveLength(1)
      expect(result.meta.page).toBe(1)
    })

    it('gets a customer order by ID', async () => {
      const result = await client.customer.orders.get('or_abc123', undefined, opts)
      expect(result.id).toBe('or_1')
    })
  })

  describe('giftCards', () => {
    it('lists gift cards', async () => {
      const result = await client.customer.giftCards.list(undefined, opts)
      expect(result.data).toBeDefined()
    })

    it('gets a gift card', async () => {
      const result = await client.customer.giftCards.get('gc_1', opts)
      expect(result).toBeDefined()
    })
  })

  describe('storeCredits', () => {
    it('lists store credits', async () => {
      const result = await client.customer.storeCredits.list(undefined, opts)
      expect(result.data).toHaveLength(1)
      expect(result.data[0].id).toBe('credit_1')
      expect(result.data[0].amount).toBe('100.00')
      expect(result.data[0].amount_remaining).toBe('75.00')
      expect(result.data[0].currency).toBe('USD')
    })

    it('gets a store credit by ID', async () => {
      const result = await client.customer.storeCredits.get('credit_1', opts)
      expect(result.id).toBe('credit_1')
      expect(result.amount).toBe('100.00')
    })
  })
})