2022-06-22 08:04:42 +08:00
|
|
|
// @ts-nocheck
|
2021-07-28 09:24:08 +08:00
|
|
|
import { nextTick } from '@vue/runtime-core'
|
|
|
|
import { mount } from '@vue/test-utils'
|
2022-04-19 12:46:57 +08:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2021-07-28 09:24:08 +08:00
|
|
|
import Scrollbar from '../src/components/scrollbar'
|
|
|
|
import { ScrollbarDirKey } from '../src/defaults'
|
|
|
|
|
|
|
|
describe('virtual scrollbar', () => {
|
2022-04-20 18:06:45 +08:00
|
|
|
async function testInlineStyle(
|
|
|
|
layout: 'vertical' | 'horizontal' = 'vertical'
|
|
|
|
) {
|
2021-07-28 09:24:08 +08:00
|
|
|
const wrapper = mount({
|
2021-09-26 19:22:56 +08:00
|
|
|
template: `<scrollbar visible layout="${layout}" :total="100" :ratio="25" :client-size="100" :scroll-from="20"></scrollbar>`,
|
2021-07-28 09:24:08 +08:00
|
|
|
components: {
|
|
|
|
Scrollbar,
|
|
|
|
},
|
|
|
|
})
|
2021-08-27 19:13:37 +08:00
|
|
|
await nextTick()
|
2021-07-28 09:24:08 +08:00
|
|
|
const scrollbar = wrapper.findComponent(Scrollbar)
|
|
|
|
const styles = getComputedStyle(scrollbar.vm.$el)
|
2021-08-27 19:13:37 +08:00
|
|
|
|
|
|
|
Object.entries({
|
2021-07-28 09:24:08 +08:00
|
|
|
display: 'block',
|
|
|
|
position: 'absolute',
|
2021-09-13 18:07:51 +08:00
|
|
|
// width: 'vertical' !== layout ? '100%' : '6px',
|
|
|
|
// height: 'vertical' !== layout ? '6px' : 'auto',
|
2021-07-28 09:24:08 +08:00
|
|
|
[ScrollbarDirKey[layout]]: '2px',
|
|
|
|
right: '2px',
|
|
|
|
bottom: '2px',
|
|
|
|
'border-radius': '4px',
|
2021-09-04 19:29:28 +08:00
|
|
|
}).forEach(([key, value]) =>
|
|
|
|
expect(`${key}: ${styles.getPropertyValue(key)}`).toBe(`${key}: ${value}`)
|
|
|
|
)
|
2021-07-28 09:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
it('vertical inline style', async () => {
|
|
|
|
testInlineStyle('vertical')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('horizontal inline style', () => {
|
|
|
|
testInlineStyle('horizontal')
|
|
|
|
})
|
2021-09-09 12:04:11 +08:00
|
|
|
|
|
|
|
it('click track', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
|
|
|
<div
|
|
|
|
style="
|
|
|
|
height: 100px;
|
|
|
|
position: relative;
|
|
|
|
border: 1px solid red;
|
|
|
|
"
|
|
|
|
>
|
|
|
|
<scrollbar
|
|
|
|
layout="vertical"
|
|
|
|
:total="100"
|
|
|
|
:ratio="25"
|
|
|
|
:client-size="100"
|
|
|
|
:scroll-from="0 / 300"
|
|
|
|
:visible="true"
|
|
|
|
ref="scrollbar"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
components: {
|
|
|
|
Scrollbar,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
const scrollbar = wrapper.findComponent(Scrollbar)
|
|
|
|
const el = scrollbar.vm.$el
|
|
|
|
|
2021-09-13 18:07:51 +08:00
|
|
|
/**
|
|
|
|
* layout: vertical; width: auto; height: 100px; scrollHeight: 400px;
|
|
|
|
* thumb ratio: (100 / 400) * 100 -> 25 // (clientHeight / scrollHeight) * 100
|
|
|
|
* thumbSize: 33 // scrollbar.ts computed thumbSize
|
|
|
|
* thumb translateY: (0 / (400 - 100)) * (100 - 25) -> 0 // (scrollTop / (scrollHeight - clientHeight)) * (clientHeight - thumbSize)
|
|
|
|
*/
|
2021-09-09 12:04:11 +08:00
|
|
|
const initializeStyle =
|
|
|
|
'height: 33px; transform: translateY(0px); webkit-transform: translateY(0px); width: 100%;'
|
|
|
|
|
|
|
|
expect(wrapper.find('.el-scrollbar__thumb').attributes('style')).toContain(
|
|
|
|
initializeStyle
|
|
|
|
)
|
|
|
|
|
|
|
|
const e = document.createEvent('MouseEvents')
|
|
|
|
const clientY = 20
|
|
|
|
e.initMouseEvent(
|
|
|
|
'mousedown',
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
clientY,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
0,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
el.dispatchEvent(e)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(
|
|
|
|
wrapper.find('.el-scrollbar__thumb').attributes('style')
|
|
|
|
).not.toContain(initializeStyle)
|
|
|
|
})
|
2021-09-13 18:07:51 +08:00
|
|
|
|
|
|
|
it('horizontal track height/width', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
|
|
|
<div
|
|
|
|
style="
|
|
|
|
width: 200px;
|
|
|
|
height: 100px;
|
|
|
|
position: relative;
|
|
|
|
border: 1px solid red;
|
|
|
|
"
|
|
|
|
>
|
|
|
|
<scrollbar
|
|
|
|
layout="horizontal"
|
|
|
|
:total="100"
|
|
|
|
:ratio="25"
|
|
|
|
:client-size="200"
|
|
|
|
:scroll-from="0 / 300"
|
|
|
|
:visible="true"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
components: {
|
|
|
|
Scrollbar,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(
|
|
|
|
(wrapper.find('.el-virtual-scrollbar').element as HTMLElement).style.width
|
2022-04-20 18:06:45 +08:00
|
|
|
).toContain('198px') // clientSize - props.endGap = 200 - 2 = 198
|
2021-09-13 18:07:51 +08:00
|
|
|
|
|
|
|
expect(
|
|
|
|
(wrapper.find('.el-virtual-scrollbar').element as HTMLElement).style
|
|
|
|
.height
|
|
|
|
).toContain('6px') // fixed 6
|
|
|
|
})
|
|
|
|
|
|
|
|
it('vertical track height/width', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
|
|
|
<div style="height: 100px; position: relative; border: 1px solid red">
|
|
|
|
<scrollbar
|
|
|
|
:total="100"
|
|
|
|
:ratio="25"
|
|
|
|
:client-size="100"
|
|
|
|
:scroll-from="0 / 300"
|
|
|
|
:visible="true"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
components: {
|
|
|
|
Scrollbar,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(
|
|
|
|
(wrapper.find('.el-virtual-scrollbar').element as HTMLElement).style
|
|
|
|
.height
|
2022-04-20 18:06:45 +08:00
|
|
|
).toContain('98px') // clientSize - props.endGap = 100 - 2 = 98
|
2021-09-13 18:07:51 +08:00
|
|
|
|
|
|
|
expect(
|
|
|
|
(wrapper.find('.el-virtual-scrollbar').element as HTMLElement).style.width
|
|
|
|
).toContain('6px') // fixed 6
|
|
|
|
})
|
2022-04-20 18:06:45 +08:00
|
|
|
|
|
|
|
it('should locate correctly when endGap is set', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
|
|
|
<div style="height: 100px; position: relative; border: 1px solid red">
|
|
|
|
<scrollbar
|
|
|
|
:total="100"
|
|
|
|
:ratio="25"
|
|
|
|
:client-size="100"
|
|
|
|
:scroll-from="0 / 300"
|
|
|
|
:end-gap="8"
|
|
|
|
:visible="true"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
components: {
|
|
|
|
Scrollbar,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(
|
|
|
|
(wrapper.find('.el-virtual-scrollbar').element as HTMLElement).style
|
|
|
|
.height
|
|
|
|
).toContain('92px') // clientSize - props.endGap = 100 - 8 = 92
|
|
|
|
})
|
2021-07-28 09:24:08 +08:00
|
|
|
})
|