import { renderHook } from '@testing-library/react-hooks'

import { useDeprecation } from '../Deprecation'

const oldThing = 'Boooo'
const newThing = 'Yaaaay'
const link = 'https://dwitter.net'

/* eslint-disable no-console */
describe('useDeprecation', () => {
  let spy
  beforeEach(() => {
    spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
  })

  afterEach(() => {
    spy.mockRestore()
  })

  const checkIsWarningCalled = (times = 1) => {
    expect(spy).toBeCalledTimes(times)

    for (let i = 0; i < times; i++) {
      expect(console.warn.mock.calls[i][0]).toContain(
        `${oldThing} is deprecated, please use ${newThing}`
      )
      expect(console.warn.mock.calls[i][1]).toContain(`${link}`)
    }
  }

  it('should warn without passing "when"', () => {
    renderHook(() => useDeprecation({ oldThing, newThing, link }))
    checkIsWarningCalled()
  })

  it('should warn when "when" is truthy', () => {
    renderHook(() => useDeprecation({ oldThing, newThing, link, when: true }))
    renderHook(() => useDeprecation({ oldThing, newThing, link, when: 'prop' }))
    checkIsWarningCalled(2)
  })

  it('should warn when "when" is falsy(except undefined)', () => {
    renderHook(() => useDeprecation({ oldThing, newThing, link, when: 0 }))
    renderHook(() => useDeprecation({ oldThing, newThing, link, when: null }))
    renderHook(() => useDeprecation({ oldThing, newThing, link, when: false }))
    renderHook(() => useDeprecation({ oldThing, newThing, link, when: '' }))
    checkIsWarningCalled(4)
  })

  it('should not warn when "when" is undefined', () => {
    renderHook(() =>
      useDeprecation({ oldThing, newThing, link, when: undefined })
    )

    expect(spy).toBeCalledTimes(0)
  })
})
/* eslint-enable no-console */
