How to test ngrx pipe select in Rxjs 6

被刻印的时光 ゝ 提交于 2019-12-12 13:57:39

问题


Previously I could test the below store select

this.store.select(fromRoot.someselector).pipe(map(r => this.store.dispatch(new Action())));

This was in my test

{provide: Store, useValue: jasmine.createSpyObj('store', ['select']);

store.select.and.returnValue(of({}));

But now it has changed to pipes

this.store.pipe(select(fromRoot.someselector));

this.store.pipe(
        select(fromRoot.someselector), 
        filter(result => !!result), 
        map(r => { 
             if (result === ' hello') { 
               this.store.dispatch(new Action()); 
             } 
           }
        ));

How to test this especially when you have map after select and within it you are dispatching an action and you want verify action was called.


回答1:


Skip the operators and test directly the result of the stream :

store
  .pipe(select('selector'))
  .subscribe(val => expect(val).toBe(theMockedValSentToTheSpy))

To explain further :

  • create a mock of your store
  • create a mock of your value
  • return this mocked value in your mocked store
  • expect your component variable to return the mocked value

This gives :

const mockedValue = { id: 1 };
const storeSubjectMock = new BehaviorSubject(mockedValue);
const mockedStore = {
  pipe: () => storeSubjectMock.asObservable(),
};

// { provide: Sotre, useValue: mockedStore }; in your testbed

it('should return an unaltered value', () => {
  component.variableReferencingStore
    .pipe(select('selector'))
    .subscribe(val => expect(val).toBe(mockedValue))
});

Now the good thing about that is that you can test all operators like that. Say your component variable is

storeValue$ = this.store.pipe(
  select('selector'),
  map(value => ({ ...value, name: 'customName' }))
)

Then your tests just changes to

it('should return an altered value with a name property set to customName', () => {
  component.variableReferencingStore
    .pipe(select('selector'))
    .subscribe(val => expect(val).toEqual({ ...mockedValue, name: 'customName'))
});



回答2:


spyOn(store, 'pipe').and.callFake(selector => { return of(true); });
... expect(store.pipe).toHaveBeenCalledTimes(1); // pass



来源:https://stackoverflow.com/questions/53315111/how-to-test-ngrx-pipe-select-in-rxjs-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!