jeudi 1 février 2018

Angular 2 EventEmitter apparently not emitting in test

This is starting to feel bewitched. After having read the Angular guide multiple times today, I cannot get my test to pass, and cannot figure out why.

component.html

<button (click)="download()" title="Save report to your computer">
  Download
</button>

component.ts

export class ExtractModalComponent implements OnInit {
  @Output() downloadRequest: EventEmitter<any> = new EventEmitter<any>();

  constructor(public activeModal: NgbActiveModal) {}

  download() {
    this.downloadRequest.emit({ fromDate: this.fromDateTime, toDate: this.toDateTime });
  }
}

component.spec.ts

it('should raise the downloadRequest event when the download button is clicked, and send the dates', () => {
  let dates = {};
  let spy = spyOn(comp.downloadRequest, 'emit');
  let dt = moment(new Date(2018, 0, 1, 13, 0, 0)).format();
  comp.downloadRequest.subscribe(result => {
    dates = result;
  });
  click(page.downloadBtn); //click helper borrowed from Angular guide; calls either el.click() or el.triggerEvent('click', eventObj)
  //have also tried just comp.download(), but that doesn't make a difference
  expect(spy).toHaveBeenCalled(); //fails: "Expected spy emit to have been called"
  expect(dates).toEqual({ fromDate: dt, toDate: dt }); //fails: Expected Object({  }) to equal Object({ fromDate: '2018-01-01T13:00:00-05:00', toDate: '2018-01-01T13:00:00-05:00' })
});

Any help would be greatly appreciated. To me it looks almost exactly like the example in the guides, but for some reason, their test passes and mine doesn't.

Aucun commentaire:

Enregistrer un commentaire