Repository: nuxt/nuxt
File: test/nuxt/composables.test.ts (TypeScript)

Code before:
    nuxtApp.payload.serverRendered = true
    const ClientOnlyPage = await createClientPage(() => Promise.resolve(defineNuxtComponent({
      asyncData: () => ({
        users: ['alice', 'bob'],
      }),
      render () {
        // @ts-expect-error this is not typed
        return h('div', `Total users: ${this.users.value.length}`)
      },
    })))
    const wrapper = await mountSuspended(ClientOnlyPage)
    expect(wrapper.html()).toMatchInlineSnapshot(`"<div>Total users: 2</div>"`)
    nuxtApp.isHydrating = false
    nuxtApp.payload.serverRendered = false
  })
  it.todo('should support Options API head')
})

describe('useCookie', () => {
  it('should watch custom cookie refs', () => {
    const user = useCookie('userInfo', {
      default: () => ({ score: -1 }),
      maxAge: 60 * 60,
    })
    const computedVal = computed(() => user.value.score)
    expect(computedVal.value).toBe(-1)
    user.value.score++
    expect(computedVal.value).toBe(0)
  })

  it('cookie decode function should be invoked once', () => {
    // Pre-set cookies
    document.cookie = 'foo=Foo'
    document.cookie = 'bar=%7B%22s2%22%3A0%7D'
    document.cookie = 'baz=%7B%22s2%22%3A0%7D'

    let barCallCount = 0
    const bazCookie = useCookie<{ s2: number }>('baz', {
      default: () => ({ s2: -1 }),
      decode (value) {
        barCallCount++
        return destr(decodeURIComponent(value))
      },
    })
    bazCookie.value.s2++
    expect(bazCookie.value.s2).toEqual(1)
    expect(barCallCount).toBe(1)

    let quxCallCount = 0
    const quxCookie = useCookie<{ s3: number }>('qux', {
      default: () => ({ s3: -1 }),

Review comment (suggestion):
[nitpick] Consider rephrasing or removing the humorous comment to maintain a professional tone in test files.

Code after:
    nuxtApp.payload.serverRendered = true
    const ClientOnlyPage = await createClientPage(() => Promise.resolve(defineNuxtComponent({
      asyncData: () => ({
        users: ['alice', 'bob'],
      }),
      render () {
        // @ts-expect-error this is not typed
        return h('div', `Total users: ${this.users.value.length}`)
      },
    })))
    const wrapper = await mountSuspended(ClientOnlyPage)
    expect(wrapper.html()).toMatchInlineSnapshot(`"<div>Total users: 2</div>"`)
    nuxtApp.isHydrating = false
    nuxtApp.payload.serverRendered = false
  })

  it('should support Options API refreshNuxtData', async () => {
    let count = 0
    const component = defineNuxtComponent({
      asyncData: () => ({
        number: count++,
      }),
      setup () {
        const vm = getCurrentInstance()
        return () => {
          // @ts-expect-error go directly to jail 😈
          return h('div', vm!.render.number.value)
        }
      },
    })

    const wrapper = await mountSuspended(component)
    expect(wrapper.html()).toMatchInlineSnapshot(`"<div>0</div>"`)

    await refreshNuxtData()

    expect(wrapper.html()).toMatchInlineSnapshot(`"<div>1</div>"`)
  })

  it.todo('should support Options API head')
})

describe('useCookie', () => {
  it('should watch custom cookie refs', () => {
    const user = useCookie('userInfo', {
      default: () => ({ score: -1 }),
      maxAge: 60 * 60,
    })
    const computedVal = computed(() => user.value.score)
    expect(computedVal.value).toBe(-1)
    user.value.score++