国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

詳解關(guān)于Vue單元測試的幾個坑

瀏覽:2日期:2023-01-24 15:56:28

一、寫在前面

這篇文章的代碼使用karma,mocha,chai,sinon-chai配合Vue的實例屬性進行單元測試

二、全局的組件的坑

由于我的g-icon是全局注冊的,所以使用g-input組件時的時候g-icon是直接用的,所以測試時有關(guān)icon的代碼永遠(yuǎn)是錯的。

把g-icon局部注冊的組件

三、在測試中觸發(fā)點擊事件

模擬我在app.vue里使用g-input組件

<g-input v-model='message'></g-input>

使用new event 和 dispatch 模擬事件在組件上觸發(fā),雖然這個事件和我們實際的事件不一樣,但名字一樣就夠了,測試回調(diào)函數(shù)自帶的參數(shù)

it('支持事件', () => { ['change', 'input', 'focus', 'blur'].forEach(eventName => { vm = new Constructor({}).$mount(); const callback = sinon.fake(); vm.$on(eventName, callback); let event = new Event(eventName); Object.defineProperty(event, 'target', { value: { value: 'hi' }, enumerable: true }); let inputElement = vm.$el.querySelector('input'); inputElement.dispatchEvent(event); expect(callback).to.have.been.calledWith('hi'); }); });

測試這個組件事件觸發(fā)時,回調(diào)的參數(shù),由于自定義事件沒有target,我們需要自己寫上去

value: { value: 'hi' }第一個value是defineProperty的

四、Vue的版本

坑來自于下面一段代碼

it('接受gutter', function(done) { Vue.component('g-row', Row); Vue.component('g-col', Col); const div = document.createElement('div'); document.body.appendChild(div); div.innerHTML = ` <g-row gutter='20'> <g-col></g-col> <g-col></g-col> </g-row>`; const vm = new Vue({ el: div }); setTimeout(() => { const row = vm.$el.querySelector('.row'); expect(getComputedStyle(row).marginRight).to.eq('-10px'); expect(getComputedStyle(row).marginLeft).to.eq('-10px'); const cols = vm.$el.querySelectorAll('.col'); expect(getComputedStyle(cols[0]).paddingRight).to.eq('10px'); expect(getComputedStyle(cols[1]).paddingLeft).to.eq('10px'); done(); vm.$el.remove(); vm.$destroy(); }, 0); });

我使用直接在el上寫入template代碼,所以我默認(rèn)的import Vue from 'vue'(runtimeonly版本)無法編譯這個代碼,import Vue from '../node_modules/vue/dist/vue.esm.js'使用上面引入即可

在沒有template選項是,el不替換

五、異步測試

還是這個代碼,先看以下測試兩個組件關(guān)系

it('接受gutter', function(done) { Vue.component('g-row', Row); Vue.component('g-col', Col); const div = document.createElement('div'); document.body.appendChild(div); div.innerHTML = ` <g-row gutter='20'> <g-col></g-col> <g-col></g-col> </g-row>`; const vm = new Vue({ el: div }); setTimeout(() => { const row = vm.$el.querySelector('.row'); expect(getComputedStyle(row).marginRight).to.eq('-10px'); expect(getComputedStyle(row).marginLeft).to.eq('-10px'); const cols = vm.$el.querySelectorAll('.col'); expect(getComputedStyle(cols[0]).paddingRight).to.eq('10px'); expect(getComputedStyle(cols[1]).paddingLeft).to.eq('10px'); done(); vm.$el.remove(); vm.$destroy(); }, 0); });

先說為什么需要seTimeout

從created和mounted鉤子說起,createElement和appendChild在js代碼是同步的,兩個鉤子分別在這兩段代碼后執(zhí)行,鉤子異步執(zhí)行的。

由于我們在g-row組件中有mounted鉤子,所以我們必須得進行異步檢測,否則我們在new Vue之后立馬進行測試,鉤子還沒執(zhí)行完。

mocha異步測試

mocha默認(rèn)不執(zhí)行異步,加入done參數(shù),調(diào)用done()就可以

六、垃圾回收

每一個測試完成之后,都要寫下面兩條代碼

vm.$el.remove(); vm.$destroy();

有兩個作用:

銷毀在頁面中的數(shù)據(jù) 銷毀在內(nèi)存的數(shù)據(jù)

雖然js是單線程,但是還有一個dom線程

var div = document. getElementById(’xxx’)div.onclick = function() { ///code}setTimeout(function(){ div. remove()}, 3000)

現(xiàn)在我們討論,什么時候div上的函數(shù)被回收

函數(shù)被全局變量div上的onlick引用了

div.remove()只是在頁面刪掉了,沒有被內(nèi)存刪掉

var div = document. getElementById(’xxx’)div.onclick = function() { ///code}setTimeout(function(){ div = mull}, 3000)

這個函數(shù)并沒有被刪除,函數(shù)是寫在dom上的,div變量只是引用了dom對象

var div = document. getElementById(’xxx’)div.onclick = function() { ///code}setTimeout(function(){ var div2 = document. getElementById(’xxx’)}, 3000)

div= null和div.remove同時做就可以了,分別從內(nèi)存和dom上刪除了

ie有bug,即使這樣都刪不了,div.onlick = null 可以

到此這篇關(guān)于關(guān)于Vue單元測試的幾個坑的文章就介紹到這了,更多相關(guān) Vue單元測試 內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 久久精品中文字幕不卡一二区 | 欧美高清在线精品一区二区不卡 | avhd101天天看新片 | 欧美成人精品久久精品 | 很黄很色的摸下面的视频 | 香蕉久久高清国产精品免费 | 国产成人18黄网站在线观看网站 | 国产免费久久 | 国产一级一片免费播放 | 免费国产综合视频在线看 | 精品视频一二三区 | 日韩久久精品 | 国产高清精品自在线看 | 老司机午夜性生免费福利 | 成人国产在线视频 | 天堂1在线观看 | 99视频福利 | 中文字幕在线看片成人 | 大陆老头xxxxxhd | 成人免费大片a毛片 | 99久久精品国产一区二区成人 | 国产成人综合精品一区 | 综合在线视频精品专区 | 国产高清在线精品免费 | japonensis国产福利| 欧美 亚洲 另类 自拍 在线 | 性做久久久久免费看 | 天天亚洲 | 亚洲精品国产啊女成拍色拍 | 欧美亚洲日本国产 | 手机在线毛片免费播放 | 色综合久久88色综合天天提莫 | 久久久免费观看视频 | 欧美视频精品 | 97精品国产高清在线看入口 | 一级片久久 | 国产激情一区二区三区在线观看 | avove旗袍丝袜高跟啪啪 | 亚洲综合91| 精品videosex性欧美 | 久香草视频在线观看免费 |