我的组件监听 ngrx/data 实体的变化
servicesNames: Map<number, string> = new Map();
...
ngOnInit(): void {
this.servicesService.entities$.subscribe((servicesNames: ServiceListShortI[]) => {
servicesNames.forEach((serviceNames: ServiceListShortI) => {
this.servicesNames.set(serviceNames.si, serviceNames.sn);
});
});
}
现在我想测试 servicesNames 是否使用 Jasmine 更新 onInit 操作。只是部分覆盖 ngRx/data 并提供新的实体更改,以便它会被 entites$ 侦听器检测到。然后的问题是如何设置存根/模拟/任何东西来 ngrx/数据实体 $ 使用 ngrx 循环触发新的存根值。
这是我的规格文件:
const initialState = {
Service: {
entities: []
},
};
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ServiceComponent],
imports: [
RouterTestingModule.withRoutes(routes),
HttpClientTestingModule,
EffectsModule.forRoot([]),
StoreModule.forRoot({}),
EntityDataModule.forRoot(entityConfig),
],
providers: [
provideMockStore({
initialState
}),
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ServiceComponent);
component = fixture.componentInstance;
store = TestBed.inject(MockStore);
servicesService = TestBed.inject(ServiceEntityService);
fixture.detectChanges();
});
it("regions entity should return list of regions names", async () => {
const storeState = store.select('Service').subscribe(store => console.log(store));
fixture.detectChanges();
store.setState({
Service: {
entities: [
{ "si": 1, "sn": "my service name" },
]
}
});
fixture.detectChanges();
// WHAT ACTION HERE TO trigger entities$ WITH FAKE/STUBED data
expect(component.servicesNames.get(0)).toBeDefined()
});