import { render, screen } from '@testing-library/react'

import { useIntegrationServiceListIntegrations2 } from 'dbaas/services/metricsIntegration'

import { IntegrationList } from './IntegrationList'

jest.mock('@tidbcloud/uikit', () => ({
  Center: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  CloseButton: () => <button />,
  Input: () => <input />,
  Stack: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  Typography: ({ children }: { children: React.ReactNode }) => <div>{children}</div>
}))

jest.mock('@tidbcloud/uikit/icons', () => ({
  IconGlasses02: () => <span />,
  IconSearchMd: () => <span />
}))

jest.mock('dbaas/components/Measurement', () => ({
  HeroComponent: ({ ready }: { ready?: boolean }) => <div data-testid="integration-list-hero" data-ready={ready} />,
  isHeroQueryReady: ({ isFetched, isError }: { isFetched: boolean; isError: boolean }) => isFetched || isError
}))

jest.mock('dbaas/services/metricsIntegration', () => ({
  useIntegrationServiceListIntegrations2: jest.fn()
}))

jest.mock('dbaas/stores/useStores', () => () => ({
  store: { tenantData: { id: 'org-1' } }
}))

jest.mock('./features/RequestIntegration', () => ({
  RequestIntegration: () => <div />
}))

jest.mock('./helper', () => ({
  useIntegrationData: () => []
}))

jest.mock('./IntegrationGroup', () => ({
  IntegrationGroup: () => <div />
}))

const mockedListIntegrations = useIntegrationServiceListIntegrations2 as jest.MockedFunction<
  typeof useIntegrationServiceListIntegrations2
>

const cluster = { id: 'tidb-1' }

describe('IntegrationList hero', () => {
  it.each([
    { isFetched: false, isError: false, expected: 'false' },
    { isFetched: true, isError: false, expected: 'true' },
    { isFetched: false, isError: true, expected: 'true' }
  ])('tracks the list query terminal state: $expected', ({ isFetched, isError, expected }) => {
    mockedListIntegrations.mockReturnValue({
      data: undefined,
      isFetched,
      isError,
      refetch: jest.fn()
    } as unknown as ReturnType<typeof useIntegrationServiceListIntegrations2>)

    render(<IntegrationList cluster={cluster as any} />)

    expect(screen.getByTestId('integration-list-hero').getAttribute('data-ready')).toBe(expected)
  })
})
