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

docs: add section to Store testing guide on using mock selectors #1797

Merged
merged 15 commits into from
May 5, 2019

Conversation

jtcrowson
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

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

What is the current behavior?

Partially closes #1761, @damienwebdev to add another example.

What is the new behavior?

  • Documents mock selectors with example.
  • Adds example to projects/ngrx.io/content/examples/testing-store to use in guide

Does this PR introduce a breaking change?

[ ] Yes
[x] No

@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 23, 2019

Preview docs changes for 24cd217 at https://previews.ngrx.io/pr1797-24cd217/


Usage:

<code-example header="auth.guard.ts">
Copy link
Member

Choose a reason for hiding this comment

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

Instead of inlining the code, provide a path attribute.

Suggested change
<code-example header="auth.guard.ts">
<code-example header="auth-guard.service.ts" path="testing-store/src/app/auth-guard.service.ts">

}
</code-example>

<code-example header="auth.guard.spec.ts">
Copy link
Member

Choose a reason for hiding this comment

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

Same here with path to example

@@ -0,0 +1,4 @@
{
"description": "Store Testing Tutorial",
"files": ["!**/*.d.ts", "!**/*.js"]
Copy link
Member

Choose a reason for hiding this comment

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

The files array needs to include a glob for the spec files so they get included in the generated StackBlitz.

@@ -0,0 +1,16 @@
import { Action } from '@ngrx/store';

export enum AuthActionTypes {
Copy link
Member

Choose a reason for hiding this comment

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

Use new createAction function for actions

loggedIn: false,
};

export function reducer(
Copy link
Member

Choose a reason for hiding this comment

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

Use createReducer function here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is createReducer available in a released beta version?

@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 24, 2019

Preview docs changes for 4329f17 at https://previews.ngrx.io/pr1797-4329f17/

<html>
<head>
<title>Angular App</title>
</head>
Copy link
Member

Choose a reason for hiding this comment

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

Add <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.99.0/jasmine.css"> to style the test page

@@ -66,8 +76,25 @@ export class CollectionEffects {
)
);

addBookToCollectionSuccess$ = createEffect(
Copy link
Member

Choose a reason for hiding this comment

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

Remove?

@@ -58,6 +58,24 @@ describe('Auth Guard', () => {
});
</code-example>

#### Using Mock Selectors
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#### Using Mock Selectors
### Using Mock Selectors

@brandonroberts
Copy link
Member

@jtcrowson is this ready to be merged now?

@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 24, 2019

Preview docs changes for 1061467 at https://previews.ngrx.io/pr1797-1061467/

@jtcrowson
Copy link
Contributor Author

@brandonroberts only issue is the live example doesn't seem to be generating correctly in stackblitz. Check out the live example link in projects/ngrx.io/content/guide/store/testing.md

@brandonroberts
Copy link
Member

@jtcrowson
Copy link
Contributor Author

It seems to be complaining about creatReducer. Is it because it hasn’t been published as a beta version yet?

@jtcrowson jtcrowson changed the title [WIP] docs: add section to Store testing guide on using mock selectors docs: add section to Store testing guide on using mock selectors Apr 24, 2019
@brandonroberts
Copy link
Member

I just published 8.0.0-beta.1 that includes createReducer. The initialState is first in the array now instead of last.

projects/ngrx.io/content/guide/store/testing.md Outdated Show resolved Hide resolved

`overrideSelector()` returns a `MemoizedSelector`. To update the mock selector to return a different value, use the `MemoizedSelector`'s `setResult()` method.

`overrideSelector()` supports mocking the `select` method (used in RxJS pipe) and the `Store` `select` instance method using a string or selector.
Copy link
Member

Choose a reason for hiding this comment

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

I think you wanted to say setResult here?
Could we also simplify the wording as the following?

Suggested change
`overrideSelector()` supports mocking the `select` method (used in RxJS pipe) and the `Store` `select` instance method using a string or selector.
`setResult()` supports mocking the `select` method and returns the given input provided in `setResult()`.

Copy link
Contributor Author

@jtcrowson jtcrowson Apr 25, 2019

Choose a reason for hiding this comment

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

I intended to use overrideSelector and summarize the note from #1688:

Covers mocking different types of selectors:

store.select('slice');
const selector = createSelector(state, state => state.slice);
store.select(selector);
const selector = createSelector(state, state => state.slice);
store.pipe(select(selector));

Co-Authored-By: jtcrowson <John.Crowson@CapitalOne.com>
@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 25, 2019

Preview docs changes for 14e20a5 at https://previews.ngrx.io/pr1797-14e20a5/

@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 25, 2019

Preview docs changes for 2679b0f at https://previews.ngrx.io/pr1797-2679b0f/

@jtcrowson
Copy link
Contributor Author

@brandonroberts updated projects/ngrx.io/tools/examples/shared/boilerplate/cli/package.json to the latest beta. CICD failing due to an issue that seems intermittent with the @ngrx/data testing of pluralize.

@brandonroberts
Copy link
Member

I restarted the job. Should be good to go now

};

export const reducer = createReducer<State>(
[
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[
initialState,
[

on(AuthActions.login, (): State => ({ loggedIn: true })),
on(AuthActions.logout, (): State => ({ loggedIn: false }))
],
initialState
Copy link
Member

Choose a reason for hiding this comment

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

Remove initialState as the last argument

@brandonroberts brandonroberts added the Needs Cleanup Review changes needed label Apr 26, 2019
@jtcrowson
Copy link
Contributor Author

@brandonroberts fixed the new createReducer argument label ordering. Still can't seem to get stackblitz to run correctly.

@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 26, 2019

Preview docs changes for cf56e40 at https://previews.ngrx.io/pr1797-cf56e40/

loggedIn: false,
};

export const reducer = createReducer<State>(
Copy link
Member

@timdeschryver timdeschryver Apr 27, 2019

Choose a reason for hiding this comment

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

The ons don't need to be wrapped in an array:

export const reducer = createReducer<State>(
  initialState,
  on(AuthActions.login, (): State => ({ loggedIn: true })),
  on(AuthActions.logout, (): State => ({ loggedIn: false }))
);

This should also resolve the stackblitz error that you're encountering

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

While you're in that file we should also add tslib:

image

(this change is needed for all the examples - this dep was needed for store migrations)

@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 27, 2019

Preview docs changes for 9b66485 at https://previews.ngrx.io/pr1797-9b66485/

@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 27, 2019

Preview docs changes for f5d6d25 at https://previews.ngrx.io/pr1797-f5d6d25/

@ngrxbot
Copy link
Collaborator

ngrxbot commented Apr 27, 2019

Preview docs changes for 976090c at https://previews.ngrx.io/pr1797-976090c/

@jtcrowson
Copy link
Contributor Author

jtcrowson commented Apr 27, 2019

Nice! Stackblitz example is up and running! Thanks for your help @timdeschryver and @brandonroberts! Ready to merge unless anything else is requested.

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.

👍

@timdeschryver timdeschryver removed the Needs Cleanup Review changes needed label May 3, 2019
@brandonroberts brandonroberts merged commit 8755d07 into ngrx:master May 5, 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.

Docs: Add section to Store testing guide on using mock selectors
4 participants