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.

1.3 KB
Raw Blame
import { beforeEach, describe, expect, it } from 'vitest'
import {
  __resetCustomFieldComponents,
  type CustomFieldComponent,
  customFieldComponents,
} from '../src/lib/custom-field-components'

const FakeComponent: CustomFieldComponent = () => null
const OtherComponent: CustomFieldComponent = () => null

describe('customFieldComponents registry', () => {
  beforeEach(() => {
    __resetCustomFieldComponents()
  })

  it('registers and resolves by namespace.key', () => {
    customFieldComponents.register('specs.color', FakeComponent)
    expect(customFieldComponents.get('specs.color')).toBe(FakeComponent)
  })

  it('returns undefined for unregistered definitions (default widget renders)', () => {
    expect(customFieldComponents.get('specs.unknown')).toBeUndefined()
  })

  it('throws on duplicate registration', () => {
    customFieldComponents.register('specs.color', FakeComponent)
    expect(() => customFieldComponents.register('specs.color', OtherComponent)).toThrow(
      /already registered/,
    )
  })

  it('remove() unregisters and is a no-op when absent', () => {
    customFieldComponents.register('specs.color', FakeComponent)
    customFieldComponents.remove('specs.color')
    expect(customFieldComponents.get('specs.color')).toBeUndefined()
    expect(() => customFieldComponents.remove('specs.color')).not.toThrow()
  })
})