81

What is a good way to unit test isolated scope in AngularJS

JSFiddle showing unit test

Directive snippet

    scope: {name: '=myGreet'},
    link: function (scope, element, attrs) {
        //show the initial state
        greet(element, scope[attrs.myGreet]);

        //listen for changes in the model
        scope.$watch(attrs.myGreet, function (name) {
            greet(element, name);
        });
    }

I want to ensure the directive is listening for changes - this does not work with an isolated scope:

    it('should watch for changes in the model', function () {
        var elm;
        //arrange
        spyOn(scope, '$watch');
        //act
        elm = compile(validHTML)(scope);
        //assert
        expect(scope.$watch.callCount).toBe(1);
        expect(scope.$watch).toHaveBeenCalledWith('name', jasmine.any(Function));
    });

UPDATE: I got it to work by checking if the expected watchers were added to the child scope, but it's very brittle and probably using the accessors in an undocumented way (aka subject to change without notice!).

//this is super brittle, is there a better way!?
elm = compile(validHTML)(scope);
expect(elm.scope().$$watchers[0].exp).toBe('name');

UPDATE 2: As I mentioned this is brittle! The idea still works but in newer versions of AngularJS the accessor has changed from scope() to isolateScope():

//this is STILL super brittle, is there a better way!?
elm = compile(validHTML)(scope);                       
expect(elm.isolateScope().$$watchers[0].exp).toBe('name');
SavoryBytes
  • 35,571
  • 4
  • 52
  • 61

4 Answers4

102

See angular element api docs. If you use element.scope() you get the element's scope that you defined in the scope property of your directive. If you use element.isolateScope() you get the entire isolated scope. For example, if your directive looks something like this :

scope : {
 myScopeThingy : '='
},
controller : function($scope){
 $scope.myIsolatedThingy = 'some value';
}

Then calling element.scope() in your test will return

{ myScopeThingy : 'whatever value this is bound to' }

But if you call element.isolateScope() you'll get

{ 
  myScopeThingy : 'whatever value this is bound to', 
  myIsolatedThingy : 'some value'
}

This is true as of angular 1.2.2 or 1.2.3, not sure exactly. In previous versions you had only element.scope().

Yair Tavor
  • 2,538
  • 1
  • 20
  • 16
  • 1
    v1.2.3 feat(jqLite): expose isolateScope() getter similar to scope() https://github.com/angular/angular.js/commit/27e9340b3c25b512e45213b39811098d07e12e3b – SavoryBytes Dec 03 '13 at 22:15
  • 1
    but where do you spy on the $watch method? – tusharmath Apr 30 '14 at 11:32
  • 1
    you could expose the function that runs on the $watch and then spy on it. In the directive, set "scope.myfunc = function()...", then in the $watch do "$scope.$watch('myName', scope.myfunc);". Now in the test you can get myFunc from the isolated scope and spy on it. – Yair Tavor Apr 30 '14 at 14:03
  • 22
    Doesn't work for me. `element.isolateScope()` returns `undefined`. And `element.scope()` returns a scope that doesn't contain all the stuff that I put on my scope. – mcv Aug 28 '14 at 15:12
  • Wish I had more upvotes to give. You helped me after an hour of scratching my head. – taylonr Sep 01 '14 at 23:53
  • 4
    @mcv I found I needed to do `element.children().isolateScope()` – Will Keeling Sep 17 '14 at 14:48
  • 1
    @mcv - are you using templateUrl in your directive? if so you'll need to wrap the isolateScope call in a $timeout to access it. I guess because the $http call for the template load runs after the call to isolateScope (if you don't use the timeout) – user1821052 May 22 '15 at 20:50
  • 1
    Need to call scope.$digest before being able to call element.isolateScope – Matsemann Oct 01 '15 at 08:29
  • *Warning*: `.scope()` and `.isolateScope()` are not available if debugInfoEnabled is false. It means you miss the performance improvement if you want to use these methods. More information: https://docs.angularjs.org/guide/production#disabling-debug-data – Arashsoft Dec 14 '17 at 16:50
11

You can do var isolateScope = myDirectiveElement.scope() to get the isolate scope.

You don't really need to test that $watch was called though.. that's more testing angularjs than testing your app. But I guess it's just an example for the question.

Andrew Joslin
  • 43,033
  • 21
  • 100
  • 75
  • 2
    I'm not sure I agree it's "testing angular" I'm not testing that $watch works but simply that the directive is property "wired-up" to angular. – SavoryBytes Jun 28 '13 at 22:24
  • 1
    Also daniellmb, the way to test this would be to expose your `greet` function and spy on that, and check if that is called - not the $watch. – Andrew Joslin Jul 06 '13 at 12:53
  • Right, this is a contrived example, but I was interested if there is a clean way to test isolate scope. Breaking encapsulation and putting methods on the scope wouldn't work in this case as there's no hook to add the spy before it's called. – SavoryBytes Jul 08 '13 at 15:25
  • @AndyJoslin, Out of curiosity, why create a `isolateScope` variable at all? See Ang's comment on this egghead video (https://egghead.io/lessons/angularjs-unit-testing-directive-scope): As of Angular 1.2, to retrieve the isolated scope, one needs to use `element.isolateScope()` instead of `element.scope()` http://code.angularjs.org/1.2.0/docs/api/angular.element – Danger14 May 14 '14 at 15:47
1

move the logic to a separate controller, ie:

//will get your isolate scope
function MyCtrl($scope)
{
  //non-DOM manipulating ctrl logic here
}
app.controller(MyCtrl);

function MyDirective()
{
  return {
    scope     : {},
    controller: MyCtrl,
    link      : function (scope, element, attrs)
    {
      //moved non-DOM manipulating logic to ctrl
    }
  }
}
app.directive('myDirective', MyDirective);

and test latter as you would any controller - passing the scope object in directly (see Controllers section here for an example).

if you need to trigger $watch in your test do:

describe('MyCtrl test', function ()
{
  var $rootScope, $controller, $scope;

  beforeEach(function ()
  {
    inject(function (_$rootScope_, _$controller_)
    {
      // The injector unwraps the underscores (_) from around the parameter names when matching
      $rootScope = _$rootScope_;
      $controller = _$controller_;
    });

    $scope = $rootScope.$new({});
    $scope.foo = {x: 1}; //initial scope state as desired
    $controller(MyCtrl, {$scope: $scope}); //or by name as 'MyCtrl'
  });

  it('test scope property altered on $digest', function ()
  {
    $scope.$digest(); //trigger $watch
    expect($scope.foo.x).toEqual(1); //or whatever
  });
});
Nikita
  • 6,019
  • 8
  • 45
  • 54
0

I'm not sure it's possible with isolate scope (although I hope someone proves me wrong). The isolate scope that gets created in the directive is, well, isolated, so the $watch method in the directive is different from the scope that you're spying on in the unit test. If you change scope: {} to scope: true, the directive scope will inherit prototypically and your tests should pass.

I guess this isn't the most ideal solution, because sometimes (a lot of the time), isolate scope is a good thing.

UnicodeSnowman
  • 1,639
  • 16
  • 13