import { renderHook, waitFor } from '@testing-library/react-native'
import { useWorksList } from './useWorksList'

describe('useWorksList', () => {
  it('should load initial works data', async () => {
    const { result } = renderHook(() => useWorksList())

    expect(result.current.loading).toBe(true)
    expect(result.current.works).toEqual([])

    await waitFor(() => {
      expect(result.current.loading).toBe(false)
    })

    expect(result.current.works.length).toBeGreaterThan(0)
    expect(result.current.error).toBeNull()
  })

  it('should handle refresh', async () => {
    const { result } = renderHook(() => useWorksList())

    await waitFor(() => {
      expect(result.current.loading).toBe(false)
    })

    const initialWorks = result.current.works

    await result.current.refresh()

    expect(result.current.refreshing).toBe(false)
    expect(result.current.works).toEqual(initialWorks)
  })

  it('should load more works', async () => {
    const { result } = renderHook(() => useWorksList())

    await waitFor(() => {
      expect(result.current.loading).toBe(false)
    })

    const initialCount = result.current.works.length

    await result.current.loadMore()

    expect(result.current.works.length).toBeGreaterThan(initialCount)
  })

  it('should set hasMore to false when no more data', async () => {
    const { result } = renderHook(() => useWorksList())

    await waitFor(() => {
      expect(result.current.loading).toBe(false)
    })

    await result.current.loadMore()
    await result.current.loadMore()
    await result.current.loadMore()

    expect(result.current.hasMore).toBe(false)
  })
})
