Vite Vue List

子組件 Nav.vue單元測試

父組件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<template>
<Nav
:navLists="navLists"
:pageNameActive="pageNameActive"
@sendMenuClickActive="menuClickActive"
/>
<main>
<KeepAlive>
<router-view />
</KeepAlive>

</main>
<footer>
<div class="reserved">LaraHuang 版權所有</div>
</footer>

</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import Nav from '../components/Nav.vue';

import {navType} from '../types/nav'

const navLists = ref<navType[]>([
{ id_set: '001', title: 'Home', href: '/' ,icon: 'icon-location2' },
{ id_set: '002', title: 'List', href: '/list',icon: 'icon-cog' },
{ id_set: '003', title: 'Pinia', href: '/pinia',icon: 'icon-cog' },
])
const pageNameActive=ref<string>('001');
const menuClickActive =(item:navType)=>{
// pageNameActive.value= localStorage.getItem('pageName')as string
pageNameActive.value = item.id_set;
}
</script>

測試子組件如下Nav.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<template>
<div class="flex justify-center header">
<a href="/me"> <b class="flex justify-center"><h1>Lara 單元測試</h1></b> </a>
</div>
<ul class="nav flex justify-center h-12 leading-10 border_bottom_solid_2 bg_c6e2ff">
<li class="flex mx-2"
v-for="(item,id_set) in navLists" :key="id_set"
:class="pageNameActive == item.id_set ? 'isActive' : ''"
@click="sendMenuClickActive(item)"
>
<router-link :to="item.href" data-test="button" >
{{ item.title }}
</router-link>
</li>
</ul>
</template>

<script setup lang="ts">
import {navType} from '../types/nav'
const props = defineProps({
pageNameActive:{type:String},
navLists:{type:Object}
})
const emit = defineEmits(['sendMenuClickActive'])
const sendMenuClickActive=(_item:navType)=>{
emit('sendMenuClickActive',_item)
}
</script>
mount:掛載。
props:父傳子。
data-test:在html綁定。
findAll():與 類似find,傳回數組DOMWrapper。
expect(value):攥寫每一筆測試時都會使用 expect(value) 和匹配器 (matcher) 來『斷言』某個值,expect (value) 的參數 value 會是程式碼產生的值
toContain('C'):包含
findComponent():尋找 Vue 元件實例,VueWrapper如果找到則傳回。否則返回ErrorWrapper。
emitted():子傳遞給父元件
toHaveLength(Number):Length數量
toEqual():等於
toHaveProperty():屬性的值
測試內容 describe:是用來將一至多組有相關的測試組合在一起的區塊。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import Nav from '../Nav.vue'
//Nav.vue 從父元件frontLayout.vue獲得以下陣列
describe('Nav.vue 從父元素 frontLayout.vue 取得以下陣列', () =>
{
const navLists = [
{ id_set: '001', title: 'Home', href: '/' ,icon: 'icon-location2' },
{ id_set: '002', title: 'List', href: '/list',icon: 'icon-cog' },
{ id_set: '003', title: 'Pinia', href: '/pinia',icon: 'icon-cog' },
]
const pageNameActive = '001';
let wrapper
if (typeof document !== 'undefined') {
wrapper = mount(Nav, {
props: {
navLists: navLists,
pageNameActive: pageNameActive
}
})
}

it('navLists資料是否正確渲染訊息? ', () =>
{
if (typeof document !== 'undefined') {
//尋找所有 li標籤
const items = wrapper.findAll('li');

expect(items.length).toBe(items.length);
expect(items[0].text()).toBe('Home');
expect(items[1].text()).toBe('List');
expect(items[2].text()).toBe('Pinia');
}


});
it('Active 傳遞初始值時渲染', () => {
expect(pageNameActive).toMatch(pageNameActive);
})

it('點擊時發出當前資料的事件,Li會新增isActive Class', async() =>
{
if (typeof document !== 'undefined') {
const items = wrapper.findAll('li');
const li = wrapper.find('li');
// 點擊按鈕
await wrapper.get('[data-test="button"]').trigger('click')
//斷言唯一碼必想等
expect(pageNameActive).toMatch(items[0].id_set);
expect(pageNameActive).toMatch(items[1].id_set)
expect(pageNameActive).toMatch(items[2].id_set)
// 斷言li 會包含一個isActive Class
expect(wrapper.get('li').classes()).toContain('isActive')
}
})
it('按一下時檢索當前資料的事件', async () =>
{
if (typeof document !== 'undefined') {
// 點擊按鈕
await wrapper.get('[data-test="button"]').trigger('click')
// 子傳父到sendMenuClickActive 函式
expect(wrapper.emitted()).toHaveProperty('sendMenuClickActive')
}
})
})