Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Vue.js Component

I'd like to test a Vue.js component, and I'm failing at that. Simply put, I'm setting a component 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.

// 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);
  });
});
like image 731
veksen Avatar asked Feb 08 '16 22:02

veksen


Video Answer


1 Answers

This should give you a hint on how to load vue components when testing;

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);
    });
});
like image 114
Ogie Avatar answered Oct 10 '22 22:10

Ogie