Search code examples
angularoutputangular-test

How to access value returned by an event emitter in angular


Like I have a method -

I call this method from my unit test using value -

var eventValue = <ManagedColumns>{
      selectedColumns : ["Select","Last Name"],
      availableColumns: ["First Name"]
    };

component.updateColumns(eventValue);

And method in the component is -

updateColumns(eventValue: ManagedColumns) {
    console.log("KKKKKKK"+JSON.stringify(eventValue));
    this.applyColumnChanges.emit(eventValue);
    console.log("LLLLLL"+JSON.stringify(this.applyColumnChanges));
  }

I want to assert that the event emitted in this method returns the same value as passed to it. When I try to (in test) -

console.log("MMMMMMMM"+JSON.stringify(component.applyColumnChanges));

It gives me something like below but not the value returned/emiited by emitter -

LOG: 'MMMMMMMM{"_isScalar":false,"observers":[],"closed":false,"isStopped":false

Solution

  • Because you are using this.applyColumnChanges in the console.log("LLLLLL"+JSON.stringify(this.applyColumnChanges));
    which is the emitter not the emitted value. If you want to check the value just subscribe it like

    this.applyColumnChanges.subscribe((emittedVal) => {
      console.log('This is emitted value'+ emittedVal);
    });