How to test Observable subscribe callback using jasmine?

460 Views Asked by At

I have an Angular9, Angular Material table similar to the ones in the docs. https://material.angular.io/components/table/examples

The difference in my case is that I implemented a constructor in the table class as well as a custom datasource, etc. I am getting the data for my table inside the actual table class using a function called inside ngOnInit() and the called function basically subscribes to an observable coming from service and I don't really know how to test it and I need help. As you can see, --code-coverage is complaining that I didn't cover the subscribe callback. So how do I do that?

Yes, the service is tested already so I separated the tests. Obviously I'm not gonna test the service in this component's testfile. I just don't get why this code is not covered/how to do it. I am using TestBed, some code is already covered but I'm not including it so I don't make the question a astronomically long.

describe('MemeTableComponent', () => {
  let component: MemeTableComponent;
  let fixture: ComponentFixture<MemeTableComponent>;
  const inputData = flattenObjectArray(gatewayInfo);

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [MemeTableComponent],
      imports: [
        NoopAnimationsModule,
        MatPaginatorModule,
        MatSortModule,
        MatTableModule,
        HttpClientTestingModule
      ],
      providers: [
        MemeService
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MemeTableComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    component.dataSource.data = inputData;
  });

  it('should compile', () => {
    expect(component).toBeTruthy();
  });
});
0

There are 0 best solutions below