123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /* eslint-disable import/first */
- let isClientMocked = false
- import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
- import { cAF, rAF } from '..'
- vi.mock('@vueuse/core', () => ({
- get isClient() {
- return isClientMocked
- },
- }))
- describe('raf', () => {
- beforeEach(() => {
- vi.useFakeTimers()
- })
- afterEach(() => {
- vi.useRealTimers()
- vi.restoreAllMocks()
- })
- it('CSR should work', () => {
- isClientMocked = true
- const fn = vi.fn()
- rAF(() => fn('first'))
- vi.runAllTimers()
- expect(fn.mock.calls).toMatchInlineSnapshot(`
- [
- [
- "first",
- ],
- ]
- `)
- rAF(() => fn('second'))
- vi.runAllTimers()
- expect(fn.mock.calls).toMatchInlineSnapshot(`
- [
- [
- "first",
- ],
- [
- "second",
- ],
- ]
- `)
- const handle = rAF(() => fn('cancel'))
- cAF(handle)
- vi.runAllTimers()
- expect(fn.mock.calls).toMatchInlineSnapshot(`
- [
- [
- "first",
- ],
- [
- "second",
- ],
- ]
- `)
- })
- it('SSR should work', () => {
- isClientMocked = false
- const fn = vi.fn()
- rAF(() => fn('first'))
- vi.runAllTimers()
- expect(fn.mock.calls).toMatchInlineSnapshot(`
- [
- [
- "first",
- ],
- ]
- `)
- rAF(() => fn('second'))
- vi.runAllTimers()
- expect(fn.mock.calls).toMatchInlineSnapshot(`
- [
- [
- "first",
- ],
- [
- "second",
- ],
- ]
- `)
- const handle = rAF(() => fn('cancel'))
- cAF(handle)
- vi.runAllTimers()
- expect(fn.mock.calls).toMatchInlineSnapshot(`
- [
- [
- "first",
- ],
- [
- "second",
- ],
- ]
- `)
- })
- })
|