// @vitest-environment jsdom

import { fireEvent, render } from "@testing-library/react"
import { describe, expect, it, vi } from "vitest"
import {
  getNavigationCommandProps,
  NavigationActionTarget,
  navigationActions,
} from "./command-actions"

describe("navigation command", () => {
  it("connects an action button to its command target", () => {
    expect(getNavigationCommandProps(navigationActions.search)).toEqual({
      command: "--invoke",
      commandfor: "navigation-action-search",
    })
  })

  it("invokes the matching command target", () => {
    const onInvoke = vi.fn()
    const { container } = render(
      <NavigationActionTarget
        action={navigationActions.search}
        onInvoke={onInvoke}
      />
    )
    const target = container.querySelector("#navigation-action-search")
    const event = new Event("command")
    Object.defineProperty(event, "command", { value: "--invoke" })

    fireEvent(target!, event)

    expect(onInvoke).toHaveBeenCalledOnce()
  })
})
