Platform: MockStore .setResult() does not emit on observable

Created on 8 Jul 2019  路  6Comments  路  Source: ngrx/platform

  • MockStore mock selectors created with overrideSelector do not emit on the stream when .setResult is called

  • This is essential functionality for testing long-lived streams, e.g. | async streams, and replacing Store in component tests

  • If another approach is preferred, it needs to be indicated in the documentation

Minimal reproduction of the bug/regression with instructions:

https://github.com/pburkindine/ngrx-mockstore-setresult

  • Run ng test to see the third test in src/app/sample-component/sample-component.component.spec.ts fails

Expected behavior:

  • Test should pass

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

Angular: 8
NgRx: 8
Node: 10
OS: OSX High Sierra

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

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

Most helpful comment

Hey @brianguppy, also checkout refreshState(), added in #2148 with documentation for it in #2182.

All 6 comments

@pburkindine I don't see the sample-component folder in the linked repo.

@jtcrowson D'oh! Try again please :D

To trigger a state change, you must set a new store result.
AFAIK we can't do this for you because a selector is just a pure function:

it('should updata the message', () => {
    fixture.detectChanges();
    expect(fixture.nativeElement.innerHTML.includes('one')).toBeTruthy();

    actions.getMessage.setResult('two two');
    store.setState({});
    fixture.detectChanges();
    expect(fixture.nativeElement.innerHTML.includes('two two')).toBeTruthy();
});

I'm sorry, but I don't know why this was closed. Tests included in the repo by @pburkindine still don't pass, even after setting new state, as described by @timdeschryver. I'm having exactly same issue with my unit tests in my projects.

The suggestion shown by @timdeschryver works for me. Make sure to call store.setState() and fixture.detectChanges().

Hey @brianguppy, also checkout refreshState(), added in #2148 with documentation for it in #2182.

Was this page helpful?
0 / 5 - 0 ratings