Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing an AngularJS directive that contains $watch() not being compiled
    primarykey
    data
    text
    <p>I'm currently researching my problem but I thought adding a question here may help as I'm new to AngularJS and even newer to Unit Testing.</p> <p>I have the current directive that works, it users the <code>$watch()</code> function to track a variable, if the variable is not present a default message is provided, for example "Deleted User Value" ...</p> <pre><code>angular.module('myModule') .directive('displayName', function ($transform) { return { restrict: 'A', scope: { displayName: '=' }, link: function (scope, element) { scope.$watch('displayName', function(value){ if(!value) { element.html($transform('users.profile.deletedUser')); } else { element.html(value); } }); } }; }) ; </code></pre> <p>and I have the following unit test (please note that I have amended this on the advice of Maurice) :</p> <pre><code>beforeEach(module('myModule')); beforeEach(inject(function($compile, $rootScope) { $scope = $rootScope; element = angular.element('&lt;div class="name" display-name="name"&gt;&lt;/div&gt;'); $compile(element)($scope); })); it('should display the deleted user name', inject(function() { $scope.displayName = null; element.scope().$apply(); console.log(element); expect(element.html()).toBe('Deleted User Value'); })); </code></pre> <p>However I am getting the following issue: <code>TypeError: Attempted to assign to readonly property</code>. and the <code>console.log</code> is not being outputted, can anyone advise where I am going wrong? Sorry for my stupidity but I am trying to learn unit testing "on the job" and I am still very new to AngularJS</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload