在测试中覆盖 NGRX Store 选择器似乎被忽略
Override NGRX Store selector in test seems to get ingored
覆盖选择器,同时返回 null
仍然 returns 初始化时定义的覆盖返回的数据。也试过 setState
但也没有用。
正在尝试在此测试 else
:
this.store.pipe(select(singleUserSelector, { _id: this.userModel?._id })).subscribe((state: IUserSelected) => {
console.log('singleUserSelector', this.userModel?._id, state);
if (state) {
this.componentState = state.componentState;
this.userModel = state.userModel;
this.selected = true;
} else if (this.selected) {
this.componentState = ComponentState.VIEW;
this.selected = false;
}
});
测试
beforeEach(async () => {
await TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
imports: [ReactiveFormsModule],
providers: [provideMockStore(
{
initialState,
selectors: [
{ selector: usersSelector, value: users },
{ selector: singleUserSelector, value: { componentState: ComponentState.VIEW, userModel: userIndy } },
{ selector: mainComponentStateSelector, value: ComponentState.VIEW }]
}
)],
declarations: [UserComponent],
}).compileComponents();
store = TestBed.inject(MockStore);
fixture = TestBed.createComponent(UserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should have not selected state based on singleUserSelector not returning valid user', () => {
const mockedStateFocusedSelectorNull = store.overrideSelector(singleUserSelector, null);
// setState doesnt work either
// store.setState({ userFocused: null } as ApplicationState);
fixture.detectChanges();
expect(component.selected).toBeFalsy();
expect(component.componentState).toBe(ComponentState.VIEW);
});
显然,这成功了..
const mockedStateFocusedSelectorNull = store.overrideSelector(singleUserSelector, null);
store.refreshState(); // <------
覆盖选择器,同时返回 null
仍然 returns 初始化时定义的覆盖返回的数据。也试过 setState
但也没有用。
正在尝试在此测试 else
:
this.store.pipe(select(singleUserSelector, { _id: this.userModel?._id })).subscribe((state: IUserSelected) => {
console.log('singleUserSelector', this.userModel?._id, state);
if (state) {
this.componentState = state.componentState;
this.userModel = state.userModel;
this.selected = true;
} else if (this.selected) {
this.componentState = ComponentState.VIEW;
this.selected = false;
}
});
测试
beforeEach(async () => {
await TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
imports: [ReactiveFormsModule],
providers: [provideMockStore(
{
initialState,
selectors: [
{ selector: usersSelector, value: users },
{ selector: singleUserSelector, value: { componentState: ComponentState.VIEW, userModel: userIndy } },
{ selector: mainComponentStateSelector, value: ComponentState.VIEW }]
}
)],
declarations: [UserComponent],
}).compileComponents();
store = TestBed.inject(MockStore);
fixture = TestBed.createComponent(UserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should have not selected state based on singleUserSelector not returning valid user', () => {
const mockedStateFocusedSelectorNull = store.overrideSelector(singleUserSelector, null);
// setState doesnt work either
// store.setState({ userFocused: null } as ApplicationState);
fixture.detectChanges();
expect(component.selected).toBeFalsy();
expect(component.componentState).toBe(ComponentState.VIEW);
});
显然,这成功了..
const mockedStateFocusedSelectorNull = store.overrideSelector(singleUserSelector, null);
store.refreshState(); // <------