[improvement] IndexBar: add test cases

This commit is contained in:
陈嘉涵
2019-05-30 20:44:05 +08:00
parent a412c605d1
commit cc36334625
2 changed files with 86 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
import { mount, trigger, triggerDrag } from '../../../test/utils';
import Vue from 'vue';
import IndexBar from '..';
import IndexAnchor from '../../index-anchor';
import { mount, trigger, triggerDrag } from '../../../test/utils';
Vue.use(IndexBar);
Vue.use(IndexAnchor);
@@ -12,6 +12,14 @@ function mockScrollIntoView() {
return fn;
}
function mockOffsetHeight(offsetHeight) {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
get() {
return offsetHeight;
}
});
}
test('custom anchor text', () => {
const wrapper = mount({
template: `
@@ -94,3 +102,44 @@ test('touch and scroll to anchor', () => {
expect(fn).toHaveBeenCalledTimes(1);
expect(onSelect).toHaveBeenCalledWith('B');
});
test('scroll and update active anchor', () => {
const nativeRect = Element.prototype.getBoundingClientRect;
Element.prototype.getBoundingClientRect = function () {
const { index } = this.dataset;
return {
top: index ? index * 10 : 0
};
};
mockOffsetHeight(10);
const wrapper = mount({
template: `
<van-index-bar :sticky="sticky">
<van-index-anchor
v-for="index in 4"
:key="index"
:index="index"
:data-index="index - 1"
/>
</van-index-bar>
`,
data() {
return {
sticky: false
};
}
});
window.scrollTop = 0;
trigger(window, 'scroll');
expect(wrapper).toMatchSnapshot();
wrapper.setData({ sticky: true });
trigger(window, 'scroll');
expect(wrapper).toMatchSnapshot();
wrapper.vm.$destroy();
Element.prototype.getBoundingClientRect = nativeRect;
});