mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
* perf: change to import-x * feat: add rules * chore: fix rule * chore: fix * chore: fix * chore: fix * style: `pnpm lint:fix` * Revert "style: `pnpm lint:fix`" This reverts commitdb0116a288. * Revert "chore: fix" This reverts commit69c82a90c0. * chore: fix * style: `pnpm lint:fix` * fix: lint * chore: `pnpm format`
107 lines
1.9 KiB
TypeScript
107 lines
1.9 KiB
TypeScript
/* eslint-disable import-x/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",
|
|
],
|
|
]
|
|
`)
|
|
})
|
|
})
|