-
Notifications
You must be signed in to change notification settings - Fork 264
feat: stubs mounting option #56
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0db36c3
wip: stubs
lmiller1990 361019d
wip: tests for stubs
lmiller1990 d4897f1
feat: stubs
lmiller1990 61b5e2b
chore: add comment to clarify behavior
lmiller1990 11f47af
fix: reset h args each test
lmiller1990 d082064
feat: support sfc as a stub
lmiller1990 d515f3f
tests: add tests for inline stubs
lmiller1990 1a5024e
refactor: extract functions to improve readability
lmiller1990 d14dc59
fix: support kebab case
lmiller1990 2355f55
tests: add test for pascal case component name
lmiller1990 1c65bf7
tests: stub component with camelcase naming
lmiller1990 485b1ac
refactor: add comments
lmiller1990 126d5dd
refactor: update rollup build
lmiller1990 de4e38d
chore: rollup config
lmiller1990 da5d3cb
chore: workflow
lmiller1990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { transformVNodeArgs, h } from 'vue' | ||
import kebabCase from 'lodash/kebabCase' | ||
|
||
import { pascalCase } from './utils' | ||
|
||
interface IStubOptions { | ||
name?: string | ||
} | ||
|
||
// TODO: figure out how to type this | ||
type VNodeArgs = any[] | ||
|
||
export const createStub = (options: IStubOptions) => { | ||
const tag = options.name ? `${options.name}-stub` : 'anonymous-stub' | ||
const render = () => h(tag) | ||
|
||
return { name: tag, render } | ||
} | ||
|
||
const resolveComponentStubByName = (name: string, stubs: Record<any, any>) => { | ||
const pascal = pascalCase(name) | ||
const kebab = kebabCase(name) | ||
|
||
for (const [key, value] of Object.entries(stubs)) { | ||
if (name === pascal || name === kebab) { | ||
return value | ||
} | ||
} | ||
} | ||
|
||
const isHTMLElement = (args: VNodeArgs) => | ||
Array.isArray(args) && typeof args[0] === 'string' | ||
const isCommentOrFragment = (args: VNodeArgs) => typeof args[0] === 'symbol' | ||
const isParent = (args: VNodeArgs) => | ||
typeof args[0] === 'object' && args[0]['name'] === 'VTU_COMPONENT' | ||
const isComponent = (args: VNodeArgs) => typeof args[0] === 'object' | ||
|
||
export function stubComponents(stubs: Record<any, any>) { | ||
transformVNodeArgs((args) => { | ||
if (isHTMLElement(args) || isCommentOrFragment(args) || isParent(args)) { | ||
return args | ||
} | ||
|
||
if (isComponent(args)) { | ||
const name = args[0]['name'] | ||
if (!name) { | ||
return args | ||
} | ||
|
||
const stub = resolveComponentStubByName(name, stubs) | ||
// we return a stub by matching Vue's `h` function | ||
// where the signature is h(Component, props) | ||
|
||
// default stub | ||
if (stub === true) { | ||
return [createStub({ name }), {}] | ||
} | ||
|
||
// custom implementation | ||
if (typeof stub === 'object') { | ||
return [stubs[name], {}] | ||
} | ||
} | ||
|
||
return args | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import camelCase from 'lodash/camelCase' | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Lodash told me to do it this way lodash/lodash#3102 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
import { h, ComponentOptions } from 'vue' | ||
|
||
import { mount } from '../../src' | ||
import Hello from '../components/Hello.vue' | ||
|
||
describe('mounting options: stubs', () => { | ||
it('stubs in a fragment', () => { | ||
const Foo = { | ||
name: 'Foo', | ||
render() { | ||
return h('p') | ||
} | ||
} | ||
const Component: ComponentOptions = { | ||
render() { | ||
return h(() => [h('div'), h(Foo)]) | ||
} | ||
} | ||
|
||
const wrapper = mount(Component, { | ||
global: { | ||
stubs: { | ||
Foo: true | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.html()).toBe('<div></div><foo-stub></foo-stub>') | ||
}) | ||
|
||
it('prevents lifecycle hooks triggering in a stub', () => { | ||
const onBeforeMount = jest.fn() | ||
const beforeCreate = jest.fn() | ||
const Foo = { | ||
name: 'Foo', | ||
setup() { | ||
onBeforeMount(onBeforeMount) | ||
return () => h('div') | ||
}, | ||
beforeCreate | ||
} | ||
const Comp = { | ||
render() { | ||
return h(Foo) | ||
} | ||
} | ||
|
||
const wrapper = mount(Comp, { | ||
global: { | ||
stubs: { | ||
Foo: true | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.html()).toBe('<foo-stub></foo-stub>') | ||
expect(onBeforeMount).not.toHaveBeenCalled() | ||
expect(beforeCreate).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('uses a custom stub implementation', () => { | ||
const onBeforeMount = jest.fn() | ||
const FooStub = { | ||
name: 'FooStub', | ||
setup() { | ||
onBeforeMount(onBeforeMount) | ||
return () => h('div', 'foo stub') | ||
} | ||
} | ||
const Foo = { | ||
name: 'Foo', | ||
render() { | ||
return h('div', 'real foo') | ||
} | ||
} | ||
|
||
const Comp = { | ||
render() { | ||
return h(Foo) | ||
} | ||
} | ||
|
||
const wrapper = mount(Comp, { | ||
global: { | ||
stubs: { | ||
Foo: FooStub | ||
} | ||
} | ||
}) | ||
|
||
expect(onBeforeMount).toHaveBeenCalled() | ||
expect(wrapper.html()).toBe('<div>foo stub</div>') | ||
}) | ||
|
||
it('uses an sfc as a custom stub', () => { | ||
const created = jest.fn() | ||
const HelloComp = { | ||
name: 'Hello', | ||
created() { | ||
created() | ||
}, | ||
render() { | ||
return h('span', 'real implementation') | ||
} | ||
} | ||
|
||
const Comp = { | ||
render() { | ||
return h(HelloComp) | ||
} | ||
} | ||
|
||
const wrapper = mount(Comp, { | ||
global: { | ||
stubs: { | ||
Hello: Hello | ||
} | ||
} | ||
}) | ||
|
||
expect(created).not.toHaveBeenCalled() | ||
expect(wrapper.html()).toBe( | ||
'<div id="root"><div id="msg">Hello world</div></div>' | ||
) | ||
}) | ||
|
||
it('stubs using inline components', () => { | ||
const Foo = { | ||
name: 'Foo', | ||
render() { | ||
return h('p') | ||
} | ||
} | ||
const Bar = { | ||
name: 'Bar', | ||
render() { | ||
return h('p') | ||
} | ||
} | ||
const Component: ComponentOptions = { | ||
render() { | ||
return h(() => [h(Foo), h(Bar)]) | ||
} | ||
} | ||
|
||
const wrapper = mount(Component, { | ||
global: { | ||
stubs: { | ||
Foo: { | ||
template: '<span />' | ||
}, | ||
Bar: { | ||
render() { | ||
return h('div') | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.html()).toBe('<span></span><div></div>') | ||
}) | ||
|
||
it('stubs a component with a kabeb-case name', () => { | ||
const FooBar = { | ||
name: 'foo-bar', | ||
render: () => h('span', 'real foobar') | ||
} | ||
const Comp = { | ||
render: () => h(FooBar) | ||
} | ||
const wrapper = mount(Comp, { | ||
global: { | ||
stubs: { | ||
FooBar: true | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.html()).toBe('<foo-bar-stub></foo-bar-stub>') | ||
}) | ||
|
||
it('stubs a component with a PascalCase name', () => { | ||
const FooBar = { | ||
name: 'FooBar', | ||
render: () => h('span', 'real foobar') | ||
} | ||
const Comp = { | ||
render: () => h(FooBar) | ||
} | ||
const wrapper = mount(Comp, { | ||
global: { | ||
stubs: { | ||
'foo-bar': true | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.html()).toBe('<foobar-stub></foobar-stub>') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.