element-plus/packages/utils/__tests__/raf.test.ts
三咲智子 1d13ebb05d
feat: drop jest (#7248)
* feat: drop jest

* test: remove ssr

* test: rename

* chore: update tsconfig
2022-04-19 16:51:44 +08:00

106 lines
1.7 KiB
TypeScript

/* 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",
],
]
`)
})
})