// @vitest-environment jsdom

import { mount } from '@vue/test-utils'
import { beforeEach, describe, expect, test, vi } from 'vitest'
import BackButton from '@/components/common/BackButton/index.vue'
console.log(BackButton)
const { push, back } = vi.hoisted(() => ({
  push: vi.fn(),
  back: vi.fn(),
}))

vi.mock('vue-router', () => ({
  useRouter: () => ({
    push,
    back,
  }),
}))

describe('component: BackButton', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  test('backHomePage 为 true 时，点击后跳转首页', async () => {
    const wrapper = mount(BackButton, {
      props: {
        backHomePage: true,
      },
    })

    await wrapper.trigger('click')

    expect(push).toHaveBeenCalledWith('/')
    expect(back).not.toHaveBeenCalled()
  })

  test('backHomePage 为 false 且存在历史记录时，点击后返回上一页', async () => {
    Object.defineProperty(window.history, 'length', {
      configurable: true,
      value: 2,
    })

    const wrapper = mount(BackButton)

    await wrapper.trigger('click')

    expect(back).toHaveBeenCalled()
    expect(push).not.toHaveBeenCalled()
  })

  test('backHomePage 为 false 且不存在历史记录时，点击后跳转首页', async () => {
    Object.defineProperty(window.history, 'length', {
      configurable: true,
      value: 1,
    })

    const wrapper = mount(BackButton)

    await wrapper.trigger('click')

    expect(push).toHaveBeenCalledWith('/')
    expect(back).not.toHaveBeenCalled()
  })
})
