Jas*_*ers 11 vue.js vuejs2 vue-test-utils
在Vue.js文档中,有一个自定义输入组件的示例.我试图找出如何为这样的组件编写单元测试.组件的用法如下所示
<currency-input v-model="price"></currency-input>
Run Code Online (Sandbox Code Playgroud)
完整的实施可以在https://vuejs.org/v2/guide/components.html#Form-Input-Components-using-Custom-Events找到
文件说
因此,对于要使用的组件
v-model,它应该(这些可以在2.2.0+中配置):
- 接受价值道具
- 使用新值发出输入事件
我如何编写一个单元测试来确保我已经编写了这个组件以便它可以使用v-model?理想情况下,我不想专门测试这两个条件,我想测试一下行为,当组件内的值发生变化时,它也会在模型中发生变化.
acd*_*ior 20
你能行的:
<currency-input><currency-input>并使用它转换的值(13.467由转换<currency-input>为12.46)price属性(绑定到v-model)是否已更改.示例代码(使用Mocha):
import { mount } from '@vue/test-utils'
import CurrencyInput from '@/components/CurrencyInput.vue'
describe('CurrencyInput.vue', () => {
it("changing the element's value, updates the v-model", () => {
var parent = mount({
data: { price: null },
template: '<div> <currency-input v-model="price"></currency-input> </div>',
components: { 'currency-input': CurrencyInput }
})
var currencyInputInnerTextField = parent.find('input');
currencyInputInnerTextField.element.value = 13.467;
currencyInputInnerTextField.trigger('input');
expect(parent.vm.price).toBe(13.46);
});
});
Run Code Online (Sandbox Code Playgroud)
var CurrencyInput = Vue.component('currency-input', {
template: '\
<span>\
$\
<input\
ref="input"\
v-bind:value="value"\
v-on:input="updateValue($event.target.value)">\
</span>\
',
props: ['value'],
methods: {
// Instead of updating the value directly, this
// method is used to format and place constraints
// on the input's value
updateValue: function(value) {
var formattedValue = value
// Remove whitespace on either side
.trim()
// Shorten to 2 decimal places
.slice(0, value.indexOf('.') === -1 ? value.length : value.indexOf('.') + 3)
// If the value was not already normalized,
// manually override it to conform
if (formattedValue !== value) {
this.$refs.input.value = formattedValue
}
// Emit the number value through the input event
this.$emit('input', Number(formattedValue))
}
}
});
// specs code ///////////////////////////////////////////////////////////
var mount = vueTestUtils.mount;
describe('CurrencyInput', () => {
it("changing the element's value, updates the v-model", () => {
var parent = mount({
data() { return { price: null } },
template: '<div> <currency-input v-model="price"></currency-input> </div>',
components: { 'currency-input': CurrencyInput }
});
var currencyInputInnerTextField = parent.find('input');
currencyInputInnerTextField.element.value = 13.467;
currencyInputInnerTextField.trigger('input');
expect(parent.vm.price).toBe(13.46);
});
});
// load jasmine htmlReporter
(function() {
var env = jasmine.getEnv()
env.addReporter(new jasmine.HtmlReporter())
env.execute()
}())Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css">
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<script src="https://npmcdn.com/vue@2.5.15/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-template-compiler@2.5.15/browser.js"></script>
<script src="https://rawgit.com/vuejs/vue-test-utils/2b078c68293a41d68a0a98393f497d0b0031f41a/dist/vue-test-utils.iife.js"></script>Run Code Online (Sandbox Code Playgroud)
注意:上面的代码工作正常(如您所见),但v-model很快就会对测试进行改进.请按照此问题获取最新信息.
| 归档时间: |
|
| 查看次数: |
6596 次 |
| 最近记录: |