Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get arguments on an event emitter in jasmine test

I have a unit test as below

it('billing information is correct', () => {
    fixture.detectChanges();
    spyOn(component.myEventEmitter, 'emit').and.callThrough();
    component.form.controls['size'].setValue(12);
    fixture.detectChanges();
    **let args= component.myEventEmitter.emit.mostRecentCall **
    expect(args.billingSize).toEqual('30')
});

When size changes, myEventEmitter is being emitted with a large json object which includes billingSize. And I want the test to check if this value is as expected. But looks like I can't do 'mostRecentCall/ calls' on the event emitter. any suggestions??

Note: I don't want to do

 expect(component.myEventEmitter.emit).toHaveBeenCalledWith(*dataExpected*);

because the dataExpected is a large json object. I just care about one field. Any help would be much appreciated.

like image 442
Josf Avatar asked Oct 25 '17 19:10

Josf


1 Answers

This should work.

it('billing information is correct', () => {
  fixture.detectChanges();
  spyOn(component.myEventEmitter, 'emit').and.callThrough();
  component.form.controls['size'].setValue(12);
  fixture.detectChanges();
  let arg: any = (component.myEventEmitter.emit as any).calls.mostRecent().args[0];
  expect(arg.billingSize).toEqual('30');
});

note:

 component.myEventEmitter.emit.calls.mostRecent() 

- wouldn't compile (error: calls does not exist on type ..') so type it to 'any' and should work.

like image 189
Josf Avatar answered Nov 04 '22 00:11

Josf