import { HelperService } from './helper.service'

describe('HelperService - convertArrayValues', () => {
  let service: HelperService

  beforeEach(() => {
    // convertArrayValues does not touch any injected dependency
    service = new HelperService(null as any)
  })

  it('returns an empty array for empty input', () => {
    expect(service.convertArrayValues([], 'string')).toEqual([])
    expect(service.convertArrayValues([], 'number')).toEqual([])
  })

  it('coerces values to numbers for the numeric path', () => {
    expect(service.convertArrayValues(['1', '2', '3'], 'number')).toEqual([
      1, 2, 3
    ])
  })

  it('coerces values to strings for the string path', () => {
    expect(service.convertArrayValues([1, 2, 3], 'string')).toEqual([
      '1',
      '2',
      '3'
    ])
  })

  // Regression: a RAW_VALUE / dropdown source value that itself contains a
  // comma must stay a single item. Previously `array.toString().split(',')`
  // joined the array and re-split on every comma, shattering one value into
  // several. Same bug that hit display_value lists.
  it('keeps a single value containing commas intact (string path)', () => {
    expect(service.convertArrayValues(['Smith, John'], 'string')).toEqual([
      'Smith, John'
    ])
  })

  it('does not split multiple comma-containing values into individual items', () => {
    const source = ['Smith, John', 'Doe, Jane', 'Plain']

    const result = service.convertArrayValues(source, 'string')

    expect(result).toEqual(['Smith, John', 'Doe, Jane', 'Plain'])
    expect(result.length).toBe(3)
  })
})
