2022-04-19 12:46:57 +08:00
|
|
|
/* eslint-disable import/first */
|
|
|
|
let isClientMocked = false
|
|
|
|
|
2022-04-12 22:50:34 +08:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { cAF, rAF } from '..'
|
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.mock('@vueuse/core', () => ({
|
|
|
|
get isClient() {
|
|
|
|
return isClientMocked
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
2022-04-12 22:50:34 +08:00
|
|
|
describe('raf', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
vi.useFakeTimers()
|
|
|
|
})
|
|
|
|
afterEach(() => {
|
|
|
|
vi.useRealTimers()
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('CSR should work', () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
isClientMocked = true
|
|
|
|
|
2022-04-12 22:50:34 +08:00
|
|
|
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', () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
isClientMocked = false
|
2022-04-12 22:50:34 +08:00
|
|
|
|
|
|
|
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",
|
|
|
|
],
|
|
|
|
]
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
})
|