-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Adds mockAsyncRedwoodDirective to test asynchronous Validator or Transformer RedwoodDirectives #5822
Adds mockAsyncRedwoodDirective to test asynchronous Validator or Transformer RedwoodDirectives #5822
Conversation
✅ Deploy Preview for redwoodjs-docs ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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.
Thanks @dthyresson, looking good! Works locally for me. I made a few edits to the docs. The example in the JSDoc may need updating. And I'm a little confused if it should be mockedResolvedValue
or resolvedValue
for transformer directives?
While this work as is, I have an API suggestion. What if instead of making another function, we just check if directive.onResolverCalled
is async or not? That way users don't have to learn another API.
To be more explicit, the code would look like...
export const mockRedwoodDirective: DirectiveMocker = (
directive,
executionMock
) => {
const { directiveArgs = {}, context, ...others } = executionMock
if (context) {
setContext(context || {})
}
// Here's the new code; just do a check here, instead of making another function
if (directive.onResolverCalled.constructor.name === 'AsyncFunction') {
return async () => {
if (directive.type === DirectiveType.TRANSFORMER) {
const { mockedResolvedValue } = others as TransformerMock
return await directive.onResolverCalled({
resolvedValue: mockedResolvedValue,
context: globalContext,
...others,
} as DirectiveParams)
} else {
await directive.onResolverCalled({
context: globalContext,
directiveArgs,
...others,
} as DirectiveParams)
}
}
}
return () => {
if (directive.type === DirectiveType.TRANSFORMER) {
const { mockedResolvedValue } = others as TransformerMock
return directive.onResolverCalled({
resolvedValue: mockedResolvedValue,
context: globalContext,
...others,
} as DirectiveParams)
} else {
directive.onResolverCalled({
context: globalContext,
directiveArgs,
...others,
} as DirectiveParams)
}
}
}
I tested it out locally and it seems to work. Thoughts on this approach?
* @example | ||
* const mockExecution = mockAsyncRedwoodDirective(myTransformer, { | ||
* context: currentUser, | ||
* resolvedValue: 'Original Value', |
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 docs say mockedResolvedValue
but here says resolvedValue
—which should it be?
Co-authored-by: Dominic Saadi <[email protected]>
Co-authored-by: Dominic Saadi <[email protected]>
Co-authored-by: Dominic Saadi <[email protected]>
Co-authored-by: Dominic Saadi <[email protected]>
@jtoar I like that much better. I was taking my cue from @xmonkee and #5713. But a single function simplifies much! |
@jtoar Do you want to push that refactoring into this PR? Much appreciated! |
@dthyresson just pushed the suggestion. This one seems good to go but maybe we can give xmonkee a few days for feedback. |
@dthyresson I've been waiting for xmonkee's feedback here before merging but let me know if you got feedback on another channel. |
Fixes #5713