Skip to content

Commit

Permalink
test: add tabbar test (#314)
Browse files Browse the repository at this point in the history
* test: add tabbar test

* chore: update test

* test: add unit test for tabbar

* test: update test

Co-authored-by: kelvinzhou <[email protected]>
Co-authored-by: anlyyao <[email protected]>
  • Loading branch information
3 people authored Sep 18, 2022
1 parent a2d34f7 commit 7edaae7
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 4 deletions.
2 changes: 1 addition & 1 deletion site/docs.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default {
path: '/mobile-vue/components/grid',
component: () => import('@/grid/grid.md'),
},
]
],
},
{
title: '导航',
Expand Down
2 changes: 1 addition & 1 deletion site/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default ({ mode }) => {
},
}),
vueJsx({
isCustomElement
isCustomElement,
}),
createTDesignPlugin(),
],
Expand Down
12 changes: 11 additions & 1 deletion site/web/test-coverage.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
module.exports = {"button":{"statements":"98.57%","branches":"58.82%","functions":"100%","lines":"98.57%"},"checkbox":{"statements":"93.47%","branches":"75.8%","functions":"80%","lines":"93.47%"},"drawer":{"statements":"100%","branches":"93.75%","functions":"100%","lines":"100%"},"fab":{"statements":"100%","branches":"100%","functions":"100%","lines":"100%"},"overlay":{"statements":"92.55%","branches":"100%","functions":"20%","lines":"92.55%"},"popup":{"statements":"95%","branches":"64.7%","functions":"55.55%","lines":"95%"},"shared":{"statements":"70.58%","branches":"63.15%","functions":"25%","lines":"70.58%"},"switch":{"statements":"98.05%","branches":"92.85%","functions":"100%","lines":"98.05%"},"toast":{"statements":"100%","branches":"80%","functions":"100%","lines":"100%"}}
module.exports = {
button: { statements: '98.57%', branches: '58.82%', functions: '100%', lines: '98.57%' },
checkbox: { statements: '93.47%', branches: '75.8%', functions: '80%', lines: '93.47%' },
drawer: { statements: '100%', branches: '93.75%', functions: '100%', lines: '100%' },
fab: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
overlay: { statements: '92.55%', branches: '100%', functions: '20%', lines: '92.55%' },
popup: { statements: '95%', branches: '64.7%', functions: '55.55%', lines: '95%' },
shared: { statements: '70.58%', branches: '63.15%', functions: '25%', lines: '70.58%' },
switch: { statements: '98.05%', branches: '92.85%', functions: '100%', lines: '98.05%' },
toast: { statements: '100%', branches: '80%', functions: '100%', lines: '100%' },
};
152 changes: 152 additions & 0 deletions src/tab-bar/__test__/__snapshots__/index.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Vitest Snapshot v1

exports[`TabBar > props > snapshot 1`] = `
<div
class="t-tab-bar t-tab-bar--bordered t-tab-bar--fixed"
role="tablist"
>
<div
aria-label="TabBar"
aria-selected="false"
class="t-tab-bar-item t-no-border"
name="label_1"
role="tab"
text="标签一"
>
<div
class="t-tab-bar-item__content"
>
<div
class="t-tab-bar-item__icon"
>
<div
class="t-badge"
>
<div
class="t-badge__inner t-badge--has-children t-badge--medium t-badge--circle"
style="right: 0px; top: 5px;"
>
1
</div>
<svg
class="t-icon t-icon-app"
fill="none"
height="1em"
viewBox="0 0 16 16"
width="1em"
>
<path
d="M11.25 1.75a3 3 0 100 6 3 3 0 000-6zm-2 3a2 2 0 114 0 2 2 0 01-4 0zM2 3a1 1 0 011-1h3.5a1 1 0 011 1v3.5a1 1 0 01-1 1H3a1 1 0 01-1-1V3zm1 0v3.5h3.5V3H3zM2 9.5a1 1 0 011-1h3.5a1 1 0 011 1V13a1 1 0 01-1 1H3a1 1 0 01-1-1V9.5zm1 0V13h3.5V9.5H3zM8.5 9.5a1 1 0 011-1H13a1 1 0 011 1V13a1 1 0 01-1 1H9.5a1 1 0 01-1-1V9.5zm1 3.5H13V9.5H9.5V13z"
fill="currentColor"
fill-opacity="0.9"
/>
</svg>
</div>
</div>
<!--v-if-->
</div>
<transition-stub>
<!--v-if-->
</transition-stub>
</div>
<div
aria-label="TabBar"
aria-selected="false"
class="t-tab-bar-item t-no-border"
name="label_2"
role="tab"
text="标签二"
>
<div
class="t-tab-bar-item__content"
>
<div
class="t-tab-bar-item__icon"
>
<div
class="t-badge"
>
<div
class="t-badge__inner t-badge--has-children t-badge--medium t-badge--circle"
style="right: 0px; top: 5px;"
>
1
</div>
<svg
class="t-icon t-icon-app"
fill="none"
height="1em"
viewBox="0 0 16 16"
width="1em"
>
<path
d="M11.25 1.75a3 3 0 100 6 3 3 0 000-6zm-2 3a2 2 0 114 0 2 2 0 01-4 0zM2 3a1 1 0 011-1h3.5a1 1 0 011 1v3.5a1 1 0 01-1 1H3a1 1 0 01-1-1V3zm1 0v3.5h3.5V3H3zM2 9.5a1 1 0 011-1h3.5a1 1 0 011 1V13a1 1 0 01-1 1H3a1 1 0 01-1-1V9.5zm1 0V13h3.5V9.5H3zM8.5 9.5a1 1 0 011-1H13a1 1 0 011 1V13a1 1 0 01-1 1H9.5a1 1 0 01-1-1V9.5zm1 3.5H13V9.5H9.5V13z"
fill="currentColor"
fill-opacity="0.9"
/>
</svg>
</div>
</div>
<!--v-if-->
</div>
<transition-stub>
<!--v-if-->
</transition-stub>
</div>
<div
aria-haspopup="true"
aria-label="TabBar"
aria-selected="false"
class="t-tab-bar-item t-no-border"
name="label_3"
role="tab"
text="此处展开"
>
<div
class="t-tab-bar-item__content"
>
<div
class="t-tab-bar-item__icon"
>
<div
class="t-badge"
>
<div
class="t-badge__inner t-badge--has-children t-badge--medium t-badge--circle"
style="right: 0px; top: 5px;"
>
1
</div>
<svg
class="t-icon t-icon-app"
fill="none"
height="1em"
viewBox="0 0 16 16"
width="1em"
>
<path
d="M11.25 1.75a3 3 0 100 6 3 3 0 000-6zm-2 3a2 2 0 114 0 2 2 0 01-4 0zM2 3a1 1 0 011-1h3.5a1 1 0 011 1v3.5a1 1 0 01-1 1H3a1 1 0 01-1-1V3zm1 0v3.5h3.5V3H3zM2 9.5a1 1 0 011-1h3.5a1 1 0 011 1V13a1 1 0 01-1 1H3a1 1 0 01-1-1V9.5zm1 0V13h3.5V9.5H3zM8.5 9.5a1 1 0 011-1H13a1 1 0 011 1V13a1 1 0 01-1 1H9.5a1 1 0 01-1-1V9.5zm1 3.5H13V9.5H9.5V13z"
fill="currentColor"
fill-opacity="0.9"
/>
</svg>
</div>
</div>
<!--v-if-->
</div>
<transition-stub>
<!--v-if-->
</transition-stub>
</div>
</div>
`;
149 changes: 149 additions & 0 deletions src/tab-bar/__test__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { describe, it, expect, vi } from 'vitest';
import { mount } from '@vue/test-utils';
import TabBar from '../tab-bar.vue'
import TabBarItem from '../tab-bar-item.vue'
import { nextTick, ref } from 'vue';
import { AppIcon as TIconApp } from 'tdesign-icons-vue-next';

const list = [
{
name: 'label_1',
text: '标签一',
icon: undefined,
value: '1'
},
{
name: 'label_2',
text: '标签二',
icon: undefined,
value: '2'
},
{
name: 'label_3',
text: '此处展开',
icon: undefined,
value: '3',
subTabBar: [
{
name: 'spread_1',
text: '展开项一',
icon: undefined,
value: '3-1',
},
{
name: 'spread_2',
text: '展开项二',
icon: undefined,
value: '3-2',
},
{
name: 'spread_3',
text: '展开项三',
icon: undefined,
value: '3-3',
},
],
},
];

describe('TabBar', () => {
describe('props', () => {
it('bordered', async () => {
const wrapper = mount(TabBar, {
shallow: true
})
expect(wrapper.classes())
.toContain('t-tab-bar--bordered')

await wrapper.setProps({ 'bordered': false})
expect(wrapper.classes())
.not.toContain('t-tab-bar--bordered')
})

it('fixed', async () => {
const wrapper = mount(TabBar, {
shallow: true
})
expect(wrapper.classes())
.toContain('t-tab-bar--fixed')

await wrapper.setProps({ 'fixed': false})
expect(wrapper.classes())
.not.toContain('t-tab-bar--fixed')
})

it('defaultValue', async () => {
const wrapper = mount({
render: () => (
<TabBar defaultValue={"2"}>
{
list.map((item) => {
return <TabBarItem {...item}>{item.text}</TabBarItem>
})
}
</TabBar>
)
})

expect(wrapper.findAll('.t-tab-bar-item')).toHaveLength(list.length)
expect(wrapper.find('[aria-selected="true"]').attributes('text')).toEqual('标签二')
})

it ('snapshot', () => {
const icon = () => <TIconApp />
const wrapper = mount({
render: () => (
<TabBar>
{
list.map((item) => {
return <TabBarItem {...item} icon={icon} badgeProps={{count: 1}} />
})
}
</TabBar>
)
})
expect(wrapper.element).toMatchSnapshot();
})
})

describe('events', () => {
it ('click to change', async () => {
const onChange = vi.fn()
const value = ref('1')
const wrapper = mount({
render: () => (
<TabBar
v-model={value.value}
onChange={onChange}
>
{
list.map((item) => {
return <TabBarItem {...item} />
})
}
</TabBar>
)
})

// 从 bar1 切到 bar2
const bar2 = wrapper.find('[name="label_2"] > .t-tab-bar-item__content')
expect(onChange).toHaveBeenCalledTimes(0)
await bar2.trigger('click')
expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenLastCalledWith('2')

// 从 bar2 切到 bar3
const bar3 = wrapper.find('[name="label_3"] > .t-tab-bar-item__content')
await bar3.trigger('click')
expect(onChange).toHaveBeenCalledTimes(2)
expect(onChange).toHaveBeenLastCalledWith(['3'])

// 点击 spread
const bar3_1 = wrapper.find('[name="label_3"] .t-tab-bar-item__spread-item')
await bar3_1.trigger('click')
expect(onChange).toHaveBeenCalledTimes(3)
expect(onChange).toHaveBeenLastCalledWith(['3', '3-1'])
expect(wrapper.find('[name="label_3"]').find('.t-tab-bar-item__spread-item').exists()).toBe(false)
})
})
})
1 change: 1 addition & 0 deletions src/tab-bar/tab-bar-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default defineComponent({
isSpread.value = !isSpread.value;
if (!isToggleCurrent.value) {
updateChild([currentName]);
return;
}
}
updateChild(currentName);
Expand Down
2 changes: 1 addition & 1 deletion src/tab-bar/tab-bar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</template>

<script lang="ts">
import { defineComponent, ref, provide, watch, Ref, computed, SetupContext } from 'vue';
import { defineComponent, ref, provide, Ref, computed, SetupContext } from 'vue';
import TabBarProps from './props';
import config from '../config';
import { useDefault } from '../shared';
Expand Down

0 comments on commit 7edaae7

Please sign in to comment.