Skip to content

Commit

Permalink
test(Tabs): add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ccccpj committed Jun 13, 2023
1 parent 94f3a08 commit cb92ba6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
63 changes: 63 additions & 0 deletions src/tabs/__test__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import { ref } from 'vue';
import { Tabs, TabPanel } from '../index';
import TTabNav from '../tab-nav-item.vue';

const sleep = (duration) =>
new Promise((resolve) =>
setTimeout(() => {
resolve();
}, duration),
);


const prefix = 't';
const name = `${prefix}-tabs`;
const list = [
Expand Down Expand Up @@ -265,5 +273,60 @@ describe('Tabs', () => {
expect(currentLabel.value).toBe(`标签页${currentValue.value}`);
}
});
it(': touch', async () => {
const currentValue = ref('1');
const onChange = vi.fn((value) => {
currentValue.value = value;
});
const wrapper = mount({
setup() {
return () => (
<Tabs v-model={currentValue.value} onChange={onChange}>
<TabPanel value="1" label="标签页一" panel="标签一内容区" />
<TabPanel value="2" label="标签页二" panel="标签二内容区" />
</Tabs>
);
},
});
const $content = wrapper.find(`.${name}__content`);

await $content.trigger('touchstart', { touches: [{ clientX: 0, clientY: 0 }] })
await $content.trigger('touchmove', { touches: [{ clientX: 101, clientY: 0 }] })
await $content.trigger('touchend', { touches: [{ clientX: 200, clientY: 0 }] })
await sleep(200)

expect(onChange).toHaveBeenCalledTimes(0);
});
});
});
describe('Tab-panel', () => {
describe('props', () => {
it(': destroyOnHide', async () => {
const currentValue = ref('1');
const currentLabel=ref('')
const onClick = vi.fn((value,label) => {
currentValue.value = value;
currentLabel.value = label;
});
const wrapper = mount({
setup() {
return () => (
<Tabs v-model={currentValue.value} onClick={onClick}>
<TabPanel value="1" label="标签页1" panel="标签一内容区" destroyOnHide={false} />
<TabPanel value="2" label="标签页2" panel="标签二内容区" />
</Tabs>
);
},
});

const navWrap = wrapper.find(`.${name}__wrapper`);
const $tabNavItems = navWrap.findAllComponents(TTabNav);

$tabNavItems.map(async (item, index) => {
await item.trigger('click');
expect(wrapper.findAll('.t-tab-panel')).toHaveLength(1);
});
});

});
});
5 changes: 1 addition & 4 deletions src/tabs/tabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ export default defineComponent({
const stickyProps = computed(() => ({ ...props.stickyProps, disabled: !props.sticky }));
const activeClass = `${name}__item--active`;
const disabledClass = `${name}__item--disabled`;
const classes = computed(() => [`${name}`, props.size ? CLASSNAMES.SIZE[props.size] : '']);
const classes = computed(() => [`${name}`, props.size && CLASSNAMES.SIZE[props.size]]);
const navClasses = ref([`${name}__nav`]);
const isScroll = ref(false);
const startX = ref(0);
const startY = ref(0);
const endX = ref(0);
Expand Down Expand Up @@ -157,8 +156,6 @@ export default defineComponent({
};
onMounted(() => {
isScroll.value = (navWrap.value?.offsetWidth || 0) > (navScroll.value?.offsetWidth || 0);
isScroll.value && navClasses.value.push(`${prefix}-is-scrollable`);
window.addEventListener('resize', moveToActiveTab, false);
setTimeout(() => {
moveToActiveTab();
Expand Down

0 comments on commit cb92ba6

Please sign in to comment.