platform icon indicating copy to clipboard operation
platform copied to clipboard

Unit Testing: MockNgRedux.getSelectorStub for @select with state => someFn(state)

Open lcecil opened this issue 6 years ago • 2 comments

This is a...

  • [ ] feature request
  • [ ] bug report
  • [x] usage question

What toolchain are you using for transpilation/bundling?

  • [x] @angular/cli
  • [ ] Custom @ngTools/webpack
  • [ ] Raw ngc
  • [ ] SystemJS
  • [ ] Rollup
  • [ ] Other

Environment

NodeJS Version: 8.11.2 Typescript Version: ~3.1.3 Angular Version: ^7.0.1 @angular-redux/store version: ^9.0.0 @angular/cli version: (if applicable) OS:

Code Example

Here's my selector in my component class

@select(state => getItems(state)) items: Observable<Item[]>;

where getItems is a selector function that returns items from the state that match certain criteria

Unit Test

I'm attempting to set up a unit test for this selector, following the documentation here: https://github.com/angular-redux/platform/blob/master/packages/store/articles/intro-tutorial.md#unit-testing-selections

My test:

const stub: Subject<Item[]> = MockNgRedux.getSelectorStub<AppState, Item[]>();
const expectedValue = // mocked object representing the state;

stub.next(expectedValue);
stub.complete();

componentUnderTest.items.subscribe(
  actualValues => expect(actualValues).toEqual(expectedValue),
  null,
  done
);

But I'm getting this error in my test:

Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

Questions:

Is there a way to test a selector which uses a selector function with a parameter of the entire state? I'm using reselect for some of my selectors, but not all, and it's not working for either (see related issue: https://github.com/angular-redux/store/issues/533)

lcecil avatar Jun 21 '19 16:06 lcecil

My personal approach would be to ensure that you are unit testing the selector function itself only. So in your case, there should be an isolated suite of tests for getItems. Since you would fully exercise that function in a separate suite, I would skip it in your Component suite. I will however take a look at this within our suite.

smithad15 avatar Jul 07 '19 23:07 smithad15

Hi Andrew,

Thanks for the reply. Yes, I am testing that selector function in its own suite of tests, for sure. However, we're aiming for 100% coverage in our Component suites, and Karma is complaining those functions/lines aren't covered.

Perhaps I'll try mocking the function response through a spy. Let me know if you come up with any other solutions.

lcecil avatar Jul 10 '19 21:07 lcecil