0
votes

I am trying to unit test this line of javascript with Jasmine framework :

$scope.validerStyleTag = function() {
    $scope.currentTag = JSON.parse($scope.tagList);
    for (var i = $scope.listTags.length - 1; i >= 0; i--) {
        if ($scope.listTags[i]._id === $scope.currentTag._id) {
            $scope.tagID = $scope.listTags[i]._id;
            $scope.listTags[i].disabled = true;
            break;
        }
    }

    // var textestyler = angular.element(document.querySelector('#style-affected-add'))[0].outerHTML;
    // var debut = textestyler.substring(textestyler.indexOf('<p'), textestyler.indexOf('>') + 1);
    // var texteFinal = debut + '</p>';
    var mytext = '<p data-font="' + $scope.policeList + '" data-size="' + $scope.tailleList + '" data-lineheight="' + $scope.interligneList + '" data-weight="' + $scope.weightList + '" data-coloration="' + $scope.colorList + '"> </p>';


    $scope.tagStyles.push({
        id_tag: $scope.currentTag._id,
        style: mytext,
        label: $scope.currentTag.libelle,
        police: $scope.policeList,
        taille: $scope.tailleList,
        interligne: $scope.interligneList,
        styleValue: $scope.weightList,
        coloration: $scope.colorList,

    });
    angular.element($('.shown-text-add').text($('.shown-text-add').text()));
    angular.element($('#style-affected-add').removeAttr('style'));
    $scope.colorationCount = 0;
    $scope.tagList = null;
    $scope.policeList = null;
    $scope.tailleList = null;
    $scope.interligneList = null;
    $scope.weightList = null;
    $scope.colorList = null;

};

the problem of testing is in the first line :

$scope.currentTag = JSON.parse($scope.tagList);

when i begin to write my unit test like this and call my function :

it('ProfilesCtrl:validerStyleTag should set validerStyleTag ', inject(function() {
expect($scope.validerStyleTag).toBeDefined();
$scope.validerStyleTag();


}));

karma is giving me an unexpected error : TypeError: Attempted to assign to readonly property at workFn (/home/app/bower_components/angular-mocks/angular-mocks.js:2107)

ideas or feedbacks are more than welcome !!

1
Are you using PhantomJS with an "old" version of Angular? There was a bug that caused that error that has been fixed recently. Looks like it was fixed in 1.2.12, if I read correctly. github.com/angular/angular.js/pull/5047 - Craig Squire
@CraigSquire THANK YOU for your answer, so how could i update my Angularjs version ? and phantomJs version ? - badaboum
BTW, it was actually angular-mocks that had the bug. - Craig Squire
Let me know if the update fixes it or not. - Craig Squire
Nice. I ran into the same problem last week. - Craig Squire

1 Answers

1
votes

Are you using PhantomJS with an "old" version of Angular? There was a bug that caused that error that has been fixed recently. Looks like it was fixed in 1.2.12, if I read correctly.

https://github.com/angular/angular.js/pull/5047