mirror of
https://github.com/element-plus/element-plus.git
synced 2025-12-19 09:09:40 +08:00
* refactor(style): Update Eslint to V9 and Prettier to V3 * fix: vscode color * fix: vscode color * chore: remove Useless dependence and old config file * chore: format * Merge branch 'dev' into eslintV9 * fix: fix * fix: ssr test * fix: ssr test * fix: use defineConfig * fix: prettier format and ignore docs dist * fix: index.mjs => index.js * fix: Vscode color * fix: prettier ignore global.d.ts * fix: format --------- Co-authored-by: 2586740555 <15972343+CYJ090915@user.noreply.gitee.com>
106 lines
1.8 KiB
TypeScript
106 lines
1.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
let isClientMocked = false
|
|
|
|
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",
|
|
],
|
|
]
|
|
`)
|
|
})
|
|
})
|