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

feat(store): add option to mock selectors in MockStoreConfig #1836

Merged

Conversation

jtcrowson
Copy link
Contributor

@jtcrowson jtcrowson commented May 7, 2019

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Closes #1827

What is the new behavior?

Adds a mock selectors option to the MockStoreConfig. This will enable developers to mock selectors up front in the testing module configuration, similar to how you can provide an initial state.

This enables developers who do not need to update the state or selector to omit creating a MockStore instance.

describe('Auth Guard', () => {
  let guard: AuthGuard;
  // let store: MockStore<fromAuth.State>;
  // don't need local instance

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        AuthGuard,
        provideMockStore({
          initialState: //...,
          selectors: [{ selector: fromAuth.getLoggedIn, value: false }]
          // able to provide multiple selectors without local instance
        }),
      ],
    });
    guard = TestBed.get(AuthGuard);
  });

  it('should return false if the user state is not logged in', () => {
    const expected = cold('(a|)', { a: false });
    expect(guard.canActivate()).toBeObservable(expected);
  });
});

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Should I add documentation in this PR or a follow up PR? Seems to usually be done as a follow up.

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 7, 2019

Preview docs changes for e009b2f at https://previews.ngrx.io/pr1836-e009b2f/

Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left some minor comments.

if (mockSelectors) {
mockSelectors.forEach(mockSelector => {
const selector = mockSelector.selector;
if (typeof selector === 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this if statement is here? I think the overrideSelector is the same 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript doesn't seem to be able to infer between the two:

Screen Shot 2019-05-08 at 2 06 42 PM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we would tweak the overrideSelector type?

overrideSelector<
    T,
    Result,
    SelectorKind =
      | string
      | MemoizedSelector<T, Result>
      | MemoizedSelectorWithProps<T, any, Result>
>(selector: SelectorKind, value: Result): SelectorKind;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaking the overrideSelector type straighten things out :
Screen Shot 2019-05-09 at 5 00 53 PM
However, it breaks the type checking of overrideSelector in the wild:
Screen Shot 2019-05-09 at 5 02 25 PM
Should have a type error on the last line because false is not an applicable value for the selector.

modules/store/src/tokens.ts Outdated Show resolved Hide resolved
@ngrxbot
Copy link
Collaborator

ngrxbot commented May 8, 2019

Preview docs changes for ed7cc49 at https://previews.ngrx.io/pr1836-ed7cc49/

@brandonroberts brandonroberts merged commit 070228c into ngrx:master May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RFC: mock selectors in MockStoreConfig
4 participants