2017-02-15 39 views
1

我在我的組件中有一個簡單的搜索框,我想單元測試在搜索框中鍵入文本時的行爲。PhantomJS中的Vue2單元測試,如何發送按鍵或觸發事件?

模板

<input type="text" placeholder="Search..." id="filterBox" v-on:input="updateFilter"> 

單元測試

import Vue from 'vue'; 
import Grid from 'src/components/Grid'; 

function create (Component, propsData) { 
    const Ctor = Vue.extend(Component); 
    return new Ctor({ propsData }).$mount(); 
} 

describe('Grid.vue',() => { 
    it('should debounce search',() => { 
    const vm = create(Grid, { 
     data: [], 
     columns: [], 
     initialSortKey: 'a' 
    }); 
    var box = vm.$el.querySelector('#filterBox'); 
    //TODO figure out how to send keys 
    }); 
}); 

我使用Vue2和PhantomJS單元測試。如何將密鑰發送到輸入框或觸發值更改事件?

回答

1

一個解決方案是使用jQuery。

var e = $.Event('keydown'); 
e.which = 56; // whatever keycode you need here 
$('#filterBox').trigger(e); 

所以現在你需要重複這樣做。但這是你如何觸發事件。

相關問題