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.

3.7 KB
Raw Blame
import { beforeAll, describe, expect, it } from 'vitest'
import type { Client } from '../src'
import { createTestClient } from './helpers'
import { fixtures } from './mocks/handlers'

describe('products', () => {
  let client: Client
  beforeAll(() => {
    client = createTestClient()
  })

  describe('list', () => {
    it('returns paginated products', async () => {
      const result = await client.products.list()

      expect(result.data).toHaveLength(1)
      expect(result.data[0].name).toBe('Test Product')
      expect(result.meta.page).toBe(1)
      expect(result.meta.count).toBe(1)
    })

    it('passes query parameters', async () => {
      const result = await client.products.list({
        page: 2,
        limit: 10,
        name_cont: 'shirt',
      })

      // MSW handler returns the same fixture regardless of params,
      // but the request is formed correctly (tested via custom fetch in client tests)
      expect(result.data).toBeDefined()
    })
  })

  describe('get', () => {
    it('returns a product by slug', async () => {
      const result = await client.products.get('test-product')

      expect(result.name).toBe(fixtures.product.name)
      expect(result.slug).toBe('test-product')
    })

    it('returns a product by ID', async () => {
      const result = await client.products.get('prod_1')
      expect(result.id).toBe('prod_1')
    })

    it('sends expand array as comma-separated query param', async () => {
      let capturedUrl = ''
      const { createClient } = await import('../src')
      const customClient = createClient({
        baseUrl: 'https://demo.spreecommerce.org',
        publishableKey: 'test-key',
        fetch: async (url, init) => {
          capturedUrl = url.toString()
          return globalThis.fetch(url, init)
        },
      })

      await customClient.products.get('test-product', {
        expand: ['variants', 'media'],
      })

      expect(capturedUrl).toContain('expand=variants%2Cmedia')
    })

    it('omits expand param when array is empty', async () => {
      let capturedUrl = ''
      const { createClient } = await import('../src')
      const customClient = createClient({
        baseUrl: 'https://demo.spreecommerce.org',
        publishableKey: 'test-key',
        fetch: async (url, init) => {
          capturedUrl = url.toString()
          return globalThis.fetch(url, init)
        },
      })

      await customClient.products.get('test-product', { expand: [] })

      expect(capturedUrl).not.toContain('expand')
    })
  })

  describe('filters', () => {
    it('returns filter options', async () => {
      const result = await client.products.filters()

      expect(result.filters).toBeDefined()
      expect(result.sort_options).toBeDefined()
      expect(result.default_sort).toBe('default')
      expect(result.total_count).toBe(1)
    })

    it('sends the category at the top level and the filters inside q', async () => {
      let capturedUrl = ''
      const { createClient } = await import('../src')
      const customClient = createClient({
        baseUrl: 'https://demo.spreecommerce.org',
        publishableKey: 'test-key',
        fetch: async (url, init) => {
          capturedUrl = decodeURIComponent(url.toString())
          return globalThis.fetch(url, init)
        },
      })

      await customClient.products.filters({
        category_id: 'ctg_1',
        q: { price_gte: '10.00', with_option_value_ids: ['optval_1'] },
      })

      expect(capturedUrl).toContain('category_id=ctg_1')
      expect(capturedUrl).toContain('q[price_gte]=10.00')
      expect(capturedUrl).toContain('q[with_option_value_ids][]=optval_1')
      expect(capturedUrl).not.toContain('[object Object]')
    })
  })
})