2021-07-05 09:10:50 +08:00
|
|
|
import { nextTick } from 'vue'
|
2020-08-10 15:29:16 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2021-04-21 15:28:46 +08:00
|
|
|
import { defineGetter, makeScroll } from '@element-plus/test-utils'
|
2021-11-06 09:32:11 +08:00
|
|
|
import Scrollbar from '../src/scrollbar.vue'
|
2020-09-02 17:44:14 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const _mount = (template: string) =>
|
|
|
|
mount({
|
|
|
|
components: {
|
|
|
|
'el-scrollbar': Scrollbar,
|
|
|
|
},
|
|
|
|
template,
|
|
|
|
})
|
2020-08-10 15:29:16 +08:00
|
|
|
|
2020-08-11 15:59:25 +08:00
|
|
|
describe('ScrollBar', () => {
|
|
|
|
test('vertical', async () => {
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerHeight = 204
|
2020-08-11 15:59:25 +08:00
|
|
|
const innerHeight = 500
|
|
|
|
const wrapper = _mount(`
|
2020-09-02 17:44:14 +08:00
|
|
|
<el-scrollbar style="height: ${outerHeight}px">
|
2020-08-11 15:59:25 +08:00
|
|
|
<div style="height: ${innerHeight}px;"></div>
|
|
|
|
</el-scrollbar>
|
2020-09-02 17:44:14 +08:00
|
|
|
`)
|
|
|
|
|
2020-08-11 15:59:25 +08:00
|
|
|
const scrollDom = wrapper.find('.el-scrollbar__wrap').element
|
2020-09-03 13:12:44 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const offsetHeightRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'offsetHeight',
|
|
|
|
outerHeight
|
|
|
|
)
|
|
|
|
const scrollHeightRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'scrollHeight',
|
|
|
|
innerHeight
|
|
|
|
)
|
2020-09-03 13:12:44 +08:00
|
|
|
|
2020-08-11 15:59:25 +08:00
|
|
|
await makeScroll(scrollDom, 'scrollTop', 100)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'height: 80px; transform: translateY(50%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2020-08-11 15:59:25 +08:00
|
|
|
await makeScroll(scrollDom, 'scrollTop', 300)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'height: 80px; transform: translateY(150%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2021-08-16 14:20:48 +08:00
|
|
|
offsetHeightRestore()
|
2021-04-21 15:28:46 +08:00
|
|
|
scrollHeightRestore()
|
2020-08-11 15:59:25 +08:00
|
|
|
})
|
2020-08-10 15:29:16 +08:00
|
|
|
|
2020-08-11 15:59:25 +08:00
|
|
|
test('horizontal', async () => {
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerWidth = 204
|
2020-08-11 15:59:25 +08:00
|
|
|
const innerWidth = 500
|
|
|
|
const wrapper = _mount(`
|
2020-09-02 17:44:14 +08:00
|
|
|
<el-scrollbar style="height: 100px; width: ${outerWidth}px">
|
2020-08-11 15:59:25 +08:00
|
|
|
<div style="height:100px; width: ${innerWidth}px;"></div>
|
|
|
|
</el-scrollbar>
|
2020-09-02 17:44:14 +08:00
|
|
|
`)
|
|
|
|
|
2020-08-11 15:59:25 +08:00
|
|
|
const scrollDom = wrapper.find('.el-scrollbar__wrap').element
|
2020-09-03 13:12:44 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const offsetWidthRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'offsetWidth',
|
|
|
|
outerWidth
|
|
|
|
)
|
|
|
|
const scrollWidthRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'scrollWidth',
|
|
|
|
innerWidth
|
|
|
|
)
|
2020-09-03 13:12:44 +08:00
|
|
|
|
2020-08-11 15:59:25 +08:00
|
|
|
await makeScroll(scrollDom, 'scrollLeft', 100)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'width: 80px; transform: translateX(50%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2020-08-11 15:59:25 +08:00
|
|
|
await makeScroll(scrollDom, 'scrollLeft', 300)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'width: 80px; transform: translateX(150%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2021-08-16 14:20:48 +08:00
|
|
|
offsetWidthRestore()
|
2021-04-21 15:28:46 +08:00
|
|
|
scrollWidthRestore()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('both vertical and horizontal', async () => {
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerHeight = 204
|
2021-04-21 15:28:46 +08:00
|
|
|
const innerHeight = 500
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerWidth = 204
|
2021-04-21 15:28:46 +08:00
|
|
|
const innerWidth = 500
|
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-scrollbar style="height: ${outerHeight}px; width: ${outerWidth}px;">
|
|
|
|
<div style="height: ${innerHeight}px; width: ${innerWidth}px;"></div>
|
|
|
|
</el-scrollbar>
|
|
|
|
`)
|
|
|
|
|
|
|
|
const scrollDom = wrapper.find('.el-scrollbar__wrap').element
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const offsetHeightRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'offsetHeight',
|
|
|
|
outerHeight
|
|
|
|
)
|
|
|
|
const scrollHeightRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'scrollHeight',
|
|
|
|
innerHeight
|
|
|
|
)
|
|
|
|
const offsetWidthRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'offsetWidth',
|
|
|
|
outerWidth
|
|
|
|
)
|
|
|
|
const scrollWidthRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'scrollWidth',
|
|
|
|
innerWidth
|
|
|
|
)
|
2021-04-21 15:28:46 +08:00
|
|
|
|
|
|
|
await makeScroll(scrollDom, 'scrollTop', 100)
|
|
|
|
await makeScroll(scrollDom, 'scrollLeft', 100)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'height: 80px; transform: translateY(50%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
|
|
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'width: 80px; transform: translateX(50%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2021-04-21 15:28:46 +08:00
|
|
|
await makeScroll(scrollDom, 'scrollTop', 300)
|
|
|
|
await makeScroll(scrollDom, 'scrollLeft', 300)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'height: 80px; transform: translateY(150%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
|
|
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'width: 80px; transform: translateX(150%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2021-04-21 15:28:46 +08:00
|
|
|
|
2021-08-16 14:20:48 +08:00
|
|
|
offsetHeightRestore()
|
2021-04-21 15:28:46 +08:00
|
|
|
scrollHeightRestore()
|
2021-08-16 14:20:48 +08:00
|
|
|
offsetWidthRestore()
|
2021-04-21 15:28:46 +08:00
|
|
|
scrollWidthRestore()
|
2020-08-10 15:29:16 +08:00
|
|
|
})
|
2021-04-20 10:44:10 +08:00
|
|
|
|
|
|
|
test('should render height props', async () => {
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerHeight = 204
|
2021-04-20 10:44:10 +08:00
|
|
|
const innerHeight = 500
|
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-scrollbar height="${outerHeight}px">
|
|
|
|
<div style="height: ${innerHeight}px;"></div>
|
|
|
|
</el-scrollbar>
|
|
|
|
`)
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.el-scrollbar__wrap').attributes('style')).toContain(
|
|
|
|
'height: 204px;'
|
|
|
|
)
|
2021-04-20 10:44:10 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should render max-height props', async () => {
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerHeight = 204
|
2021-04-20 10:44:10 +08:00
|
|
|
const innerHeight = 100
|
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-scrollbar max-height="${outerHeight}px">
|
|
|
|
<div style="height: ${innerHeight}px;"></div>
|
|
|
|
</el-scrollbar>
|
|
|
|
`)
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.el-scrollbar__wrap').attributes('style')).toContain(
|
|
|
|
'max-height: 204px;'
|
|
|
|
)
|
2021-04-20 10:44:10 +08:00
|
|
|
})
|
2021-07-05 09:10:50 +08:00
|
|
|
|
|
|
|
test('should render always props', async () => {
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerHeight = 204
|
2021-07-05 09:10:50 +08:00
|
|
|
const innerHeight = 500
|
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-scrollbar height="${outerHeight}px" always>
|
|
|
|
<div style="height: ${innerHeight}px;"></div>
|
|
|
|
</el-scrollbar>
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(wrapper.find('.el-scrollbar__bar').attributes('style')).toBeFalsy()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('set scrollTop & scrollLeft', async () => {
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerHeight = 204
|
2021-07-05 09:10:50 +08:00
|
|
|
const innerHeight = 500
|
2021-08-16 14:20:48 +08:00
|
|
|
const outerWidth = 204
|
2021-07-05 09:10:50 +08:00
|
|
|
const innerWidth = 500
|
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-scrollbar ref="scrollbar" style="height: ${outerHeight}px; width: ${outerWidth}px;">
|
|
|
|
<div style="height: ${innerHeight}px; width: ${innerWidth}px;"></div>
|
|
|
|
</el-scrollbar>
|
|
|
|
`)
|
|
|
|
|
|
|
|
const scrollbar = wrapper.vm.$refs.scrollbar as any
|
|
|
|
const scrollDom = wrapper.find('.el-scrollbar__wrap').element
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const offsetHeightRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'offsetHeight',
|
|
|
|
outerHeight
|
|
|
|
)
|
|
|
|
const scrollHeightRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'scrollHeight',
|
|
|
|
innerHeight
|
|
|
|
)
|
|
|
|
const offsetWidthRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'offsetWidth',
|
|
|
|
outerWidth
|
|
|
|
)
|
|
|
|
const scrollWidthRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'scrollWidth',
|
|
|
|
innerWidth
|
|
|
|
)
|
2021-07-05 09:10:50 +08:00
|
|
|
|
|
|
|
scrollbar.setScrollTop(100)
|
|
|
|
await nextTick()
|
|
|
|
scrollbar.setScrollLeft(100)
|
|
|
|
await nextTick()
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'height: 80px; transform: translateY(0%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
|
|
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'width: 80px; transform: translateX(0%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2021-07-05 09:10:50 +08:00
|
|
|
|
2021-08-16 14:20:48 +08:00
|
|
|
offsetHeightRestore()
|
2021-07-05 09:10:50 +08:00
|
|
|
scrollHeightRestore()
|
2021-08-16 14:20:48 +08:00
|
|
|
offsetWidthRestore()
|
2021-07-05 09:10:50 +08:00
|
|
|
scrollWidthRestore()
|
|
|
|
})
|
2021-08-16 14:20:48 +08:00
|
|
|
|
|
|
|
test('should render min-size props', async () => {
|
|
|
|
const outerHeight = 204
|
|
|
|
const innerHeight = 10000
|
|
|
|
const wrapper = _mount(`
|
|
|
|
<el-scrollbar style="height: ${outerHeight}px">
|
|
|
|
<div style="height: ${innerHeight}px;"></div>
|
|
|
|
</el-scrollbar>
|
|
|
|
`)
|
|
|
|
|
|
|
|
const scrollDom = wrapper.find('.el-scrollbar__wrap').element
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const offsetHeightRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'offsetHeight',
|
|
|
|
outerHeight
|
|
|
|
)
|
|
|
|
const scrollHeightRestore = defineGetter(
|
|
|
|
scrollDom,
|
|
|
|
'scrollHeight',
|
|
|
|
innerHeight
|
|
|
|
)
|
2021-08-16 14:20:48 +08:00
|
|
|
|
|
|
|
await makeScroll(scrollDom, 'scrollTop', 0)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
2021-10-29 19:16:53 +08:00
|
|
|
'height: 20px; transform: translateY(0%);'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2021-08-16 14:20:48 +08:00
|
|
|
offsetHeightRestore()
|
|
|
|
scrollHeightRestore()
|
|
|
|
})
|
2020-08-10 15:29:16 +08:00
|
|
|
})
|