-
Notifications
You must be signed in to change notification settings - Fork 262
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: stubs mounting option #56
Conversation
@@ -29,6 +31,7 @@ interface MountingOptions { | |||
plugins?: Plugin[] | |||
mixins?: ComponentOptions[] | |||
mocks?: Record<string, any> |
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.
Typing Vue components is very difficult, will need to investigate the Vue codebase to figure out how best to do this
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 noticed this too. Am interested to hear about what you find.
src/mount.ts
Outdated
// default stub | ||
if (options?.global?.stubs[name] === true) { | ||
return [createStub({ name: args[0]['name'] })] | ||
} |
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.
@dobromir-hristov we one of us gets to working on findComponent
we will probably need to pass props
as the second arg here, so we can do findComponent(Foo).props)
. So it will be like
return [createStub({ name: args[0]['name'] }), args[1]]
or something. I didn't do it here because I am not exactly sure how this will work yet, but I don't expect it to be a problem.
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.
Yep, we will check it out.
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.
Very nice :) Turning up very well.
src/mount.ts
Outdated
|
||
if ( | ||
typeof args[0] === 'object' && | ||
args[0]['name'] in options?.global?.stubs |
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.
Probably we should do that magic with the Name transformation here? PascalCase, snake-case, that sort of stuff.
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.
Yeah this is probably a good idea, make it easier to find the stubs, I will implement this
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 matched Vue 3, exact name, kebab case, pacal case (aka capital case)
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 also did that for the findComponent by name Branch. Need to sync these up later, so we don't duplicate :)
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.
Sure, sounds good!
src/mount.ts
Outdated
@@ -133,6 +137,46 @@ export function mount(originalComponent: any, options?: MountingOptions) { | |||
const { emitMixin, events } = createEmitMixin() | |||
vm.mixin(emitMixin) | |||
|
|||
// stubs | |||
if (options?.global?.stubs) { | |||
transformVNodeArgs((args) => { |
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.
Maybe we can destructure like this, so its easier to understand context later down?
transformVNodeArgs((args) => { | |
transformVNodeArgs(([componentOptions]) => { |
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 for the suggestion, but I am not really a fan of this refactor. This variable can be a string (eg 'div'), or an object of options, so componentOptions is a bit misleading. I guess the ideal name is htmlTagOrComponentOptions
. I will just put a comment here for now.
src/mount.ts
Outdated
|
||
// custom stub implementation | ||
if (typeof options?.global?.stubs[name] === 'object') { | ||
return [options?.global?.stubs[name]] |
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.
return [options?.global?.stubs[name]] | |
return [options.global.stubs[name]] |
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.
very good
src/mount.ts
Outdated
) { | ||
const name = args[0]['name'] | ||
// default stub | ||
if (options?.global?.stubs[name] === true) { |
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.
You are already in the conditional check, its there.
if (options?.global?.stubs[name] === true) { | |
if (options.global.stubs[name] === true) { |
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.
heh, copy pasted... thanks, I'll clean this up
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.
Did we discuss dropping support for render functions? If we’re dropping support for render functions, how does this affect JSX unit tests?
In general, I like this implementation. I have some of the same cleanup nits that Dob and Adria do.
I am concerned about the lack of support for JSX/render functions.
@JessicaSachs we support render functions. You can do this:
etc. There is some tests for this. We will support JSX/TSX too. I am not sure on the status of JSX/TSX support yet. |
src/utils.ts
Outdated
import upperFirst from 'lodash/upperFirst' | ||
import flow from 'lodash/flow' | ||
|
||
export const pascalCase = flow(camelCase, upperFirst) |
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.
Lodash told me to do it this way lodash/lodash#3102
- run: yarn install | ||
- run: yarn lint | ||
- run: yarn test | ||
- run: yarn build |
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.
oops :D
Going to merge this up. I fully expect to revisit this code soon, probably for |
resolves #3!!!! :partyparrot: I'm really happy with this implementation; it feels super clean, main because of the transforVNodeArgs function @JessicaSachs and Evan made in core.
Points of interest:
stubs
is now in theglobal
mounting option, since when you stub a component, it stubs them all the way down the tree (same as beta)Breaking change:
We do not support an array of stubs (at least right now). So instead of
You need to do:
If this is a bit problem for people we can implement array notation for beta. For alpha, I think this is a good place to leave this and move on to other features.