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

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

Open
2 of 9 tasks
lcecil opened this issue Jun 21, 2019 · 2 comments
Open
2 of 9 tasks

Comments

@lcecil
Copy link

lcecil commented Jun 21, 2019

This is a...

  • feature request
  • bug report
  • usage question

What toolchain are you using for transpilation/bundling?

  • @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: angular-redux/store#533)

@smithad15
Copy link
Member

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.

@lcecil
Copy link
Author

lcecil commented Jul 10, 2019

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.

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

No branches or pull requests

2 participants