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: stubs mounting option #56

Merged
merged 15 commits into from
Apr 14, 2020
Merged

feat: stubs mounting option #56

merged 15 commits into from
Apr 14, 2020

Conversation

lmiller1990
Copy link
Member

@lmiller1990 lmiller1990 commented Apr 13, 2020

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 the global mounting option, since when you stub a component, it stubs them all the way down the tree (same as beta)
  • the stub is not a DOM node as discussed, but a bare-bones Vue component. Gives us some flexibility for implementing shallowMount.

Breaking change:

We do not support an array of stubs (at least right now). So instead of

stubs: [Foo, Bar]

You need to do:

stubs: {
  Foo: true, Bar: True
}

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.

@@ -29,6 +31,7 @@ interface MountingOptions {
plugins?: Plugin[]
mixins?: ComponentOptions[]
mocks?: Record<string, any>
Copy link
Member Author

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

Copy link
Contributor

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'] })]
}
Copy link
Member Author

@lmiller1990 lmiller1990 Apr 13, 2020

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.

Copy link
Contributor

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.

@lmiller1990 lmiller1990 mentioned this pull request Apr 13, 2020
src/mount.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@dobromir-hristov dobromir-hristov left a 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
Copy link
Contributor

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.

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

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 :)

Copy link
Member Author

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) => {
Copy link
Contributor

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?

Suggested change
transformVNodeArgs((args) => {
transformVNodeArgs(([componentOptions]) => {

Copy link
Member Author

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]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return [options?.global?.stubs[name]]
return [options.global.stubs[name]]

Copy link
Member Author

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) {
Copy link
Contributor

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.

Suggested change
if (options?.global?.stubs[name] === true) {
if (options.global.stubs[name] === true) {

Copy link
Member Author

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

Copy link
Contributor

@JessicaSachs JessicaSachs left a 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.

@lmiller1990
Copy link
Member Author

lmiller1990 commented Apr 14, 2020

@JessicaSachs we support render functions. You can do this:

stubs: {
  Foo: { render() { return h('div') } 
}

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)
Copy link
Member Author

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

Comment on lines +27 to +30
- run: yarn install
- run: yarn lint
- run: yarn test
- run: yarn build
Copy link
Member

Choose a reason for hiding this comment

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

oops :D

@lmiller1990
Copy link
Member Author

Going to merge this up. I fully expect to revisit this code soon, probably for shallow.

@lmiller1990 lmiller1990 merged commit 08e2881 into master Apr 14, 2020
@lmiller1990 lmiller1990 deleted the feature/stubs branch April 14, 2020 13:35
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.

feat: stubs mounting option
4 participants