How to write unit test for a subscribe function with filter in Angular 2? -


right writing unit test kind of situation below:

public div: htmldivelement; public currentevent: eventtype;  public listentorender() {     this.adsservice.render.filter((event: eventtype) => {             return this.div.id === event.slot.getslotelementid();         }).subscribe((event: eventtype) => {             let custom_event = new customevent('render', {                 detail: event             });             this.currentevent= event;         }); } 

during unit test, mock render subject, don't know how can make pass filter

return this.div.id === event.slot.getslotelementid(); 

and go subscribe function.

class mockadsservice {    render = new subject(); }  class mockeventtype {    name: 'test_event';    slot: {       getslotelementid = function() {return 'test_id'}    }; }  describe('test', () => {     let mockadsservice: mockadsservice,         mockeventtype: mockeventtype;      beforeeach(() => {         mockadsservice = new mockadsservice();         mockeventtype = new mockeventtype();     });      it('listentorender fired correctly', () => {         mockadsservice.render.next(mockeventtype);         component.listentorender();         expect(component.currentevent).toequal(mockeventtype);     }); }); 

do need set in subject.next passing filter?

it's simple. you're subscribing component after event has happened. it's late cold observable. switch render.next() , component.listentorender() calls , should work fine:

it('listentorender fired correctly', () => {     component.listentorender();     mockadsservice.render.next(mockeventtype);     expect(component.currentevent).toequal(mockeventtype); }); 

Comments