Home Ask Login Register

Developers Planet

Your answer is one click away!

veksen February 2016

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);
  });
});

Answers


Sado Ogie February 2016

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); // -> true
        assert.equal(modal.active, false);
      });
    });

Post Status

Asked in February 2016
Viewed 1,119 times
Voted 9
Answered 1 times

Search




Leave an answer


Quote of the day: live life