lundi 29 août 2016

unit testing angular components

I have the following project structure

  • project
    • app
      • app.module.js
      • components
        • comp1
        • index.js
        • comp1.component.js
        • comp1.html
        • test.spec.js
      • comp2
      • ...

The content of /app/components/comp1/index.js is:

    angular.module('TestModule')
    .component('comp1', require('./comp1.component'));

The content of /app/components/comp1/comp1.component.js is:

const template = require('./comp1.html');

class Comp1Controller {
    constructor () {
        var panes = this.panes = ['pane1', 'pane2'];
    }
 }
 addPane(pane) {
  if (panes.length === 0) {
    this.select(pane);
  }
  ....
}
}

module.exports = {
   controller: [ Comp1Controller ],
   templateUrl: template
};

I am trying to unit test comp1 component; however, I am getting undefined is not a constructor error. This is my test.spec.js:

var TestModule = require('../../app.module');
...
describe('Component: comp1.component', function () {
    var $componentController;

    beforeEach(angular.mock.module('TestModule'));

    beforeEach(inject(function(_$componentController_) {
      $componentController = _$componentController_;
    }));

   it('test component', function() {

     var ctrl = $componentController('comp1', null, null);

     expect(ctrl).toBeDefined();    
 });
});

ERROR: undefined is not a constructor (evaluating 'expect(ctrl).toBeDefined()')

Will appreciate if anyone has any suggestions.

Aucun commentaire:

Enregistrer un commentaire