jeudi 25 août 2016

Unexpected $locationChangeStart and $locationChangeSuccess events broadcasted in ionic unit tests

I'm implementing unit tests for ionic application and need to check that no events have been broadcasted on $rootScope

expect($rootScope.$broadcast).not.toHaveBeenCalled();

The problem is sometimes there are $locationChangeStart and $locationChangeSuccess events broadcasted which makes my tests fail. It seems that ionic v1.3.0 triggers those in the below code

// update browser
$rootScope.$watch(function $locationWatch() {
  var oldUrl = trimEmptyHash($browser.url());
  var newUrl = trimEmptyHash($location.absUrl());
  var oldState = $browser.state();
  var currentReplace = $location.$$replace;
  var urlOrStateChanged = oldUrl !== newUrl ||
    ($location.$$html5 && $sniffer.history && oldState !== $location.$$state);

  if (initializing || urlOrStateChanged) {
    initializing = false;

    $rootScope.$evalAsync(function() {
      var newUrl = $location.absUrl();
      var defaultPrevented = $rootScope.$broadcast('$locationChangeStart', newUrl, oldUrl,
          $location.$$state, oldState).defaultPrevented;

The "initializing" flag is true in the above code and this triggers $rootScope.$broadcast. Question - why is it happening? And what is the best way to handle this situation? Right now I added $rootScope.$digest(); before acting in my tests which seemed to fix the problem however I'm not sure if this is valid solution

Thank you

Aucun commentaire:

Enregistrer un commentaire