@eventmobi/angular-easy-test

3.0.3 • Public • Published

angular-easy-test

Circle CI Dependency Status devDependency Status npm version

A library that makes Angular unit tests easier to write.

install

npm install @eventmobi/angular-easy-test

usage

CommonJS

var EasyTest = require('@eventmobi/angular-easy-test');

AMD

define(['angular-easy-test'], function(EasyTest) {
  ...
});

Browser Global

<script src="angular.js"></script>
<script src="angular-mocks.js"></script>
<script src="angular-easy-test.js"></script>

Here are some examples of using EasyTest with Chai.

describe('MyService', function() {

  beforeEach(function() {
    EasyTest.mockModules('myModule myDependentModule');
    var services = EasyTest.injectify([ 'ServiceOne', '$q' ]);
    services.ServiceOne.functionToFake = function() {
      return services.$q.when('something');
    };
  });

  it('should look like my service', function(done) {
    var res = EasyTest.testService('MyService', {
      'function': 'one two',
      'number': 'three'
    });
    done(res);
  });

  it('some controller stuff', function() {
    var context = EasyTest.createTestContext('MyController');
    expect(context.$scope).to.have.property('something');
    context.controller.myFunction();
    expect(context.controller.state).to.equal('something');
  });

  it('some service stuff', function() {
    var service = EasyTest.getService('MyService');
    service.something();
    EasyTest.getService('$rootScope').$digest();
    expect(service.property).to.equal('something');
  });

  it('some directive stuff', function() {
    var element = EasyTest.compileDirective('<div my-directive></div>');
    expect(element.find('li')).to.have.length(10);
  });

});

comparison with ngMock

angular-easy-test is built on top of AngularJS's ngMock. Here is the same set of test cases implemented using just ngMock:

describe('MyService', function() {

  beforeEach(module('myModule', 'myDependentModule', function($provide, $q) {
    $provide.value('ServiceOne', {
      functionToFake: function() {
        return $q.when('something');
      };
    })
  }));

  it('should look like my service', inject(function(MyService) {
    expect(MyService.one).to.be.a('function');
    expect(MyService.two).to.be.a('function');
    expect(MyService.three).to.be.a('number');
  }));

  it('some controller stuff', inject(function($rootScope, $controller) {
    var scope = $rootScope.$new();
    var ctrl = $controller('MyController', {
      $scope: scope
    })
    expect(scope).to.have.property('something');
    ctrl.myFunction();
    expect(ctrl.state).to.equal('something');
  }));

  it('some service stuff', inject(function($rootScope, MyService) {
    MyService.something();
    $rootScope.$digest();
    expect(MyService.property).to.equal('something');
  }));

  it('some directive stuff', inject(function($compile, $rootScope) {
    var element = $compile('<div my-directive></div>')($rootScope.$new());
    expect(element.find('li')).to.have.length(10);
  }));

});

documentation

See the jsdocs.

Package Sidebar

Install

npm i @eventmobi/angular-easy-test

Weekly Downloads

0

Version

3.0.3

License

MIT

Unpacked Size

57.7 kB

Total Files

13

Last publish

Collaborators

  • em-user