How to Unit Test Isolated Scope Directive in AngularJS

daniellmb

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');
Yair Tavor

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().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unit Test Directive with Isolated Scope

From Dev

How can I unit test isolated scope in directive

From Dev

Unit testing AngularJS Directive with isolated scope - how to get bindings to output?

From Dev

Unit testing AngularJS Directive with isolated scope - how to get bindings to output?

From Dev

Angularjs unit test isolated scope value

From Dev

angularjs directive isolated scope

From Dev

AngularJS Isolated Scope Directive

From Dev

AngularJS : How to change scope value from isolated scope within directive

From Dev

Angularjs TreeView Directive With Isolated Scope

From Dev

AngularJS : Directive Isolated Scope Undefined

From Dev

AngularJS overwrites isolated directive scope

From Dev

AngularJS directive: Updating the isolated scope

From Dev

AngularJS Directive Isolated Scope Issue

From Dev

How to access isolated $scope in customer AngularJS directive without remote template

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

Directive scope attributes without an isolated scope in AngularJS

From Dev

AngularJS directive Passing parameters to isolated scope function

From Dev

Bind ngModel to AngularJS directive with isolated scope

From Dev

Angularjs passing variables to a directive with an inherited (not isolated) scope

From Dev

AngularJS directive with isolated scope - default attributes values

From Dev

Angularjs directive with isolated scope needs clarification

From Dev

Pass variable to AngularJS directive without isolated scope

From Dev

AngularJS : Initializing isolated scope inside a directive

From Dev

AngularJs custom directive isolated scope custom fields

From Dev

angularjs : custom directive with isolated scope and passing argument

From Dev

AngularJS accessing isolated scope in child directive

From Dev

AngularJS : databinding in isolated scope of directive, using an object?

From Dev

AngularJS : directive with ng-repeat and isolated scope

Related Related

  1. 1

    Unit Test Directive with Isolated Scope

  2. 2

    How can I unit test isolated scope in directive

  3. 3

    Unit testing AngularJS Directive with isolated scope - how to get bindings to output?

  4. 4

    Unit testing AngularJS Directive with isolated scope - how to get bindings to output?

  5. 5

    Angularjs unit test isolated scope value

  6. 6

    angularjs directive isolated scope

  7. 7

    AngularJS Isolated Scope Directive

  8. 8

    AngularJS : How to change scope value from isolated scope within directive

  9. 9

    Angularjs TreeView Directive With Isolated Scope

  10. 10

    AngularJS : Directive Isolated Scope Undefined

  11. 11

    AngularJS overwrites isolated directive scope

  12. 12

    AngularJS directive: Updating the isolated scope

  13. 13

    AngularJS Directive Isolated Scope Issue

  14. 14

    How to access isolated $scope in customer AngularJS directive without remote template

  15. 15

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  16. 16

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  17. 17

    Directive scope attributes without an isolated scope in AngularJS

  18. 18

    AngularJS directive Passing parameters to isolated scope function

  19. 19

    Bind ngModel to AngularJS directive with isolated scope

  20. 20

    Angularjs passing variables to a directive with an inherited (not isolated) scope

  21. 21

    AngularJS directive with isolated scope - default attributes values

  22. 22

    Angularjs directive with isolated scope needs clarification

  23. 23

    Pass variable to AngularJS directive without isolated scope

  24. 24

    AngularJS : Initializing isolated scope inside a directive

  25. 25

    AngularJs custom directive isolated scope custom fields

  26. 26

    angularjs : custom directive with isolated scope and passing argument

  27. 27

    AngularJS accessing isolated scope in child directive

  28. 28

    AngularJS : databinding in isolated scope of directive, using an object?

  29. 29

    AngularJS : directive with ng-repeat and isolated scope

HotTag

Archive