import { config, DOMWrapper, mount } from '@vue/test-utils';
import Tabbar from '../index.vue';
import TabbarItem from '../../tabbaritem/index.vue';
import NutIcon from '../../icon/index.vue';
import { nextTick } from 'vue';
// 模拟setup导入资源
jest.mock('vue-router', () => ({
useRouter: jest.fn()
}));
// 所有的测试用例之前执行一次
beforeAll(() => {
config.global.components = {
NutIcon
};
});
// 所有的测试用例之后执行一次
afterAll(() => {
config.global.components = {};
});
// 测试用例
test('should render tabbar when default', async () => {
const wrapper = mount({
components: {
Tabbar: Tabbar,
TabbarItem: TabbarItem
},
template: `
`,
setup() {}
});
await nextTick();
// 断言 期望值
expect(wrapper.find('.nut-tabbar').exists()).toBe(true);
expect(wrapper.find('.nut-tabbar-item').exists()).toBe(true);
expect(wrapper.find('.nut-tabbar-item_icon-box_icon').exists()).toBe(true);
});
test('should render custom img when using img prop', async () => {
const wrapper = mount({
components: {
Tabbar: Tabbar,
TabbarItem: TabbarItem
},
template: `
`
});
await nextTick();
const tabbarItemIcon: any = wrapper.findAll('.nut-tabbar-item_icon-box_icon');
expect(tabbarItemIcon[0].element.style.backgroundImage).toContain('c98ad61124172e93');
expect(tabbarItemIcon[1].element.style.backgroundImage).toContain('c9b3421fe18614e2');
});
test('should render custom check and icon size when using visible', async () => {
const wrapper = mount({
components: {
Tabbar: Tabbar,
TabbarItem: TabbarItem
},
template: `
`
});
await nextTick();
const tabbarItem = wrapper.findAll('.nut-tabbar-item');
expect(tabbarItem[1].element.style.color).toEqual('blue');
expect(wrapper.find('.nut-icon').element.style.fontSize).toEqual('18px');
});
test('should render custom color and bage when using prop', async () => {
const wrapper = mount({
components: {
Tabbar: Tabbar,
TabbarItem: TabbarItem
},
template: `
`
});
await nextTick();
const tabbarItem: any = wrapper.findAll('.nut-tabbar-item');
expect(tabbarItem.length).toBe(3);
expect(tabbarItem[0].element.style.color).toEqual('blue');
expect(tabbarItem[1].element.style.color).toEqual('grey');
expect(wrapper.find('.nut-tabbar-item_icon-box_tips').exists()).toBe(true);
});
test('should render fixed element when using bottom prop', async () => {
const wrapper = mount(Tabbar, {
props: {
bottom: true,
safeAreaInsetBottom: true
}
});
expect(wrapper.html()).toMatchSnapshot();
});
test('should match active tabbar by clcik', async () => {
const wrapper = mount({
components: {
Tabbar: Tabbar,
TabbarItem: TabbarItem
},
template: `
`
});
const tabbarItem: any = wrapper.findAll('.nut-tabbar-item');
expect(tabbarItem[0].element.style.color).toEqual('blue');
await tabbarItem[1].trigger('click');
expect(tabbarItem[1].element.style.color).toEqual('blue');
await tabbarItem[2].trigger('click');
expect(tabbarItem[2].element.style.color).toEqual('blue');
});
test('should show sure emitted when click', async () => {
const wrapper = mount({
components: {
Tabbar: Tabbar,
TabbarItem: TabbarItem
},
template: `
`,
data: () => {
return {
val: -1,
data: [] as any
};
},
methods: {
tabSwitch(item: any, index: number) {
(this as any).val = index;
(this as any).data = item;
}
}
});
const tabbarItem: any = wrapper.findAll('.nut-tabbar-item');
await tabbarItem[1].trigger('click');
await nextTick();
expect(wrapper.vm.val).toBe(1);
expect(wrapper.vm.data.tabTitle).toBe('分类');
});