element-plus/packages/utils/__tests__/raf.test.ts
btea d0eb6c3d1a
feat: support node 18+ & update tsx and vitest (#16190)
* feat: support node 18+ & update tsx and vitest

* chore: update

* chore: remove

* fix: update dep

* test: fix autocomplete

* test: update

* test: update

* chore: update

* chore: update

* chore: update
2024-04-11 17:48:34 +08:00

107 lines
1.8 KiB
TypeScript

/* eslint-disable import/first */
let isClientMocked = false
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('@vueuse/core', () => ({
get isClient() {
return isClientMocked
},
}))
describe('raf', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
vi.restoreAllMocks()
})
it('CSR should work', async () => {
isClientMocked = true
const obj = await import('..')
const { cAF, rAF } = obj
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', async () => {
isClientMocked = false
const obj = await import('..')
const { cAF, rAF } = obj
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",
],
]
`)
})
})