0

I have an Angular.js directive which I am trying to write a test for. Basically, it is a wrapper for a checkbox, such as that when the wrapper is clicked, the checkbox's state is changed. The problem is that I don't know how to replicate this behaviour inside a unit test. What I have for a test is this:

describe('Checkbox field', function() { var elm, scope; beforeEach(module('MyApp')); beforeEach(module('app/partials/checkboxfield.html')); beforeEach(inject(function($rootScope, $compile) { elm = angular.element( '<checkboxfield data="data" model="value"></checkboxfield>'); scope = $rootScope; scope.value = false; $compile(elm)(scope); scope.$digest(); })); it('should change checkbox value on wrapper click', inject(function($compile, $rootScope) { var wrapper = elm.find('div').eq(0); //var wrapper = $('div.checkbox'); wrapper.click(); expect(checkbox.prop('checked')).toBe(true); expect(scope.value).toBe(true); wrapper.click(); expect(checkbox.prop('checked')).toBe(false); expect(scope.value).toBe(false); })); }); 

Angular.js comes with jqLite, so when I run "elm.find('div').eq(0)", I don't get the click() method. I would simply use jQuery instead but every time I use one of its selectors, no items are returned; I always get $(whatever).length === 0.

Just to clarify, I am using karma-ng-html2js-preprocessor as seen here.

4
  • beforeEach(module('app/partials/checkboxfield.html')); is completely irrelevant. Moreover, $compile takes template string and returns the element. Your usage seems wrong.
    – idursun
    CommentedMay 23, 2014 at 15:07
  • Hi! I am using newtriks.com/2013/04/26/how-to-test-an-angularjs-directive as an example so I've pretty much copied the structure from there. Besides, when I remove the beforeEach(module('app/partials/checkboxfield.html')); line, Karma starts complaining about unexpected GET petitions.CommentedMay 26, 2014 at 8:08
  • I see. That example is using html2js karma plugin: github.com/karma-runner/karma-ng-html2js-preprocessor retry after installing it.
    – idursun
    CommentedMay 27, 2014 at 7:06
  • I have installed it already. In fact, I have run other tests inside the same suite and they worked flawlessly - I just omitted them for the same of brevity. It is only this one I am having issues with.CommentedMay 27, 2014 at 8:12

1 Answer 1

0

You should test the value of your scope variable instead of the element attribute value. Because if everything went well then the value of the scope variable should have changed.

For example, the following test passes:

describe('Checkbox field', function () { var $scope, elm; beforeEach(module('myApp')) beforeEach(inject(function ($rootScope, $compile) { $scope = $rootScope.$new(); $scope.value = true; $scope.data = "Click on me"; elm = $compile('<checkboxfield data="data" model="value"></checkboxfield>')($scope); })) it('should change checkbox value on wrapper click', function () { expect($scope.value).toBe(true); elm.click(); expect($scope.value).toBe(false); elm.click(); expect($scope.value).toBe(true); }) }) 
3
  • The problem is that the click() method is not defined. When I try to call it, I get the usual "undefined is not a function" complaint.CommentedMay 27, 2014 at 13:39
  • Please note my usage of $compile is different than yours. I am also not using html2js plugin. elm and also elm.click are defined.
    – idursun
    CommentedMay 27, 2014 at 14:09
  • I have just ran your code as is and I'm getting the exact same error. :( Which version of Angular are you using? I'm using 1.3.0-beta.8 - perhaps this could be causing the error? Also, I'm using jQuery 1.8.2. I'm not sure about whether this could be related.CommentedMay 28, 2014 at 9:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.