Skip to content

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 15 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run lint
- run: npm run build
- run: npm test
- run: yarn install
- run: yarn lint
- run: yarn test
- run: yarn build
Comment on lines +27 to +30
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

env:
CI: true
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
"dist",
"README.md"
],
"dependencies": {
"lodash": "^4.17.15"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.8.4",
"@babel/types": "^7.8.3",
"@rollup/plugin-node-resolve": "^7.1.3",
"@types/estree": "^0.0.42",
"@types/jest": "^24.9.1",
"@types/lodash": "^4.14.149",
"@vue/compiler-sfc": "^3.0.0-alpha.12",
"babel-jest": "^25.2.3",
"babel-preset-jest": "^25.2.1",
Expand Down
12 changes: 10 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ts from 'rollup-plugin-typescript2'
import resolve from '@rollup/plugin-node-resolve'

import pkg from './package.json'

Expand All @@ -19,8 +20,15 @@ function createEntry(options) {

const config = {
input,
external: ['vue'],
plugins: [],
external: [
'vue',
'lodash/mergeWith',
'lodash/camelCase',
'lodash/upperFirst',
'lodash/kebabCase',
'lodash/flow'
],
plugins: [resolve()],
output: {
banner,
file: 'dist/vue-test-utils.other.js',
Expand Down
11 changes: 11 additions & 0 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
defineComponent,
VNodeNormalizedChildren,
ComponentOptions,
transformVNodeArgs,
Plugin,
Directive,
Component,
Expand All @@ -15,6 +16,7 @@ import { createWrapper } from './vue-wrapper'
import { createEmitMixin } from './emitMixin'
import { createDataMixin } from './dataMixin'
import { MOUNT_ELEMENT_ID } from './constants'
import { stubComponents } from './stubs'

type Slot = VNode | string | { render: Function }

Expand All @@ -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.

stubs?: Record<any, any>
provide?: Record<any, any>
// TODO how to type `defineComponent`? Using `any` for now.
components?: Record<string, Component | object>
Expand Down Expand Up @@ -72,6 +75,7 @@ export function mount(originalComponent: any, options?: MountingOptions) {

// create the wrapper component
const Parent = defineComponent({
name: 'VTU_COMPONENT',
render() {
return h(component, props, slots)
}
Expand Down Expand Up @@ -133,6 +137,13 @@ export function mount(originalComponent: any, options?: MountingOptions) {
const { emitMixin, events } = createEmitMixin()
vm.mixin(emitMixin)

// stubs
if (options?.global?.stubs) {
stubComponents(options.global.stubs)
} else {
transformVNodeArgs()
}

// mount the app!
const app = vm.mount(el)

Expand Down
80 changes: 80 additions & 0 deletions src/stubs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { transformVNodeArgs, h } from 'vue'

import { pascalCase, kebabCase } 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 = (
componentName: string,
stubs: Record<any, any>
) => {
const componentPascalName = pascalCase(componentName)
const componentKebabName = kebabCase(componentName)

for (const [stubKey, value] of Object.entries(stubs)) {
if (
stubKey === componentPascalName ||
stubKey === componentKebabName ||
stubKey === componentName
) {
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) => {
// args[0] can either be:
// 1. a HTML tag (div, span...)
// 2. An object of component options, such as { name: 'foo', render: [Function], props: {...} }
// Depending what it is, we do different things.
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)
// case 1: default stub
if (stub === true) {
return [createStub({ name }), {}]
}

// case 2: custom implementation
if (typeof stub === 'object') {
return [stubs[name], {}]
}
}

return args
})
}
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import camelCase from 'lodash/camelCase'
import upperFirst from 'lodash/upperFirst'
import kebabCase from 'lodash/kebabCase'
import flow from 'lodash/flow'

const pascalCase = flow(camelCase, upperFirst)

export { kebabCase, pascalCase }
Loading