import { describe, expect, test } from 'vitest'
import { useResetData } from '@/hooks/useResetData'

describe('hook: useResetData', () => {
  test('初始化时返回 initialValue 的深拷贝', () => {
    const rawObj = {
      age: 1,
      arr: [],
    }
    const [data] = useResetData(rawObj)

    expect(data.value).not.toBe(rawObj)
    expect(data.value).toEqual(rawObj)

    data.value.age = 2
    expect(data.value).not.toEqual(rawObj)
  })

  test('reset仍然是深拷贝', () => {
    const rawObj = {
      age: 1,
      arr: [],
    }
    const [data, reset] = useResetData(rawObj)

    data.value.age = 2
    reset()

    expect(data.value).not.toBe(rawObj)
    expect(data.value).toEqual(rawObj)
  })

  test('reset 会替换 state.value，旧 toRef 不再跟随后续状态变化', () => {
    const rawObj = {
      age: 1,
      arr: [],
    }

    const [data, reset] = useResetData(rawObj)
    const ageRef = toRef(data.value, 'age')

    data.value.age = 2
    expect(ageRef.value).toBe(2)

    reset()
    data.value.age = 3

    expect(ageRef.value).toBe(2)
  })

  test('forReset 会保留 state.value 引用，旧 toRef 仍会跟随后续状态变化', () => {
    const rawObj = {
      age: 1,
      arr: [],
    }

    const [data, , forReset] = useResetData(rawObj)
    const ageRef = toRef(data.value, 'age')

    data.value.age = 2
    expect(ageRef.value).toBe(2)

    forReset()
    expect(ageRef.value).toBe(1)

    data.value.age = 3
    expect(ageRef.value).toBe(3)
  })
})
