I'd like to test a Vue.js ponent, and I'm failing at that. Simply put, I'm setting a ponent property, and I want to assert that it is set correctly. If that matters, the module is loaded with exports, and the JS is output using Webpack.
// ponent
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
}
}
};
// ponent-test
var modal = require('../../resources/src/js/ponents/_ponent.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);
});
});
I'd like to test a Vue.js ponent, and I'm failing at that. Simply put, I'm setting a ponent property, and I want to assert that it is set correctly. If that matters, the module is loaded with exports, and the JS is output using Webpack.
// ponent
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
}
}
};
// ponent-test
var modal = require('../../resources/src/js/ponents/_ponent.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);
});
});
Share
Improve this question
asked Feb 8, 2016 at 22:55
veksenveksen
7,0035 gold badges22 silver badges33 bronze badges
2
- so what is actually failing? what's the output of your tests? have you check the vue js guide about testing? – Yerko Palma Commented Feb 9, 2016 at 11:53
- also check the webpack example on github, there are some test defined there, with karma + jasmine + phantomjs – Yerko Palma Commented Feb 9, 2016 at 11:57
2 Answers
Reset to default 6This should give you a hint on how to load vue ponents when testing;
var modalComponent = require('../../resources/src/js/ponents/_ponent.js');
var assert = require('assert');
//load the ponent with a vue instance
vm = new Vue({
template: '<div><test v-ref:test-ponent></test></div>',
ponents: {
'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./eddyerburgh/avoriaz is now the official testing library for Vue.js checkout the documentation on getting setup to make assertions on your ponents https://eddyerburgh.gitbooks.io/avoriaz/content/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742413874a4439356.html
评论列表(0条)