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.3 KB
Raw Blame
import { transformListParams } from '@spree/sdk-core'
import { describe, expect, it } from 'vitest'

describe('transformListParams', () => {
  it('sends the entries of q as filters', () => {
    const result = transformListParams({
      name_cont: 'shirt',
      q: { erp_id_eq: 'ERP-1', tags_in: ['a', 'b'] },
    })
    expect(result).toEqual({
      'q[erp_id_eq]': 'ERP-1',
      'q[tags_in][]': ['a', 'b'],
      'q[name_cont]': 'shirt',
    })
  })

  it('keeps every entry of q inside q, nested objects included', () => {
    const result = transformListParams({
      page: 2,
      q: { page: 3, sort: 'name', g: { '0': { name_cont: 'shirt' } } },
    })
    expect(result).toEqual({
      page: 2,
      'q[page]': 3,
      'q[sort]': 'name',
      'q[g][0][name_cont]': 'shirt',
    })
  })

  it('prefers a typed filter over the same key in q', () => {
    const result = transformListParams({ name_cont: 'typed', q: { name_cont: 'raw' } })
    expect(result).toEqual({ 'q[name_cont]': 'typed' })
  })

  it('passes through pagination params unchanged', () => {
    const result = transformListParams({ page: 2, limit: 10 })
    expect(result).toEqual({ page: 2, limit: 10 })
  })

  it('joins expand array into comma-separated string', () => {
    const result = transformListParams({ expand: ['variants', 'media'] })
    expect(result).toEqual({ expand: 'variants,media' })
  })

  it('joins single-element expand array', () => {
    const result = transformListParams({ expand: ['variants'] })
    expect(result).toEqual({ expand: 'variants' })
  })

  it('handles empty expand array', () => {
    const result = transformListParams({ expand: [] })
    expect(result).toEqual({ expand: '' })
  })

  it('passes through descending sort param unchanged', () => {
    const result = transformListParams({ sort: '-price' })
    expect(result).toEqual({ sort: '-price' })
  })

  it('passes through ascending sort param unchanged', () => {
    const result = transformListParams({ sort: 'name' })
    expect(result).toEqual({ sort: 'name' })
  })

  it('wraps filter keys in q[...]', () => {
    const result = transformListParams({
      name_cont: 'shirt',
      price_gte: 20,
      price_lte: 100,
    })
    expect(result).toEqual({
      'q[name_cont]': 'shirt',
      'q[price_gte]': 20,
      'q[price_lte]': 100,
    })
  })

  it('transforms search', () => {
    const result = transformListParams({ search: 'shirt' })
    expect(result).toEqual({ 'q[search]': 'shirt' })
  })

  it('passes through already-wrapped q[...] keys (backward compat)', () => {
    const result = transformListParams({ 'q[name_cont]': 'shirt' })
    expect(result).toEqual({ 'q[name_cont]': 'shirt' })
  })

  it('skips undefined values', () => {
    const result = transformListParams({ name_cont: undefined, page: 1 })
    expect(result).toEqual({ page: 1 })
  })

  it('wraps array values with [] suffix automatically', () => {
    const result = transformListParams({
      with_option_value_ids: ['optval_abc', 'optval_def'],
    })
    expect(result).toEqual({
      'q[with_option_value_ids][]': ['optval_abc', 'optval_def'],
    })
  })

  it('handles array values even when key already has [] suffix', () => {
    const result = transformListParams({
      'with_option_value_ids[]': ['optval_abc'],
    })
    expect(result).toEqual({
      'q[with_option_value_ids][]': ['optval_abc'],
    })
  })

  it('joins fields array into comma-separated string', () => {
    const result = transformListParams({ fields: ['name', 'slug', 'price'] })
    expect(result).toEqual({ fields: 'name,slug,price' })
  })

  it('passes fields through without wrapping in q[...]', () => {
    const result = transformListParams({ fields: ['name', 'slug'], name_cont: 'shirt' })
    expect(result).toEqual({
      fields: 'name,slug',
      'q[name_cont]': 'shirt',
    })
  })

  it('handles a full combined query', () => {
    const result = transformListParams({
      page: 1,
      limit: 12,
      expand: ['media', 'default_variant'],
      sort: '-created_at',
      name_cont: 'shirt',
      price_gte: 20,
      in_category: 'ctg_abc123',
    })
    expect(result).toEqual({
      page: 1,
      limit: 12,
      expand: 'media,default_variant',
      sort: '-created_at',
      'q[name_cont]': 'shirt',
      'q[price_gte]': 20,
      'q[in_category]': 'ctg_abc123',
    })
  })
})