Skip to content

Commit

Permalink
add checkCompileToFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
38elements committed Jul 18, 2018
1 parent 041c17a commit 89c66a3
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 28 deletions.
2 changes: 2 additions & 0 deletions packages/create-instance/create-render-slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function createVNodes (
slotValue: Component | string
): ?Array<VNode> {
if (typeof slotValue === 'string') {
// Since compileToFunctions is checked in createSlotVNodes(),
// it is not necessary to check compileToFunctions.
const compiledResult = compileToFunctions(`<div>${slotValue}</div>`)
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
vm._renderProxy.$options.staticRenderFns = compiledResult.staticRenderFns
Expand Down
2 changes: 2 additions & 0 deletions packages/create-instance/create-scoped-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Vue from 'vue'
import { compileToFunctions } from 'vue-template-compiler'
import { throwError, vueVersion } from 'shared/util'
import { checkCompileToFunctions } from 'shared/validators'

function isDestructuringSlotScope (slotScope: string): boolean {
return slotScope[0] === '{' && slotScope[slotScope.length - 1] === '}'
Expand Down Expand Up @@ -36,6 +37,7 @@ function getVueTemplateCompilerHelpers (): { [name: string]: Function } {
}

function validateEnvironment (): void {
checkCompileToFunctions()
if (window.navigator.userAgent.match(/PhantomJS/i)) {
throwError(
`the scopedSlots option does not support PhantomJS. ` +
Expand Down
2 changes: 2 additions & 0 deletions packages/create-instance/create-slot-vnodes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import { compileToFunctions } from 'vue-template-compiler'
import { checkCompileToFunctions } from 'shared/validators'

function startsWithTag (str: SlotValue): boolean {
return typeof str === 'string' && str.trim()[0] === '<'
Expand All @@ -15,6 +16,7 @@ function createVNodesForSlot (
return slotValue
}

checkCompileToFunctions()
const el =
typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue

Expand Down
20 changes: 7 additions & 13 deletions packages/create-instance/validate-slots.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// @flow

import { throwError } from 'shared/util'
import { compileToFunctions } from 'vue-template-compiler'
import { isVueComponent } from '../shared/validators'
import {
checkCompileToFunctions,
isVueComponent
} from 'shared/validators'

function isValidSlot (slot: any): boolean {
return (
Expand All @@ -11,16 +13,6 @@ function isValidSlot (slot: any): boolean {
)
}

function requiresTemplateCompiler (slot: any): void {
if (typeof slot === 'string' && !compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
`precompiled components if vue-template-compiler is ` +
`undefined`
)
}
}

export function validateSlots (slots: SlotsObject): void {
Object.keys(slots).forEach(key => {
const slot = Array.isArray(slots[key]) ? slots[key] : [slots[key]]
Expand All @@ -32,7 +24,9 @@ export function validateSlots (slots: SlotsObject): void {
`of Components`
)
}
requiresTemplateCompiler(slotValue)
if (typeof slotValue === 'string') {
checkCompileToFunctions()
}
})
})
}
2 changes: 2 additions & 0 deletions packages/shared/compile-template.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @flow

import { compileToFunctions } from 'vue-template-compiler'
import { checkCompileToFunctions } from './validators'

export function compileTemplate (component: Component): void {
if (component.template) {
checkCompileToFunctions()
Object.assign(component, compileToFunctions(component.template))
}

Expand Down
17 changes: 3 additions & 14 deletions packages/shared/stub-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
hyphenate
} from './util'
import {
checkCompileToFunctions,
componentNeedsCompiling,
templateContainsComponent,
isVueComponent
Expand Down Expand Up @@ -66,13 +67,7 @@ function createStubFromString (
originalComponent: Component,
name: string
): Component {
if (!compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
`precompiled components if vue-template-compiler is ` +
`undefined`
)
}
checkCompileToFunctions()

if (templateContainsComponent(templateString, name)) {
throwError('options.stub cannot contain a circular reference')
Expand Down Expand Up @@ -176,13 +171,7 @@ export function createComponentStubs (
}
} else {
if (typeof stub === 'string') {
if (!compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
`precompiled components if vue-template-compiler is ` +
`undefined`
)
}
checkCompileToFunctions()
components[stubName] = {
...compileToFunctions(stub)
}
Expand Down
11 changes: 11 additions & 0 deletions packages/shared/validators.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// @flow
import { compileToFunctions } from 'vue-template-compiler'
import { throwError, capitalize, camelize, hyphenate } from './util'

export function checkCompileToFunctions (): void {
if (!compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
`precompiled components if vue-template-compiler is ` +
`undefined`
)
}
}

export function isDomSelector (selector: any): boolean {
if (typeof selector !== 'string') {
return false
Expand Down
41 changes: 40 additions & 1 deletion test/specs/mounting-options/scopedSlots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
isRunningPhantomJS
} from '~resources/utils'
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
import { itDoNotRunIf } from 'conditional-specs'
import { itSkipIf, itDoNotRunIf } from 'conditional-specs'

describeWithShallowAndMount('scopedSlots', mountingMethod => {
const windowSave = window
Expand Down Expand Up @@ -165,4 +165,43 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
.with.property('message', message)
}
)

itSkipIf(
mountingMethod.name === 'renderToString',
'throws error if passed string in default slot object and vue-template-compiler is undefined',
() => {
const compilerSave =
require.cache[require.resolve('vue-template-compiler')].exports
.compileToFunctions
require.cache[
require.resolve('vue-template-compiler')
].exports.compileToFunctions = undefined
delete require.cache[require.resolve('../../../packages/test-utils')]
const mountingMethodFresh = require('../../../packages/test-utils')[
mountingMethod.name
]
const message =
'[vue-test-utils]: vueTemplateCompiler is undefined, you must pass precompiled components if vue-template-compiler is undefined'
const fn = () => {
mountingMethodFresh(ComponentWithScopedSlots, {
scopedSlots: {
list: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>'
}
})
}
try {
expect(fn)
.to.throw()
.with.property('message', message)
} catch (err) {
require.cache[
require.resolve('vue-template-compiler')
].exports.compileToFunctions = compilerSave
throw err
}
require.cache[
require.resolve('vue-template-compiler')
].exports.compileToFunctions = compilerSave
}
)
})
38 changes: 38 additions & 0 deletions test/specs/wrapper.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describeWithShallowAndMount } from '~resources/utils'
import { itSkipIf } from 'conditional-specs'

describeWithShallowAndMount('Wrapper', mountingMethod => {
['vnode', 'element', 'vm', 'options'].forEach(property => {
Expand All @@ -12,4 +13,41 @@ describeWithShallowAndMount('Wrapper', mountingMethod => {
.with.property('message', message)
})
})

itSkipIf(
mountingMethod.name === 'renderToString',
'throws error if passed string in default slot array when vue-template-compiler is undefined',
() => {
const compilerSave =
require.cache[require.resolve('vue-template-compiler')].exports
.compileToFunctions
require.cache[require.resolve('vue-template-compiler')].exports = {
compileToFunctions: undefined
}
delete require.cache[require.resolve('../../packages/test-utils')]
const mountingMethodFresh = require('../../packages/test-utils')[
mountingMethod.name
]
const message =
'[vue-test-utils]: vueTemplateCompiler is undefined, you must pass precompiled components if vue-template-compiler is undefined'
const fn = () => {
mountingMethodFresh({
template: '<div />'
})
}
try {
expect(fn)
.to.throw()
.with.property('message', message)
} catch (err) {
require.cache[
require.resolve('vue-template-compiler')
].exports.compileToFunctions = compilerSave
throw err
}
require.cache[
require.resolve('vue-template-compiler')
].exports.compileToFunctions = compilerSave
}
)
})

0 comments on commit 89c66a3

Please sign in to comment.