import Tabs from '../../tabs';
import { mount, later, triggerDrag, mockScrollTop } from '../../../test';
function createWrapper(options = {}) {
return mount({
template: `
${options.extraTemplate || ''}
Text
Text
Text
`,
data() {
return {
color: '#ee0a24',
type: 'line',
sticky: true,
lineWidth: 2,
lazyRender: true,
};
},
...options,
});
}
test('click to switch tab', async () => {
const onChange = jest.fn();
const wrapper = mount({
template: `
Text
Text
Text
`,
methods: {
onChange,
},
});
await later();
expect(wrapper.html()).toMatchSnapshot();
const tabs = wrapper.findAll('.van-tab');
tabs.at(1).trigger('click');
tabs.at(2).trigger('click');
await later();
expect(wrapper.html()).toMatchSnapshot();
expect(onChange).toHaveBeenCalledTimes(1);
});
test('swipe to switch tab', async () => {
const onChange = jest.fn();
const wrapper = mount({
template: `
Text
Text
Text
`,
data() {
return {
active: 0,
};
},
methods: {
onChange,
},
});
const content = wrapper.find('.van-tabs__content');
await later();
expect(wrapper.html()).toMatchSnapshot();
triggerDrag(content, -100, 0);
expect(wrapper.html()).toMatchSnapshot();
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(1, 'title2');
triggerDrag(content, -100, 0);
expect(onChange).toHaveBeenCalledTimes(1);
expect(wrapper.html()).toMatchSnapshot();
await later();
wrapper.destroy();
});
test('change tabs data', async () => {
const wrapper = createWrapper();
await later();
expect(wrapper.html()).toMatchSnapshot();
wrapper.setData({
swipeable: false,
sticky: false,
type: 'card',
color: 'blue',
});
await later();
expect(wrapper.html()).toMatchSnapshot();
});
test('lazy render', async () => {
const wrapper = createWrapper();
expect(wrapper.html()).toMatchSnapshot();
wrapper.setData({
lazyRender: false,
});
await later();
expect(wrapper.html()).toMatchSnapshot();
});
test('render nav-left & nav-right slot', async () => {
const wrapper = createWrapper({
extraTemplate: `
Nav Left
Nav Right
`,
});
expect(wrapper.html()).toMatchSnapshot();
});
test('border props', async () => {
const wrapper = mount(Tabs, {
props: {
border: false,
},
});
expect(wrapper.html()).toMatchSnapshot();
});
test('click event', async () => {
const onClick = jest.fn();
const onDisabled = jest.fn();
const wrapper = mount({
template: `
Text
Text
`,
methods: {
onClick,
onDisabled,
},
});
const tabs = wrapper.findAll('.van-tab');
tabs.at(0).trigger('click');
expect(onClick).toHaveBeenCalledWith(0, 'title1');
tabs.at(1).trigger('click');
expect(onDisabled).toHaveBeenCalledWith(1, 'title2');
});
test('name prop', async () => {
const onClick = jest.fn();
const onChange = jest.fn();
const onDisabled = jest.fn();
const wrapper = mount({
template: `
Text
Text
Text
`,
methods: {
onClick,
onChange,
onDisabled,
},
data() {
return {
active: 0,
};
},
});
await later();
expect(wrapper.html()).toMatchSnapshot();
const tabs = wrapper.findAll('.van-tab');
tabs.at(1).trigger('click');
expect(onClick).toHaveBeenCalledWith('b', 'title2');
expect(onChange).toHaveBeenCalledWith('b', 'title2');
expect(onChange).toHaveBeenCalledTimes(1);
tabs.at(2).trigger('click');
expect(onDisabled).toHaveBeenCalledWith('c', 'title3');
expect(onChange).toHaveBeenCalledTimes(1);
});
test('set name to zero', async () => {
const onClick = jest.fn();
const wrapper = mount({
template: `
Text
Text
`,
methods: {
onClick,
},
});
const tabs = wrapper.findAll('.van-tab');
tabs.at(1).trigger('click');
expect(onClick).toHaveBeenCalledWith(0, 'title2');
});
test('title-style prop', () => {
const wrapper = mount({
template: `
Text
`,
});
expect(wrapper.find('.van-tab').element.style.color).toEqual('red');
});
test('dot prop', () => {
const wrapper = mount({
template: `
Text
`,
});
expect(wrapper.html()).toMatchSnapshot();
});
test('badge prop', () => {
const wrapper = mount({
template: `
Text
`,
});
expect(wrapper.html()).toMatchSnapshot();
});
test('scrollspy prop', async () => {
const onChange = jest.fn();
window.scrollTo = jest.fn();
const wrapper = mount({
template: `
Text
Text
Text
`,
methods: {
onChange,
},
});
await later();
expect(wrapper.html()).toMatchSnapshot();
const tabs = wrapper.findAll('.van-tab');
tabs.at(2).trigger('click');
expect(onChange).toHaveBeenCalledWith('c', 'title3');
await later();
mockScrollTop(100);
expect(wrapper.html()).toMatchSnapshot();
expect(onChange).toHaveBeenCalledWith('c', 'title3');
});
test('scrollTo method', async () => {
const onChange = jest.fn();
window.scrollTo = jest.fn();
mount({
template: `
Text
Text
Text
`,
methods: {
onChange,
},
mounted() {
this.$refs.root.scrollTo('b');
},
});
await later();
expect(onChange).toHaveBeenCalledWith('b', 'title2');
});
test('rendered event', async () => {
const onRendered = jest.fn();
const wrapper = mount({
template: `
Text
Title2
`,
data() {
return {
active: 'a',
};
},
methods: {
onRendered,
},
});
await later();
expect(onRendered).toHaveBeenCalledWith('a', 'title1');
expect(wrapper.find('.van-tab__pane')).toMatchSnapshot();
const tabs = wrapper.findAll('.van-tab');
tabs.at(1).trigger('click');
tabs.at(0).trigger('click');
await later();
expect(onRendered).toHaveBeenCalledTimes(2);
});
test('should not trigger rendered event when disable lazy-render', async () => {
const onRendered = jest.fn();
mount({
template: `
Text
Title2
`,
methods: {
onRendered,
},
});
await later();
expect(onRendered).toHaveBeenCalledTimes(0);
});
test('before-change prop', async () => {
const onChange = jest.fn();
const wrapper = mount({
template: `
Text
Text
Text
Text
Text
`,
methods: {
onChange,
beforeChange(name) {
switch (name) {
case 1:
return false;
case 2:
return true;
case 3:
return Promise.resolve(false);
case 4:
return Promise.resolve(true);
}
},
},
});
await later();
const tabs = wrapper.findAll('.van-tab');
tabs.at(1).trigger('click');
expect(onChange).toHaveBeenCalledTimes(0);
tabs.at(2).trigger('click');
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenLastCalledWith(2, 'title3');
tabs.at(3).trigger('click');
expect(onChange).toHaveBeenCalledTimes(1);
tabs.at(4).trigger('click');
await later();
expect(onChange).toHaveBeenCalledTimes(2);
expect(onChange).toHaveBeenLastCalledWith(4, 'title5');
});
test('render empty tab', async () => {
const wrapper = mount({
template: `
`,
});
expect(wrapper.html()).toMatchSnapshot();
});