测试 Vue.js 组件
Testing Vue.js Component
我想测试一个 Vue.js 组件,但我失败了。简而言之,我正在设置一个组件属性,并且我想断言它设置正确。如果这很重要,则加载模块导出,并使用 Webpack 输出 JS。
// component
exports = module.exports = {};
module.exports = {
data: function () {
return {
active: false
};
},
methods: {
'close': function () {
console.log(this.active); // -> true
this.active = false;
console.log(this.active); // -> false
}
}
};
// component-test
var modal = require('../../resources/src/js/components/_component.js');
var assert = require('assert');
describe('close()', function () {
beforeEach(function () {
modal.data.active = true;
});
it('should set modal to inactive', function () {
console.log(modal.data.active); // -> true
modal.methods.close();
console.log(modal.data.active); // -> true
assert.equal(modal.data.active, false);
});
});
这应该给你一个关于如何在测试时加载 vue 组件的提示;
var modalComponent = require('../../resources/src/js/components/_component.js');
var assert = require('assert');
//load the component with a vue instance
vm = new Vue({
template: '<div><test v-ref:test-component></test></div>',
components: {
'test': modalComponent
}
}).$mount();
var modal = vm.$refs.testComponent;
describe('close()', function () {
beforeEach(function () {
modal.active = true;
});
it('should set modal to inactive', function () {
console.log(modal.active); // -> true
modal.close();
console.log(modal.active); // -> false
assert.equal(modal.active, false);
});
});
https://github.com/eddyerburgh/avoriaz 现在是 Vue 的官方测试库.js请查看有关设置以对组件进行断言的文档 https://eddyerburgh.gitbooks.io/avoriaz/content/
相关文章:
- 如何创建带有插槽的vue js组件预加载程序
- 如何在react js中将值从一个组件发送到另一个组件
- 如何在react js中移动第二个组件
- React js更改状态不会更新组件
- ember-js组件初始化不同的函数
- 复选框保持React JS中各组件的状态
- 如何在cycle js中从JSON数组创建组件
- Vue.js获取组件中的一个元素
- Ember.js(v2.4.5)组件不是't错过第二个动作
- 将自定义脚本加载到Vue.js组件中
- Ember.js:接受的子组件和父组件之间通信的最佳实践
- 使用react.js渲染其他组件内部的组件
- React.js不区分同一类的两个组件
- Ember.js:未在组件'中渲染的模型的计算属性;关于变更的参考
- 如何忽略node.js中其他文件或组件所需的文件
- React.js简单组件组成问题
- 在多个react.js组件中呈现json数据
- 以最封装的方式封装/封装ScalaJS react js组件(react stick)
- 需要来自单个JS文件的多个子模块与节点/组件.js
- 在组件.js内加载客户 sapui5 库