I am learning how to write unit test. Here is part of the code from the course.
#spec.ts
...
beforeEach(waitForAsync(() => {
const coursesServiceSpy = jasmine.createSpyObj("CoursesService", [
"findAllCourses",
]);
TestBed.configureTestingModule({
imports: [CoursesModule, NoopAnimationsModule, HttpClientTestingModule],
providers: [{ provide: CoursesService, useValue: coursesServiceSpy }],
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
el = fixture.debugElement;
coursesService = TestBed.inject(CoursesService);
});
}));
...
it("should display data from service", () => {
coursesService.findAllCourses.and.returnValue(of(fake data));
fixture.detectChanges();
// the test code
});
#component.ts
reloadCourses() {
const courses$ = this.coursesService.findAllCourses();
}
The problem is I am using component-store to manage the state which does not return anything after I call the effect.
# my component
constructor(
private store: StoreService
) {}
...
viewModel$ = this.store.viewModel$;
// how I call effect to refresh data
this.store.initCourses();
I just directly display data from component-store in my template.
# my template
<ng-container *ngIf="viewModel$ | async as viewModel">
I don't know how to fake component-store response and let my component know something has changed.
What I am trying to do is to assign value to the viewModel$ which is an Observable.
Does anyone know how to do that or have any other solution?
Sorry for my bad explanation, if you need more information plz let me know. Big thanks!
Assuming
StoreServiceis just a service, you need to mock it similar to howCoursesServiceis mocked in the class you took.You should try this:
Then you can do
mockViewModel$.next({/* new stuff here */ });to makeviewModel$emit with new data.Learn more about testing here: https://testing-angular.com/testing-components-depending-on-services/#testing-components-depending-on-services.