mardi 3 mai 2016

$watch callback fires every time in browser but just once during the test

I have code which should store information about filter changes. In browser $watch callback fires every time I change value. However, when I trying to replicate that in jasmine/karma test it fires just after first change.

Controller

var tableFilter = this
  ;

var init0 = true
  ;
$scope.$watch( 'tableFilter.config.period', function () {
  console.log('watch');
  if ( !init0 ) {
    console.log('set dirty');
    tbFilterConfig.set( 'pristine', false );
    tbFilterConfig.setDirty( 'period' );
  }
  init0 = false;
} );

Test

describe( '$scope $watch', function () {
  it( 'should add period to dirty after second change', function () {
    $scope.$apply(console.log(1), controller.config.period = 'test');
    expect( tbFilterConfigObj.get( 'dirty' ) ).toEqual( [] );
    $scope.$apply(console.log(2), controller.config.period = 'test2');
    expect( tbFilterConfigObj.get( 'dirty' ) ).toEqual( [ 'period' ] );
  } );
});

Output in console:

LOG: 1
LOG: 'watch'
LOG: 2
PhantomJS 1.9.8 (Windows 8 0.0.0) Controller: TableFilterCtrl $scope $watch should add period to dirty after second change FAILED
Expected [  ] to equal [ 'period' ].
        at C:/Projects/trackback-network-insight-ui/test/spec/controllers/table_filter.js:74

But in browser:

setTimeout(function (  ) {
  $scope.$apply(console.log(1), tableFilter.config.period = 'test');
  $scope.$apply(console.log(2), tableFilter.config.period = 'test2');
}, 1000);

Gives me:

LOG: watch
LOG: 1
LOG: watch
LOG: set dirty
LOG: 2
LOG: watch
LOG: set dirty

That is expected behavior.

What is wrong with my test then? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire