import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import KsDataGrid from '../KsDataGrid.vue'

const gridHarness = {
  stubs: {
    AgGridVue: {
      props: ['rowData', 'columnDefs', 'rowSelection', 'loading'],
      template: '<button data-test="grid" @click="$emit(\'row-clicked\', { data: rowData[0] })">select</button>',
    },
  },
}

describe('KsDataGrid contract', () => {
  it('preserves grid defaults and native grid configuration', () => {
    const rows = [{ id: 'row-1' }]
    const columns = [{ field: 'id', header: 'ID' }]
    const wrapper = mount(KsDataGrid, {
      props: { rows, columns },
      global: gridHarness,
    })

    expect(wrapper.find('[data-test="grid"]').exists()).toBe(true)
    expect(wrapper.html()).toContain('grid')
  })

  it('forwards the selected row without changing its identity', async () => {
    const row = { id: 'row-1', status: 'READY' }
    const wrapper = mount(KsDataGrid, {
      props: { rows: [row], columns: [] },
      global: gridHarness,
    })

    await wrapper.get('[data-test="grid"]').trigger('click')
    expect(wrapper.emitted('rowSelected')).toEqual([[row]])
  })
})
