-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
Preview docs changes for 24cd217 at https://previews.ngrx.io/pr1797-24cd217/ |
|
||
Usage: | ||
|
||
<code-example header="auth.guard.ts"> |
There was a problem hiding this comment.
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.
<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"> |
There was a problem hiding this comment.
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"] |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use createReducer
function here
There was a problem hiding this comment.
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?
Preview docs changes for 4329f17 at https://previews.ngrx.io/pr1797-4329f17/ |
<html> | ||
<head> | ||
<title>Angular App</title> | ||
</head> |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### Using Mock Selectors | |
### Using Mock Selectors |
@jtcrowson is this ready to be merged now? |
Preview docs changes for 1061467 at https://previews.ngrx.io/pr1797-1061467/ |
@brandonroberts only issue is the live example doesn't seem to be generating correctly in stackblitz. Check out the live example link in |
Ahh. That's because our dependencies haven't been updated here https://github.com/ngrx/platform/blob/master/projects/ngrx.io/tools/examples/shared/boilerplate/cli/package.json |
It seems to be complaining about creatReducer. Is it because it hasn’t been published as a beta version yet? |
I just published |
|
||
`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. |
There was a problem hiding this comment.
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?
`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()`. |
There was a problem hiding this comment.
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>
Preview docs changes for 14e20a5 at https://previews.ngrx.io/pr1797-14e20a5/ |
Preview docs changes for 2679b0f at https://previews.ngrx.io/pr1797-2679b0f/ |
@brandonroberts updated |
I restarted the job. Should be good to go now |
}; | ||
|
||
export const reducer = createReducer<State>( | ||
[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ | |
initialState, | |
[ |
on(AuthActions.login, (): State => ({ loggedIn: true })), | ||
on(AuthActions.logout, (): State => ({ loggedIn: false })) | ||
], | ||
initialState |
There was a problem hiding this comment.
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 fixed the new |
Preview docs changes for cf56e40 at https://previews.ngrx.io/pr1797-cf56e40/ |
loggedIn: false, | ||
}; | ||
|
||
export const reducer = createReducer<State>( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To resolve the stackblitz error, add jasmine
in https://github.com/ngrx/platform/blob/master/projects/ngrx.io/tools/examples/shared/boilerplate/cli/package.json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preview docs changes for 9b66485 at https://previews.ngrx.io/pr1797-9b66485/ |
Preview docs changes for f5d6d25 at https://previews.ngrx.io/pr1797-f5d6d25/ |
Preview docs changes for 976090c at https://previews.ngrx.io/pr1797-976090c/ |
Nice! Stackblitz example is up and running! Thanks for your help @timdeschryver and @brandonroberts! Ready to merge unless anything else is requested. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Partially closes #1761, @damienwebdev to add another example.
What is the new behavior?
projects/ngrx.io/content/examples/testing-store
to use in guideDoes this PR introduce a breaking change?