import { render } from "@opensea/testing-utils/render"
import { act, screen, waitFor } from "@testing-library/react"
import { userEvent } from "@testing-library/user-event"
import React from "react"
import { beforeEach, describe, expect, test, vi } from "vitest"
import { AppRouter } from "../../tests/AppRouter"
import { Item } from "./Item"

describe("<Item />", () => {
  beforeEach(() => {
    Object.defineProperty(window, "location", {
      writable: true,
      value: { ...window.location, assign: vi.fn() },
    })
  })

  test("onNavigationCompleted", async () => {
    const onNavigationCompleted = vi.fn()
    render(
      <Item href="https://opensea.io">
        <Item
          href="/collection/azuki"
          onNavigationCompleted={onNavigationCompleted}
        >
          Click me
        </Item>
      </Item>,
      { wrapper: ({ children }) => <AppRouter>{children}</AppRouter> },
    )

    const [_, innerLink] = screen.getAllByRole("link", {
      name: "Click me",
    })

    await act(async () => {
      await userEvent.click(innerLink)
    })

    await waitFor(() => {
      const args = onNavigationCompleted.mock.calls[0]
      expect(args[0]).toBeTypeOf("number")
      expect(args[0]).toBeGreaterThan(0)
      expect(args[1]).toBeTypeOf("number")
      expect(args[1]).toBeGreaterThan(0)
    })
  })
})
