Skip to content

Mockstore selector override with undefined value #2244

@peterreisz

Description

@peterreisz

Minimal reproduction of the bug/regression with instructions:

interface MyFeatureState {
  myProp?: string;
}

const selectMyFeature = createFeatureSelector<MyFeatureState>('my-feature');

const selectMyProp = createSelector(selectMyFeature,
  (state: MyFeatureState) => state.myProp);

TestBed.configureTestingModule({
  providers: [provideMockStore()],
}).compileComponents();

const store: MockStore<any> = TestBed.get(Store);
const mockSelector = store.overrideSelector(selectMyProp, undefined);

mockSelector.setResult(undefined);
store.refreshState();

store.select(selectMyProp).subscribe(val => expect(val).toBe(undefined))

Expected behavior:

User should be able to override selector result with undefined too, at this moment undefined value is indicating that the selector result is not overwritten, thus mockstore will try to run all the selector logic.

This would be useful in case of a deep state contains an optional value and the user do not want to define the whole state tree in the parameter of the provideMockStore method. As a workaround setResult can be called with null as any, but projects can ban null keyword via a tslint rule, thus it might requires additional lint ignore comment too.

NgRx should use a different variable for indicating is the selector value overwritten.

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):

NgRx: 8.4.0

I would be willing to submit a PR to fix this issue

[X] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions