Skip to content

Cannot override mockstore selector during testing #2171

@peterreisz

Description

@peterreisz

Once a select stream is created it cannot be overwritten during tests with the mockStore.overrideSelector

Minimal reproduction of the bug/regression with instructions:

I have the following component:

@Component({
  template: `{{ myValue$ | async }}`
})
class MyComponent {
  myValue$ = this.store.select(mySelector);
  // ... other observables

  constructor(private store: Store<any>) {}

  onSomeEvent(): void {
    this.store.dispatch(myAction());
  }
  // ... other event handlers
}

Which I would like to test:

describe('MyComponent', () => {
  let store: MockStore<any>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        MyComponent,
        ...
      ],
      providers: [
        provideMockStore({
          selectors: [{ selector: mySelector, value: 'foo' }],
        })
      ],
    }).compileComponents();
  }));

  beforeEach(() => {
    store = TestBed.get(Store);
    fixture = TestBed.createComponent(MyComponent);
    fixture.detectChanges();
  });

  it('should render bar insteadof foo', () => {
    store.overrideSelector(mySelector, 'bar');
    fixture.detectChanges();
    expect(fixture.debugElement.nativeElement.textContent).toBe('bar');
  });

}

Expected behavior:

The above test should pass, the mockStore.overrideSelector should update the select stream.

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

NgRx: 8.4.0

Other information:

The mockstore implementation is now creating the stream, but it does not store any reference of the subject. Mockstore should the reference of the stream and next a new value into it in case of overrideSelector called.

There is a similar issue, but that does not solve the use case defined above: #2121

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

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions