Platform: docs: testing changes with overridden selector and setResult

Created on 27 Jun 2019  路  5Comments  路  Source: ngrx/platform

It feels that there is documentation missing for a case on testing change in selector value using overridden selector.

In a given scenario below the selection from store for ready state only receives false (initial value of overridden selector), but never receives true (the updated value from setResult).

There is a small repo with reproduction of scenario: https://github.com/minijus/ngrx-set-result/blob/master/src/app/service/app.service.spec.ts

export class AppService {
  constructor(private store: Store<AppState>) {}
  isReady() {
    return this.store
      .select(selectAppReady)
      .pipe(find(ready => !!ready))
      .toPromise();
  }
}
describe('AppService', () => {
  let service: AppService;
  let store: MockStore<AppState>;
  let appReady: MemoizedSelector<AppState, boolean>;

  beforeEach(() => {
    TestBed.configureTestingModule({ providers: [provideMockStore()] });

    store = TestBed.get(Store);
    appReady = store.overrideSelector(selectAppReady, false);

    service = TestBed.get(AppService);
  });

  describe('#isReady', () => {
    it('should resolve once app state becomes ready', fakeAsync(() => {
      setTimeout(() => {
        appReady.setResult(true);
      }, 1000);

      const spy = jasmine.createSpy();
      service.isReady().then(spy);

      expect(spy).not.toHaveBeenCalled();

      tick(1001);

      expect(spy).toHaveBeenCalled();
    }));
  });
});

I would be willing to submit a PR for the docs :heart:

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

Docs Store

Most helpful comment

In 8.4.0 we release MockStore.refreshState() to solve this issue.

Closing in favor of #2154 (to add it to our docs)

All 5 comments

More generally, setResult should cause the observable to emit. As it stands we cannot make any use of the MockStore for long-lived streams in components, for example.

More generally, setResult should cause the observable to emit. As it stands we cannot make any use of the MockStore for long-lived streams in components, for example.

Exactly, setResult could internally re-apply current state on store. Maybe by calling setState on store instance internally.

The MockStore.setState() method doesn't do anything as far as mocked selectors are concerned.

In 8.4.0 we release MockStore.refreshState() to solve this issue.

Closing in favor of #2154 (to add it to our docs)

Was this page helpful?
0 / 5 - 0 ratings