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

issue with unit-testing directive using karma

Open ajs2 opened this issue 13 years ago • 17 comments

I'm unit testing my directive and in my unit test I only have

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

element = $compile(element)(scope);
scope.$digest();

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

This test fails :( To reproduce this I have created a small angular project here on github, with only 1 directive and only one unit test. To reproduce do

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

I was able to fix this test by doing one the following

  1. In the directive add to the 'link' function
$element.data('$scope', $scope);
  1. or change 'replace' into
replace: false 
  1. remove jquery from karma.conf.js

It doesn't make any sense, but it demonstrates that this situation is weird (I think). Can someone comment on this, because after many hours of tweaking I start to think that I have hit a bug here!

ajs2 avatar Oct 25 '13 08:10 ajs2

yep, so you removed jquery from the karma configuration. But shouldn't angular work with jquery as well ?

Subject: Re: [angular.js] issue with unit-testing directive using karma (#4640)

Your test passes when running with jqLite.

— Reply to this email directly or view it on GitHub.

ajs2 avatar Oct 25 '13 09:10 ajs2

Sorry, I posted that before realizing that you had mentioned this in your original post.

Anyways I was going to add that it is a bit curious, because the core tests are not failing en masse with jQuery. Is it possible that we are missing some important tests in the jQuery suite which should check that the extended functions work correctly? It might be a legitimate problem to be tracked (I'm not sure if it's a known issue, I haven't personally seen this issue with jQuery + angular yet)

caitp avatar Oct 25 '13 10:10 caitp

It looks to me like compileSpec is basically covering this test case, with the exception that your directive adds a controller as well. Since the tests are passing, I'm sure this is an issue with the way the directive is being written or tested

The following change:

        element = $compile('<div><my-foo></my-foo></span>')(scope);
        scope.$digest();
        expect(scope.bar).toEqual('foo'); // Not really needed, but just for fun
        expect(scope.foo).toEqual('bar');

        expect(element.children('div').scope().$id).toEqual(scope.$id);

Gets the test passing (and is slightly more specific than simply expecting .scope to be defined). This is the strategy used by the directives testing this behaviour in compileSpec

caitp avatar Oct 25 '13 19:10 caitp

sorry for the late response, but you made my day!! :+1:

ajs2 avatar Oct 28 '13 10:10 ajs2

found an other issue. When the directive has an isolated scope, the properties set on the scope are not accessible in the unit test, so a test like

expect(element.scope().foo).toEqual('bar');

fails. I'v updated the project! Here is a jsfiddle which demonstrates that it can work

ajs2 avatar Nov 02 '13 22:11 ajs2

It is working only on 1.2.0-rc2. I've updated to 1.2.2 and it fails now:

http://jsfiddle.net/YvQTZ/1/

tomafc330 avatar Dec 11 '13 21:12 tomafc330

@tommytcchan:

Breaking Changes

$compile:

due to d0efd5ee, Child elements that are defined either in the application template or in some other directives template do not get the isolate scope. In theory, nobody should rely on this behavior, as it is very rare - in most cases the isolate directive has a template.

due to 909cabd3, Directives without isolate scope do not get the isolate scope from an isolate directive on the same element. If your code depends on this behavior (non-isolate directive needs to access state from within the isolate scope), change the isolate directive to use scope locals to pass these explicitly.

Before

<input ng-model="$parent.value" ng-isolate>

.directive('ngIsolate', function() {
  return {
    scope: {},
    template: '{{value}}'
  };
});

After

<input ng-model="value" ng-isolate>

.directive('ngIsolate', function() {
  return {
    scope: {value: '=ngModel'},
    template: '{{value}}
  };
});

Basically, the scope() you're getting for that directive is the same as the parent scope, you need to use element.isolateScope() to get at the isolate scope.

caitp avatar Dec 11 '13 21:12 caitp

@caitp Interesting. Thanks for the explaination. I've updated the plunkr so that it uses the isolateScope() instead and it works.

http://jsfiddle.net/YvQTZ/2/

Best,

TC

tomafc330 avatar Dec 11 '13 21:12 tomafc330

thanks a lot!!

ajs2 avatar Dec 12 '13 07:12 ajs2

Okay so I've tried to get the isolateScope in a jasmine test but that doesn't work...any ideas why?

tomafc330 avatar Dec 13 '13 22:12 tomafc330

Possibilities:

  • Testing with an angular version which predates the jqLite.isolateScope() api
  • Not calling isolateScope() on the right element (it must be the root element which creates the isolate scope, eg the directive element)

caitp avatar Dec 13 '13 22:12 caitp

I'm going to close this since it seems resolved.

If it's not, let me know and I'll gladly re-open this. Thanks!

btford avatar Jan 09 '14 00:01 btford

The jsfiddle from tommytcchan works fine with scope: {}, but how do you get the scope and test it when defined as scope: true? (the jsfiddle does not work in this case).

tkrotoff avatar Feb 13 '14 17:02 tkrotoff

@tkrotoff, if scope is set to true, then a new child scope is created (and the root element of the test should have the scope in its data(), IE element.data('$scope') or just element.scope())

But, there are some caveats here --- for the moment, if you're using jQuery 2.x and replacing the element with a template, unfortunately we don't set the data correctly. There is a fix for this which hasn't been merged yet, but we should have a solution to that merged in 1.3

So, ignoring that issue with jQuery 2.x, this should work as expected, and I'd be interested to see a reproduction of it not working, if it isn't.

caitp avatar Feb 13 '14 17:02 caitp

@caitp Thanks a lot!! I've switched my directive tests (ui-select) from jQuery 2.1.0 to jQuery 1.11.0 and accessing the scope (scope: true) with element.scope() now works.

With jQuery 2.1.0, element.scope() is undefined.

I would have never find it myself.

tkrotoff avatar Feb 13 '14 17:02 tkrotoff

Did the fix get pushed for jQuery 2 and elem.scope() or elem.isolateScope() in angular 1.3?

brandonburkett avatar Nov 09 '14 00:11 brandonburkett

^ Anything new to this?

mbrand avatar Dec 03 '14 15:12 mbrand