How to invoke $interval on init when testing a directive

时间:2019-04-17 01:09:40

标签: angularjs unit-testing jasmine

I want to test a directive that doesn't show until $interval is called on initialization. How do I go about testing this? I currently am trying to call interval during the test but it doesn't seem to be doing anything. This is what I currently have.

The directive:

export class IdleDisconnectWarningController {

static $inject = ["$scope", "$interval"];

private TAG = "IdleDisconnectWarningController";
public timeToDisconnect;

constructor(private $scope: IIdleDisconnectWarningScope,
            private $interval: IIntervalService
) {
  this.$interval(() => this.updateCountdown(), 1000);
}

  private updateCountdown(){
    console.log("UPDATED");
    var time = Math.floor((this.$scope.disconnectTimeInMilliSecs - Date.now())/1000);
    this.timeToDisconnect = time.toString();
  }

}

The test

  beforeEach(inject(function($rootScope, _$interval_, _$compile_ ){
      elem = angular.element('<idle-disconnect-warning disconnect-time-in-milli-secs=300></idle-disconnect-warning>');
      scope = $rootScope.$new();
      $interval = _$interval_;
      $compile = _$compile_;
      elem = $compile(elem)(scope);
      scope.$digest();
      console.log(elem.html())
  }));

  it('has valid content ', function() {
    // Check that the compiled element contains the templated content
    $interval.flush(100);
    scope.$apply();
    console.log(elem.text());
    expect(elem).not.toBeNull();
  });

0 个答案:

没有答案