element-plus/packages/scrollbar/__tests__/scrollbar.spec.ts

51 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-08-10 15:29:16 +08:00
import { mount } from '@vue/test-utils'
2020-09-02 17:44:14 +08:00
import { makeScroll, defineGetter } from '@element-plus/test-utils'
2020-08-11 15:59:25 +08:00
import Scrollbar from '../src/index'
2020-09-02 17:44:14 +08:00
2020-08-11 15:59:25 +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 () => {
2020-09-02 17:44:14 +08:00
const outerHeight = 200
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
`)
defineGetter(window.HTMLElement.prototype, 'clientHeight', outerHeight)
defineGetter(window.HTMLElement.prototype, 'scrollHeight', innerHeight)
2020-08-11 15:59:25 +08:00
const scrollDom = wrapper.find('.el-scrollbar__wrap').element
await makeScroll(scrollDom, 'scrollTop', 100)
expect(wrapper.find('.is-vertical div').attributes('style')).toContain('height: 40%; transform: translateY(50%); webkit-transform: translateY(50%)')
await makeScroll(scrollDom, 'scrollTop', 300)
expect(wrapper.find('.is-vertical div').attributes('style')).toContain('height: 40%; transform: translateY(150%); webkit-transform: translateY(150%)')
})
2020-08-10 15:29:16 +08:00
2020-08-11 15:59:25 +08:00
test('horizontal', async () => {
2020-09-02 17:44:14 +08:00
const outerWidth = 200
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
`)
defineGetter(window.HTMLElement.prototype, 'clientWidth', outerWidth)
defineGetter(window.HTMLElement.prototype, 'scrollWidth', innerWidth)
2020-08-11 15:59:25 +08:00
const scrollDom = wrapper.find('.el-scrollbar__wrap').element
await makeScroll(scrollDom, 'scrollLeft', 100)
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain('width: 40%; transform: translateX(50%); webkit-transform: translateX(50%)')
await makeScroll(scrollDom, 'scrollLeft', 300)
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain('width: 40%; transform: translateX(150%); webkit-transform: translateX(150%)')
2020-08-10 15:29:16 +08:00
})
})