cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <body ng-app="numbersApp" ng-controller="numbersController">

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine.css">

<!-- the order that these files load is critical, think twice before changing -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine-html.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/boot.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-mocks.js"></script>

  <p>
    <strong>Your task:</strong> Create the Jasmine unit tests for the directive.
  </p>
  <p>
    Number A: {{numbersObj.numberA}}<br />
    Number B: {{numbersObj.numberB}}<br />
    Sum: {{add(numbersObj.numberA, numbersObj.numberB)}}<br />
    Difference: {{subtract(numbersObj.numberA, numbersObj.numberB)}}
  </p>
  
<numbers-directive test-obj="numbersObj" add="add" subtract="subtract"></numbers-directive>
  
</body>
            
          
!
            
              var myApp = angular.module("numbersApp",[]);

myApp.controller("numbersController", ["$scope",function($scope){
  $scope.numbersObj = {
    numberA: 2,
    numberB: 3
  };

  $scope.add = function(a, b){
    return a + b;
  };
  $scope.subtract = function(a, b){
    return a - b;
  };
}]);

myApp.directive("numbersDirective", [function(){
  return {
    restrict: 'E', 
    replace: true,
    scope: {
      testObj: "=",
      add: "=",
      subtract: "="
    },
    template: '<div>Directive: {{testObj.numberA}}, {{testObj.numberB}}'+
    '<p><button ng-click="increment(testObj, \'numberA\')">Increment First</button> <button ng-click="decrement(testObj, \'numberA\')">Decrement First</button></p>'+
    '<p><button ng-click="increment(testObj, \'numberB\')">Increment Second</button> <button ng-click="decrement(testObj, \'numberB\')">Decrement Second</button></p>'+
    '<p><button ng-click="incrementBoth()">Increment Both</button> <button ng-click="decrementBoth()">Decrement Both</button></p></div>',
    link: function(scope, elem, attrs){
      scope.increment = function(numberObj, key){
        if(!!key){
          numberObj[key] = scope.add(numberObj[key], 1);
          return numberObj[key];
        }else{
          return numberObj++;
        }
      };
      
      scope.decrement = function(numberObj, key){
        if(!!key){
          numberObj[key] = scope.subtract(numberObj[key], 1);
          return numberObj[key];
        }else{
          return numberObj++;
        }
      };

      scope.incrementBoth = function(){
        scope.increment(scope.testObj, "numberA");
        scope.increment(scope.testObj, "numberB");
      };
      scope.decrementBoth = function(){
        scope.decrement(scope.testObj, "numberA");
        scope.decrement(scope.testObj, "numberB");
      };
    }
  };
}]);


///////////////// The Tests //////////////////

describe('numbersController', function() {
  var $scope;

  beforeEach(module('numbersApp'));

  beforeEach(inject(function ($rootScope, $controller) {
    $scope = $rootScope.$new();
    $controller('numbersController', {
      $scope: $scope 
    });
  }));

  describe('add method', function(){
    it("should add two integer numbers.", function() {    
      expect($scope.add(2,3)).toEqual(5);
    });
  });

  describe('subtract method', function(){
    it("should subtract two integer numbers.", function() {    
      expect($scope.subtract(3,1)).toEqual(2);
    });
  });
});


describe('numbersDirective directive', function() {
  var scope, elem, isolateScope, $compile;

  beforeEach(module('numbersApp'));

  beforeEach(inject(function ($rootScope, _$compile_) {
    scope = $rootScope.$new();
    $compile = _$compile_;
    
    //TODO: set up mocks
  }));
  
  function compileDirective(template){
    if(!template){
      //TODO: inject mocks here
      template = '<numbers-directive></numbers-directive>'
    }
    
    elem = angular.element(template)

    inject(function($compile){
      $compile(elem)(scope);
    });

    scope.$digest();
    isolateScope = elem.isolateScope();
  }

  beforeEach(function(){
    compileDirective();
  });
    
  describe('', function(){
    //TODO: fill this in
  });
});



            
          
!
999px
Loading ..................

Console