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.

11.7 KB
Raw Blame
import { HttpResponse, http } from 'msw'
import { describe, expect, it } from 'vitest'
import { API_PREFIX, createTestClient, paginated } from './helpers'
import { server } from './mocks/server'

const sampleStockLocation = {
  id: 'sloc_abc123',
  name: 'Main Warehouse',
  default: true,
  active: true,
  created_at: '2026-05-01T00:00:00Z',
  updated_at: '2026-05-01T00:00:00Z',
}

const sampleStockLevel = {
  id: 'sl_abc123',
  count_on_hand: 42,
  backorderable: false,
  variant_id: 'variant_1',
  stock_location_id: 'sloc_abc123',
  created_at: '2026-05-01T00:00:00Z',
  updated_at: '2026-05-01T00:00:00Z',
}

const sampleStockMovement = {
  id: 'sm_abc123',
  quantity: 3,
  kind: 'allocated',
  reason: null,
  stock_level_id: 'sl_abc123',
  order_id: 'or_abc123',
  fulfillment_id: 'ful_abc123',
  return_id: null,
  exchange_id: null,
  stock_transfer_id: null,
  created_at: '2026-05-01T00:00:00Z',
  updated_at: '2026-05-01T00:00:00Z',
}

const sampleStockTransfer = {
  id: 'st_abc123',
  number: 'T123456789',
  source_location_id: 'sloc_abc123',
  destination_location_id: 'sloc_def456',
  created_at: '2026-05-01T00:00:00Z',
  updated_at: '2026-05-01T00:00:00Z',
}

describe('stockLocations', () => {
  describe('list', () => {
    it('GETs /stock_locations and returns paginated data', async () => {
      server.use(
        http.get(`${API_PREFIX}/stock_locations`, () =>
          HttpResponse.json(paginated([sampleStockLocation])),
        ),
      )

      const res = await createTestClient().stockLocations.list()

      expect(res.data).toHaveLength(1)
      expect(res.data[0]?.id).toBe('sloc_abc123')
    })

    it('wraps Ransack predicates via transformListParams', async () => {
      let url: URL | null = null
      server.use(
        http.get(`${API_PREFIX}/stock_locations`, ({ request }) => {
          url = new URL(request.url)
          return HttpResponse.json(paginated([]))
        }),
      )

      await createTestClient().stockLocations.list({ name_cont: 'warehouse', active_eq: true })

      expect(url!.searchParams.get('q[name_cont]')).toBe('warehouse')
      expect(url!.searchParams.get('q[active_eq]')).toBe('true')
    })
  })

  describe('get', () => {
    it('GETs /stock_locations/:id and forwards expand params', async () => {
      let url: URL | null = null
      server.use(
        http.get(`${API_PREFIX}/stock_locations/sloc_abc123`, ({ request }) => {
          url = new URL(request.url)
          return HttpResponse.json(sampleStockLocation)
        }),
      )

      const res = await createTestClient().stockLocations.get('sloc_abc123', {
        expand: ['stock_levels'],
      })

      expect(res.id).toBe('sloc_abc123')
      expect(url!.searchParams.get('expand')).toBe('stock_levels')
    })
  })

  describe('create / update / delete', () => {
    it('POSTs the create body verbatim', async () => {
      let body: Record<string, unknown> | null = null
      server.use(
        http.post(`${API_PREFIX}/stock_locations`, async ({ request }) => {
          body = (await request.json()) as Record<string, unknown>
          return HttpResponse.json(sampleStockLocation, { status: 201 })
        }),
      )

      await createTestClient().stockLocations.create({ name: 'Main Warehouse', default: true })

      expect(body).toEqual({ name: 'Main Warehouse', default: true })
    })

    it('PATCHes the update body verbatim', async () => {
      let body: Record<string, unknown> | null = null
      server.use(
        http.patch(`${API_PREFIX}/stock_locations/sloc_abc123`, async ({ request }) => {
          body = (await request.json()) as Record<string, unknown>
          return HttpResponse.json({ ...sampleStockLocation, active: false })
        }),
      )

      const res = await createTestClient().stockLocations.update('sloc_abc123', { active: false })

      expect(body).toEqual({ active: false })
      expect(res.active).toBe(false)
    })

    it('DELETEs /stock_locations/:id', async () => {
      let hit = false
      server.use(
        http.delete(`${API_PREFIX}/stock_locations/sloc_abc123`, () => {
          hit = true
          return new HttpResponse(null, { status: 204 })
        }),
      )

      await expect(createTestClient().stockLocations.delete('sloc_abc123')).resolves.toBeUndefined()
      expect(hit).toBe(true)
    })
  })
})

describe('stockLevels', () => {
  describe('list', () => {
    it('GETs /stock_levels and returns paginated data', async () => {
      server.use(
        http.get(`${API_PREFIX}/stock_levels`, () =>
          HttpResponse.json(paginated([sampleStockLevel])),
        ),
      )

      const res = await createTestClient().stockLevels.list()

      expect(res.data).toHaveLength(1)
      expect(res.data[0]?.id).toBe('sl_abc123')
    })

    it('wraps Ransack predicates via transformListParams', async () => {
      let url: URL | null = null
      server.use(
        http.get(`${API_PREFIX}/stock_levels`, ({ request }) => {
          url = new URL(request.url)
          return HttpResponse.json(paginated([]))
        }),
      )

      await createTestClient().stockLevels.list({
        stock_location_id_eq: 'sloc_abc123',
        variant_id_eq: 'variant_1',
      })

      expect(url!.searchParams.get('q[stock_location_id_eq]')).toBe('sloc_abc123')
      expect(url!.searchParams.get('q[variant_id_eq]')).toBe('variant_1')
    })
  })

  describe('get', () => {
    it('GETs /stock_levels/:id and forwards expand params', async () => {
      let url: URL | null = null
      server.use(
        http.get(`${API_PREFIX}/stock_levels/sl_abc123`, ({ request }) => {
          url = new URL(request.url)
          return HttpResponse.json(sampleStockLevel)
        }),
      )

      const res = await createTestClient().stockLevels.get('sl_abc123', { expand: ['variant'] })

      expect(res.id).toBe('sl_abc123')
      expect(url!.searchParams.get('expand')).toBe('variant')
    })
  })

  describe('update / delete', () => {
    it('PATCHes the update body verbatim', async () => {
      let body: Record<string, unknown> | null = null
      server.use(
        http.patch(`${API_PREFIX}/stock_levels/sl_abc123`, async ({ request }) => {
          body = (await request.json()) as Record<string, unknown>
          return HttpResponse.json({ ...sampleStockLevel, count_on_hand: 100 })
        }),
      )

      const res = await createTestClient().stockLevels.update('sl_abc123', { count_on_hand: 100 })

      expect(body).toEqual({ count_on_hand: 100 })
      expect(res.count_on_hand).toBe(100)
    })

    it('DELETEs /stock_levels/:id', async () => {
      let hit = false
      server.use(
        http.delete(`${API_PREFIX}/stock_levels/sl_abc123`, () => {
          hit = true
          return new HttpResponse(null, { status: 204 })
        }),
      )

      await expect(createTestClient().stockLevels.delete('sl_abc123')).resolves.toBeUndefined()
      expect(hit).toBe(true)
    })
  })
})

describe('stockTransfers', () => {
  describe('list', () => {
    it('GETs /stock_transfers and returns paginated data', async () => {
      server.use(
        http.get(`${API_PREFIX}/stock_transfers`, () =>
          HttpResponse.json(paginated([sampleStockTransfer])),
        ),
      )

      const res = await createTestClient().stockTransfers.list()

      expect(res.data).toHaveLength(1)
      expect(res.data[0]?.id).toBe('st_abc123')
    })

    it('wraps Ransack predicates via transformListParams', async () => {
      let url: URL | null = null
      server.use(
        http.get(`${API_PREFIX}/stock_transfers`, ({ request }) => {
          url = new URL(request.url)
          return HttpResponse.json(paginated([]))
        }),
      )

      await createTestClient().stockTransfers.list({
        number_cont: 'T123',
        source_location_id_eq: 'sloc_abc123',
      })

      expect(url!.searchParams.get('q[number_cont]')).toBe('T123')
      expect(url!.searchParams.get('q[source_location_id_eq]')).toBe('sloc_abc123')
    })
  })

  describe('get', () => {
    it('GETs /stock_transfers/:id and forwards expand params', async () => {
      let url: URL | null = null
      server.use(
        http.get(`${API_PREFIX}/stock_transfers/st_abc123`, ({ request }) => {
          url = new URL(request.url)
          return HttpResponse.json(sampleStockTransfer)
        }),
      )

      const res = await createTestClient().stockTransfers.get('st_abc123', {
        expand: ['stock_movements'],
      })

      expect(res.id).toBe('st_abc123')
      expect(url!.searchParams.get('expand')).toBe('stock_movements')
    })
  })

  describe('create / delete', () => {
    it('POSTs the create body verbatim', async () => {
      let body: Record<string, unknown> | null = null
      server.use(
        http.post(`${API_PREFIX}/stock_transfers`, async ({ request }) => {
          body = (await request.json()) as Record<string, unknown>
          return HttpResponse.json(sampleStockTransfer, { status: 201 })
        }),
      )

      await createTestClient().stockTransfers.create({
        source_location_id: 'sloc_abc123',
        destination_location_id: 'sloc_def456',
      })

      expect(body).toEqual({
        source_location_id: 'sloc_abc123',
        destination_location_id: 'sloc_def456',
      })
    })

    it('DELETEs /stock_transfers/:id', async () => {
      let hit = false
      server.use(
        http.delete(`${API_PREFIX}/stock_transfers/st_abc123`, () => {
          hit = true
          return new HttpResponse(null, { status: 204 })
        }),
      )

      await expect(createTestClient().stockTransfers.delete('st_abc123')).resolves.toBeUndefined()
      expect(hit).toBe(true)
    })
  })
})

describe('stockMovements', () => {
  describe('list', () => {
    it('GETs /stock_movements and returns paginated data', async () => {
      server.use(
        http.get(`${API_PREFIX}/stock_movements`, () =>
          HttpResponse.json(paginated([sampleStockMovement])),
        ),
      )

      const res = await createTestClient().stockMovements.list()

      expect(res.data).toHaveLength(1)
      expect(res.data[0]?.kind).toBe('allocated')
      expect(res.data[0]?.fulfillment_id).toBe('ful_abc123')
    })

    it('wraps Ransack predicates via transformListParams', async () => {
      let url: URL | null = null
      server.use(
        http.get(`${API_PREFIX}/stock_movements`, ({ request }) => {
          url = new URL(request.url)
          return HttpResponse.json(paginated([]))
        }),
      )

      await createTestClient().stockMovements.list({ kind_eq: 'shipped', order_id_eq: 'or_abc123' })

      expect(url!.searchParams.get('q[kind_eq]')).toBe('shipped')
      expect(url!.searchParams.get('q[order_id_eq]')).toBe('or_abc123')
    })
  })

  describe('get', () => {
    it('GETs /stock_movements/:id', async () => {
      server.use(
        http.get(`${API_PREFIX}/stock_movements/sm_abc123`, () =>
          HttpResponse.json(sampleStockMovement),
        ),
      )

      const res = await createTestClient().stockMovements.get('sm_abc123')

      expect(res.id).toBe('sm_abc123')
    })
  })
})

describe('stockReservations', () => {
  describe('list', () => {
    it('GETs /stock_reservations with Ransack-wrapped filters', async () => {
      let url: URL | null = null
      server.use(
        http.get(`${API_PREFIX}/stock_reservations`, ({ request }) => {
          url = new URL(request.url)
          return HttpResponse.json(
            paginated([{ id: 'res_abc123', quantity: 2, stock_level_id: 'sl_abc123' }]),
          )
        }),
      )

      const res = await createTestClient().stockReservations.list({
        expires_at_lt: '2026-05-01T00:00:00Z',
        sort: 'expires_at',
      })

      expect(res.data[0]?.id).toBe('res_abc123')
      expect(url!.searchParams.get('q[expires_at_lt]')).toBe('2026-05-01T00:00:00Z')
      expect(url!.searchParams.get('sort')).toBe('expires_at')
    })
  })
})