angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

unit testing directive with isolate scope

Open ajs2 opened this issue 13 years ago • 11 comments

I have created a demo project which reproduces the issue I found. Perform the following to reproduce:

$> git clone https://github.com/ajs2/angularjs-directive
$> npm install  
$> bower install
$> karma start karma.conf.js

I have also tried to reproduce the issue in a jsfiddle here but there it doesn't fail.

Anyway, in that project is just one directive with one unit test file. The problem I found occurred when I implemented an isolated scope in the directive.

This is the directive:

angular.module('MyTest')
    .directive('myFoo', ['$rootScope',
        function ($rootScope) {
            return {
                restrict: 'E',
                replace: true,
                scope: {},
                templateUrl: '/views/my-foo.html',
                link: function ($scope, $element, $iAttrs, controller) {
                    $scope.bar = 'foo';
                },
                controller: function ($scope) {
                    $scope.foo = 'bar';
                }
            };
        }]);

and the test I wrote tests the existence of the property 'foo' on the directive's scope as follows

var scope = $rootScope.$new(),
   element = angular.element('<div><my-foo></my-foo></div>');
element = $compile(element)(scope);
scope.$digest();

expect(element.scope().foo).toEqual('bar'); // fails here
expect(element.scope().$id).not.toEqual(scope.$id); // fails too, they are the same!

Is this a bug, or is there an explanation why these karma tests fail and the jsfiddle does not ?

ajs2 avatar Nov 02 '13 18:11 ajs2

Same as #4640 ?

pkozlowski-opensource avatar Nov 02 '13 19:11 pkozlowski-opensource

No its definitely different (thats why I created this issue)! That issue you mention is about not having a scope at all, so a test like

expect(element.scope()).toBeDefined();

would fail. This issue is about not being able to access the properties set by the directive on the scope.

ajs2 avatar Nov 02 '13 19:11 ajs2

After upgrade from 1.2.0-rc3 to stable 1.2.0 I'm not able to access isolated scope in my specs. Here is an example http://jsfiddle.net/lucassus/pTv49/1/ This example works fine with rc3 version. The workaround for this issue is directiveScope = element.scope().$$childHead but it's not very elegant code.

lucassus avatar Nov 11 '13 11:11 lucassus

https://github.com/angular/angular.js/commit/27e9340b3c25b512e45213b39811098d07e12e3b @lucassus

caitp avatar Nov 11 '13 11:11 caitp

How about this: http://jsfiddle.net/pTv49/3/

markkra avatar Apr 05 '14 00:04 markkra

@markkra thanks for your fiddle dude, that helped me figure out my issue. Basically isolateScope() doesn't work until after the $digest call.

tapmantwo avatar Apr 16 '14 15:04 tapmantwo

@markkra thanks so much for this, man, I spent about 2 hours trying to figure this out today. this is the only reference i've found online to el.isolateScope().

davidagee avatar Apr 17 '14 06:04 davidagee

this is the only reference i've found online to el.isolateScope().

Did you try the official API docs? =)

caitp avatar Apr 17 '14 06:04 caitp

I did, but didn't think to look under angular.element. Google search for "isolatescope" doesn't come up with any relevant Angular API doc.

davidagee avatar Apr 17 '14 17:04 davidagee

I added 'passing value from outside scope to isolated scope' which is missing on markrra's demo, check this out http://jsfiddle.net/vd1mjf0g/

ypwu1 avatar Sep 10 '14 01:09 ypwu1

Awesome! This is the second time this same issue saved my day (stupid brain)

rpedroni avatar Nov 10 '14 16:11 rpedroni