mardi 22 novembre 2016

Angular hasClass returns true even though the class is not present

In yet another exciting installment of "what are you doing, tbm?", I have a test that (admittedly was passing before [changes] but now) is failing due to an apparent class being present, despite all efforts to prove otherwise.

Context:

A list of entrants is shown, each of which can be rated or scored. Inside an entrant's div, related (by category) entrants are shown in summary. A summarized entrant's score is not shown until the entrant itself is rated via its own div. This test verifies this behaviour.

Test:

it('related items are hidden until scored', function() {

    expect(judgeService.entrants[2].rating.score).toBe(0);

    var other = angular.element(element.find('.competition')[0]);
    var score_container = other.find('div > div');
    expect(score_container.hasClass('ng-hide')).toBe(true);

    judgeService.entrants[2].rating.score = 3;
    scope.$digest();

    // We'll refer to this later
    console.log('HTML: ' + other.html());
    console.log('CLASS: ' + score_container.attr('class'));

    // failure here
    expect(score_container.hasClass('ng-hide')).toBe(false); 
});

HTML:

        <div class="competition"
            ng-repeat="other in item.related">
            <div>
                <br/>
                <!-- this becomes the div > div in the test -->
                <score-block item="other" ng-show="other.rating.score"></score-block>
            </div>
        </div>

Output:

LOG: 'HTML:
            <div class="ng-binding">
                Andres<br>
                <div class="ng-isolate-scope" ng-class="{error: vm.error}" item="other" ng-show="other.rating.score">
[ skippy bits ]'

LOG: 'CLASS: ng-isolate-scope'

So, yeah, the class IS NOT present on the element.. so why does hasClass() think it is?

[changes]

Per git bisect, the change that caused the test to start failing was in an unrelated directive. But even if the change was directly related, I fail to see how everybody but hasClass() reports the class as absent.

Aucun commentaire:

Enregistrer un commentaire