I have to test some property of my component, to change the proper way after loading datas.
Here is my components' ngOnInit
ngOnInit(): void {
const getOne$ = this.appDataService.proxy.getAllWithoutPaginate().pipe(
tap((result: Array<CategoryInterface>) => {
const categories = [];
result.forEach((category: CategoryInterface) => {
this.parseCategoryTree(categories, category);
});
this.categories$.next(categories);
this.numberOfSelectedCategories$.next(this.calendarService.categories$.value.length);
})
);
this.subscriptions.add(this.appDataService.start(getOne$, 'load categories').subscribe());
}
Here is my spec.ts for it:
imports....
import { CalendarHeaderComponent } from './calendar-header.component';
describe('CalendarHeaderComponent', () => {
let component: CalendarHeaderComponent;
let fixture: ComponentFixture<CalendarHeaderComponent>;
let debugElement: DebugElement;
const datas = CATEGORY_RESPONSE;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
DdataCoreModule.forRoot(environment),
RouterTestingModule,
AppModule
],
providers: [
],declarations: [ CalendarHeaderComponent ]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(CalendarHeaderComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
const proxySpy = jasmine.createSpyObj('ProxyService', ['getAllWithoutPaginate']);
const appDataServiceSpy = jasmine.createSpyObj('AppDataService', ['proxy', 'start']);
appDataServiceSpy.start.and.returnValue(of(datas));
proxySpy.getAllWithoutPaginate.and.returnValue(of(datas));
appDataServiceSpy.proxy = proxySpy;
component.appDataService = appDataServiceSpy;
fixture.detectChanges();
});
}));
it('has running functions', fakeAsync(() => {
expect(component.appDataService.start)
.withContext('appDataService.start()')
.toHaveBeenCalledTimes(1);
expect(component.appDataService.proxy.getAllWithoutPaginate)
.withContext('appDataService.proxy.getAllWithoutPaginate()')
.toHaveBeenCalledTimes(1);
}));
it('properties changing after load', fakeAsync(() => {
expect(component.numberOfSelectedCategories$.value).withContext('numberOfSelectedCategories$').toEqual(10);
expect(component.categories$.value)
}));
});
I have a CATEGORY_RESPONSE for testing, and I have 6 parent and 4 child category in it. The parseCategoryTree() makes an array from the result with 10 category. Tha numberOfSelectedCategories$ has to be the length of this array.
The first test (has running functions) pass, but the second (properties changing after load) fails, the numberOfSelectedCategories$
is still equals 0.
How can I make test that run in rxjs pipe and tap and other?