Files
element-plus/packages/utils/__tests__/raf.test.ts
Noblet Ouways 2f17df1209 style(eslint-config): newline before import type (#21036)
* 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 commit db0116a288.

* Revert "chore: fix"

This reverts commit 69c82a90c0.

* chore: fix

* style: `pnpm lint:fix`

* fix: lint

* chore: `pnpm format`
2025-06-16 15:37:12 +08:00

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