Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockStore .setResult() does not emit on observable #2000

Closed
pburkindine opened this issue Jul 8, 2019 · 6 comments
Closed

MockStore .setResult() does not emit on observable #2000

pburkindine opened this issue Jul 8, 2019 · 6 comments

Comments

@pburkindine
Copy link

pburkindine commented Jul 8, 2019

  • 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

@jtcrowson
Copy link
Contributor

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

@pburkindine
Copy link
Author

@jtcrowson D'oh! Try again please :D

@timdeschryver
Copy link
Member

timdeschryver commented Jul 8, 2019

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();
});

@garrappachc
Copy link

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.

@brianguppy
Copy link

brianguppy commented Oct 29, 2019

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

@jtcrowson
Copy link
Contributor

jtcrowson commented Oct 29, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants