// @vitest-environment jsdom
import { describe, it, expect, vi } from 'vitest'
import { useDialogSubmitShortcut } from '@/components/shared/use-dialog-submit-shortcut'

function makeEvent(opts: Partial<KeyboardEvent>) {
  return {
    metaKey: false,
    ctrlKey: false,
    key: '',
    preventDefault: vi.fn(),
    ...opts,
  } as unknown as React.KeyboardEvent
}

describe('useDialogSubmitShortcut', () => {
  it('triggert submit op Cmd+Enter', () => {
    const submit = vi.fn()
    const handler = useDialogSubmitShortcut(submit)
    const e = makeEvent({ metaKey: true, key: 'Enter' })

    handler(e)

    expect(submit).toHaveBeenCalledTimes(1)
    expect(e.preventDefault).toHaveBeenCalled()
  })

  it('triggert submit op Ctrl+Enter', () => {
    const submit = vi.fn()
    const handler = useDialogSubmitShortcut(submit)
    const e = makeEvent({ ctrlKey: true, key: 'Enter' })

    handler(e)

    expect(submit).toHaveBeenCalledTimes(1)
  })

  it('triggert NIET op Enter zonder modifier', () => {
    const submit = vi.fn()
    const handler = useDialogSubmitShortcut(submit)
    const e = makeEvent({ key: 'Enter' })

    handler(e)

    expect(submit).not.toHaveBeenCalled()
    expect(e.preventDefault).not.toHaveBeenCalled()
  })

  it('triggert NIET op Cmd+andere toets', () => {
    const submit = vi.fn()
    const handler = useDialogSubmitShortcut(submit)
    const e = makeEvent({ metaKey: true, key: 'a' })

    handler(e)

    expect(submit).not.toHaveBeenCalled()
  })
})
