import { useTealiumUtag, waitForTealium } from './use-tealium-utag'
import { renderHook } from '@testing-library/react-hooks'
import { act } from '@testing-library/react'

describe('useTealiumUtag', () => {
  it('should return object with tealiumID of "1234"', async () => {
    global.utag = {
      data: {
        tealium_visitor_id: '1234',
      },
    } as any
    const { result, waitForNextUpdate } = renderHook(() => useTealiumUtag())
    await act(async () => {
      await waitForNextUpdate()
    })
    expect(result.current.tealiumId).toBe('1234')
    expect(result.current.loading).toBe(false)
  })

  it('should return object with tealiumID of undefined and loading is false after 3 seconds of waiting', async () => {
    global.utag = {
      data: {},
    } as any
    const { result, waitForNextUpdate } = renderHook(() => useTealiumUtag())
    await act(async () => {
      await waitForNextUpdate({ timeout: 3000 })
    })
    expect(result.current.tealiumId).toBe(undefined)
    expect(result.current.loading).toBe(false)
  })
})

describe('waitForTealium', () => {
  it('should return object with tealiumID of "1234"', async () => {
    global.utag = {
      data: {
        tealium_visitor_id: '1234',
      },
    } as any
    const res = await waitForTealium()
    expect(res.tealium_visitor_id).toBe('1234')
  })

  it('should return object with tealiumID of undefined', async () => {
    global.utag = {
      data: {},
    } as any
    const res = await waitForTealium()
    expect(res.tealium_visitor_id).toBe(undefined)
  })
})
