angularjs - Jasmine test fails with error from a different suite -
experiencing peculiar behaviour karma-jasmine testing angular app.
have test in 1 suite reports failure message comes separate test in test suite file.
test result:
factory: page should uncheck items failed expected undefined contain '<div class="ui-select-container ui-select-bootstrap dropdown ng-valid" ng-class="{open: $select.open}" ng-model="test.slctbx.selected" theme="bootstrap" ng-disabled="disabled"></div>'
that test actually:
describe 'factory: page', () -> ... 'should check items', () -> page.checkall null expect(page.actions).toequal {checkall: false}
the error comes from:
describe 'directive: select', () -> ... 'should replace select box', () -> replacementmarkup = '<div class="ui-select-container ui-select-bootstrap dropdown ng-valid" ng-class="{open: $select.open}" ng-model="test.slctbx.selected" theme="bootstrap" ng-disabled="disabled"></div>' settimeout () -> expect($('select').length).toequal 0 expect($('form').html()).tocontain replacementmarkup return , 0
if remove factory: page suite behaviour exists test suite.
the timeout smelly because directive has timeout within waits arbitrary amount of time element replace has been populated... (which bit smelly itself!)
==== edit + directive code ====
link: (scope, element, attrs) -> items = [] name = element.attr('name').replace '[]', '' placeholder = '' index, el of element.find('option') when typeof el 'object' , index isnt '0' if index == '1' placeholder = el.innerhtml else if typeof el[0] == 'undefined' items.push {value: el.getattribute('value'), label: el.innerhtml} if typeof scope.$parent.test != 'object' scope.$parent.test = {} scope.$parent.test[name] = items select = '<ui-select ng-model="test.' + name + '.selected" theme="bootstrap" ng-disabled="disabled"> <ui-select-match placeholder="' + placeholder + '">{{ $select.selected.label }}</ui-select-match> <ui-select-choices repeat="item in test[\'' + name + '\'] | filter: $select.search"> <div ng-bind-html="item.label | highlight: $select.search"></div> </ui-select-choices> </ui-select>' newsel = $compile(select)(scope.$parent) settimeout () -> element.replacewith angular.element(newsel) , 10 return false
why dont use $timeout
instead of settimeout()
in both directive , test? able use $timeout.flush()
in test verify if the directive have waited arbitrary amount of time without waiting it.
your directive should use $timeout(func, arbitraryamountoftime)
instead of settimeout(func, arbitraryamountoftime)
, test becomes:
it 'should replace select box', -> inject ($timeout) -> $timeout.flush() expect($('select').length).toequal 0 expect($('form').html()).tocontain replacementmarkup
i suppose in current code, jasmie moves on next test , timeouted call appears failing test being executed. it's hunch.
Comments
Post a Comment