1
  L  ˆl–ßi~”Šj"Tâ‚Æ[¸ØÅ£�þY–6Vq¹¡Ž8ÄáŸö„Œ�ßõ_“I|¥ŽõHºl=$¤�zTÖ™	Àô ?³   // Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { h, ref } from 'vue'

import { renderComponent } from '#tests/support/components/index.ts'
import type { ExtendedRenderResult } from '#tests/support/components/index.ts'

import type { SelectOption } from '#shared/components/CommonSelect/types.ts'
import { i18n } from '#shared/i18n.ts'

import CommonSelect, { type Props } from '../CommonSelect.vue'

import type { ConcreteComponent, Ref } from 'vue'

const options = [
  {
    value: 0,
    label: 'Item A',
  },
  {
    value: 1,
    label: 'Item B',
  },
  {
    value: 2,
    label: 'Item C',
  },
]

const html = String.raw

// A functional component, so that it survives the deep cloning of the render options.
const CustomSelectItem = (
  props: { option: SelectOption; selected?: boolean },
  { emit }: { emit: (event: 'select', option: SelectOption) => void },
) =>
  h(
    'button',
    {
      role: 'option',
      tabindex: '0',
      'aria-selected': props.selected ?? false,
      'data-test-id': 'custom-select-item',
      onClick: () => emit('select', props.option),
    },
    props.option.label,
  )

CustomSelectItem.props = {
  option: { type: Object, required: true },
  selected: { type: Boolean, default: false },
}
CustomSelectItem.emits = ['select']

const optionComponent = CustomSelectItem as unknown as ConcreteComponent

const renderSelect = (props: Props, modelValue?: Ref) => {
  return renderComponent(CommonSelect, {
    props: {
      isTargetVisible: true,
      ...props,
    },
    slots: {
      default: html`
        <template #default="{ open, focus }">
          <button @click="open()">Open Select</button>
          <button @click="focus()">Move Focus</button>
        </template>
      `,
    },
    vModel: {
      modelValue,
    },
  })
}

beforeEach(() => {
  i18n.setTranslationMap(new Map([]))
})

describe('CommonSelect.vue', () => {
  it('can select and unselect value', async () => {
    const modelValue = ref()
    const view = renderSelect({ options }, modelValue)

    await view.events.click(view.getByText('Open Select'))

    expect(view.queryByRole('menu')).toBeInTheDocument()

    await view.events.click(view.getByText('Item A'))

    expect(view.emitted().select).toEqual([[options[0]]])
    expect(view.queryByRole('menu')).not.toBeInTheDocument()
    expect(modelValue.value).toBe(0)

    await view.events.click(view.getByText('Open Select'))

    expect(
      view.getByIconName((name, node) => {
        return name === '#icon-check2' && !node?.parentElement?.classList.contains('invisible')
      }),
    ).toBeInTheDocument()

    await view.events.click(view.getByText('Item A'))

    expect(view.emitted().select).toEqual([[options[0]], [options[0]]])
    expect(modelValue.value).toBe(undefined)
  })

  it('does not close select with noClose prop', async () => {
    const view = renderSelect({ options, noClose: true })

    await view.events.click(view.getByText('Open Select'))
    await view.events.click(view.getByRole('option', { name: 'Item A' }))

    expect(view.getByRole('menu')).toBeInTheDocument()
  })

  it('can select and unselect multiple values', async () => {
    const modelValue = ref()
    const view = renderSelect({ options, multiple: true }, modelValue)

    await view.events.click(view.getByText('Open Select'))
    await view.events.click(view.getByText('Item A'))

    expect(modelValue.value).toEqual([0])

    expect(view.queryAllByIconName('check-square')).toHaveLength(1)
    await view.events.click(view.getByText('Item A'))

    expect(modelValue.value).toEqual([])

    await view.events.click(view.getByText('Item A'))
    await view.events.click(view.getByText('Item B'))

    expect(modelValue.value).toEqual([0, 1])

    expect(view.queryAllByIconName('check-square')).toHaveLength(2)
  })

  it('can use select all action with active multiple', async () => {
    const modelValue = ref()
    const view = renderSelect({ options, multiple: tr