mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
* fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
76 lines
3.2 KiB
TypeScript
76 lines
3.2 KiB
TypeScript
import { defineComponent, nextTick, ref } from 'vue'
|
|
import { mount } from '@vue/test-utils'
|
|
import { describe, expect, it } from 'vitest'
|
|
import sleep from '@element-plus/test-utils/sleep'
|
|
import { useThrottleRender } from '../use-throttle-render'
|
|
|
|
const Comp = defineComponent({
|
|
setup() {
|
|
const loading = ref(false)
|
|
const throttled = useThrottleRender(loading, 1000)
|
|
// Test the settimeout branch clearly: trigger the watch to record the settimeout first, and then record the settimeout again when mount.
|
|
loading.value = true
|
|
|
|
return () => <div class="test-dom">{throttled.value.toString()}</div>
|
|
},
|
|
})
|
|
|
|
describe.concurrent('useThrottleRender', () => {
|
|
it('should throttle rendering when loading is true', async () => {
|
|
const wrapper = mount(Comp)
|
|
await nextTick()
|
|
expect(wrapper.find('.test-dom').text()).toBe('false') // initially false
|
|
await sleep(1000)
|
|
expect(wrapper.find('.test-dom').text()).toBe('true') // after throttle time, should be true
|
|
wrapper.unmount()
|
|
})
|
|
|
|
it('should return false immediately when loading is false', () => {
|
|
const loading = ref(false)
|
|
const throttled = useThrottleRender(loading, 1000)
|
|
expect(throttled.value).toBe(false)
|
|
})
|
|
|
|
it('should return the same value immediately when throttle is 0', () => {
|
|
const loading = ref(true)
|
|
const throttled = useThrottleRender(loading, 0)
|
|
expect(throttled.value).toBe(true) // should be same as loading
|
|
})
|
|
|
|
it('should throttle rendering and update when loading changes', async () => {
|
|
const loading = ref(true)
|
|
const throttled = useThrottleRender(loading, 1000)
|
|
expect(throttled.value).toBe(false) // initially false
|
|
loading.value = false
|
|
expect(throttled.value).toBe(false) // should remain false immediately
|
|
await sleep(1000)
|
|
loading.value = true
|
|
expect(throttled.value).toBe(false) // should still be false after throttle time
|
|
})
|
|
|
|
it('should use `initVal` as initial value when pass `{ initVal: true/false }`', async () => {
|
|
const loading = ref(false)
|
|
const throttled = useThrottleRender(loading, { initVal: true })
|
|
expect(throttled.value).toBe(true)
|
|
const throttled2 = useThrottleRender(loading, { initVal: false })
|
|
expect(throttled2.value).toBe(false)
|
|
})
|
|
|
|
it('should throttle on display and disappear when pass `{ leading: xxx, trailing: xxx }`', async () => {
|
|
const loading = ref(false)
|
|
const throttled = useThrottleRender(loading, {
|
|
leading: 200,
|
|
trailing: 200,
|
|
})
|
|
expect(throttled.value).toBe(false) // initially false when not pass initVal
|
|
loading.value = true
|
|
expect(throttled.value).toBe(false) // should remain false until throttle time
|
|
await sleep(250) // Here, the delay time cannot be set to 200, setTimeout is not so precise, you can set it a little larger.
|
|
expect(throttled.value).toBe(true) // should be true after throttle time
|
|
loading.value = false
|
|
expect(throttled.value).toBe(true) // should remain true until throttle time
|
|
await sleep(250) // Here, the delay time cannot be set to 200, setTimeout is not so precise, you can set it a little larger.
|
|
expect(throttled.value).toBe(false) // should be false after throttle time
|
|
})
|
|
})
|