2019-09-04 06:11:04 +08:00
|
|
|
import {
|
|
|
|
createApp,
|
|
|
|
h,
|
|
|
|
nodeOps,
|
|
|
|
serializeInner,
|
|
|
|
provide,
|
|
|
|
inject,
|
|
|
|
resolveComponent,
|
|
|
|
resolveDirective,
|
2019-10-19 04:35:01 +08:00
|
|
|
withDirectives,
|
2019-09-04 06:11:04 +08:00
|
|
|
Plugin,
|
2019-09-06 08:59:45 +08:00
|
|
|
ref,
|
2020-03-23 23:08:22 +08:00
|
|
|
getCurrentInstance,
|
|
|
|
defineComponent
|
2019-09-04 06:11:04 +08:00
|
|
|
} from '@vue/runtime-test'
|
|
|
|
|
|
|
|
describe('api: createApp', () => {
|
|
|
|
test('mount', () => {
|
2020-03-23 23:08:22 +08:00
|
|
|
const Comp = defineComponent({
|
2019-09-04 06:11:04 +08:00
|
|
|
props: {
|
|
|
|
count: {
|
|
|
|
default: 0
|
|
|
|
}
|
|
|
|
},
|
2020-03-23 23:08:22 +08:00
|
|
|
setup(props) {
|
2019-09-06 06:48:49 +08:00
|
|
|
return () => props.count
|
2019-09-04 06:11:04 +08:00
|
|
|
}
|
2020-03-23 23:08:22 +08:00
|
|
|
})
|
2019-09-04 06:11:04 +08:00
|
|
|
|
|
|
|
const root1 = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
createApp(Comp).mount(root1)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(serializeInner(root1)).toBe(`0`)
|
|
|
|
|
|
|
|
// mount with props
|
|
|
|
const root2 = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
const app2 = createApp(Comp, { count: 1 })
|
|
|
|
app2.mount(root2)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(serializeInner(root2)).toBe(`1`)
|
|
|
|
|
|
|
|
// remount warning
|
|
|
|
const root3 = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
app2.mount(root3)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(serializeInner(root3)).toBe(``)
|
|
|
|
expect(`already been mounted`).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
2020-01-17 01:23:47 +08:00
|
|
|
test('unmount', () => {
|
2020-03-23 23:08:22 +08:00
|
|
|
const Comp = defineComponent({
|
2020-01-17 01:23:47 +08:00
|
|
|
props: {
|
|
|
|
count: {
|
|
|
|
default: 0
|
|
|
|
}
|
|
|
|
},
|
2020-03-23 23:08:22 +08:00
|
|
|
setup(props) {
|
2020-01-17 01:23:47 +08:00
|
|
|
return () => props.count
|
|
|
|
}
|
2020-03-23 23:08:22 +08:00
|
|
|
})
|
2020-01-17 01:23:47 +08:00
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Comp)
|
|
|
|
app.mount(root)
|
2020-01-17 01:23:47 +08:00
|
|
|
|
|
|
|
app.unmount(root)
|
|
|
|
expect(serializeInner(root)).toBe(``)
|
|
|
|
})
|
|
|
|
|
2019-09-04 06:11:04 +08:00
|
|
|
test('provide', () => {
|
|
|
|
const Root = {
|
|
|
|
setup() {
|
|
|
|
// test override
|
|
|
|
provide('foo', 3)
|
|
|
|
return () => h(Child)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Child = {
|
|
|
|
setup() {
|
|
|
|
const foo = inject('foo')
|
|
|
|
const bar = inject('bar')
|
2020-02-26 23:20:10 +08:00
|
|
|
try {
|
|
|
|
inject('__proto__')
|
|
|
|
} catch (e) {}
|
2019-09-04 06:11:04 +08:00
|
|
|
return () => `${foo},${bar}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Root)
|
|
|
|
app.provide('foo', 1)
|
|
|
|
app.provide('bar', 2)
|
|
|
|
|
2019-09-04 06:11:04 +08:00
|
|
|
const root = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
app.mount(root)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(serializeInner(root)).toBe(`3,2`)
|
2020-02-26 23:20:10 +08:00
|
|
|
expect('[Vue warn]: injection "__proto__" not found.').toHaveBeenWarned()
|
2019-09-04 06:11:04 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('component', () => {
|
|
|
|
const Root = {
|
|
|
|
// local override
|
|
|
|
components: {
|
|
|
|
BarBaz: () => 'barbaz-local!'
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
// resolve in setup
|
|
|
|
const FooBar = resolveComponent('foo-bar') as any
|
|
|
|
return () => {
|
|
|
|
// resolve in render
|
|
|
|
const BarBaz = resolveComponent('bar-baz') as any
|
|
|
|
return h('div', [h(FooBar), h(BarBaz)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Root)
|
|
|
|
|
|
|
|
const FooBar = () => 'foobar!'
|
|
|
|
app.component('FooBar', FooBar)
|
|
|
|
expect(app.component('FooBar')).toBe(FooBar)
|
|
|
|
|
|
|
|
app.component('BarBaz', () => 'barbaz!')
|
|
|
|
|
|
|
|
app.component('BarBaz', () => 'barbaz!')
|
|
|
|
expect(
|
|
|
|
'Component "BarBaz" has already been registered in target app.'
|
|
|
|
).toHaveBeenWarnedTimes(1)
|
|
|
|
|
2019-09-04 06:11:04 +08:00
|
|
|
const root = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
app.mount(root)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('directive', () => {
|
|
|
|
const spy1 = jest.fn()
|
|
|
|
const spy2 = jest.fn()
|
|
|
|
const spy3 = jest.fn()
|
2019-09-04 06:30:52 +08:00
|
|
|
|
2019-09-04 06:11:04 +08:00
|
|
|
const Root = {
|
|
|
|
// local override
|
|
|
|
directives: {
|
|
|
|
BarBaz: { mounted: spy3 }
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
// resolve in setup
|
2019-10-06 03:37:55 +08:00
|
|
|
const FooBar = resolveDirective('foo-bar')!
|
2019-09-04 06:11:04 +08:00
|
|
|
return () => {
|
|
|
|
// resolve in render
|
2019-10-06 03:37:55 +08:00
|
|
|
const BarBaz = resolveDirective('bar-baz')!
|
2019-10-19 04:35:01 +08:00
|
|
|
return withDirectives(h('div'), [[FooBar], [BarBaz]])
|
2019-09-04 06:11:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Root)
|
|
|
|
|
|
|
|
const FooBar = { mounted: spy1 }
|
|
|
|
app.directive('FooBar', FooBar)
|
|
|
|
expect(app.directive('FooBar')).toBe(FooBar)
|
|
|
|
|
|
|
|
app.directive('BarBaz', {
|
|
|
|
mounted: spy2
|
|
|
|
})
|
|
|
|
|
|
|
|
app.directive('BarBaz', {
|
|
|
|
mounted: spy2
|
|
|
|
})
|
|
|
|
expect(
|
|
|
|
'Directive "BarBaz" has already been registered in target app.'
|
|
|
|
).toHaveBeenWarnedTimes(1)
|
|
|
|
|
2019-09-04 06:11:04 +08:00
|
|
|
const root = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
app.mount(root)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(spy1).toHaveBeenCalled()
|
|
|
|
expect(spy2).not.toHaveBeenCalled()
|
|
|
|
expect(spy3).toHaveBeenCalled()
|
2019-10-19 00:34:45 +08:00
|
|
|
|
|
|
|
app.directive('bind', FooBar)
|
|
|
|
expect(
|
|
|
|
`Do not use built-in directive ids as custom directive id: bind`
|
|
|
|
).toHaveBeenWarned()
|
2019-09-04 06:11:04 +08:00
|
|
|
})
|
|
|
|
|
2019-09-06 08:59:45 +08:00
|
|
|
test('mixin', () => {
|
|
|
|
const calls: string[] = []
|
|
|
|
const mixinA = {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
a: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created(this: any) {
|
|
|
|
calls.push('mixinA created')
|
|
|
|
expect(this.a).toBe(1)
|
|
|
|
expect(this.b).toBe(2)
|
|
|
|
expect(this.c).toBe(3)
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
calls.push('mixinA mounted')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const mixinB = {
|
2019-10-29 04:22:03 +08:00
|
|
|
name: 'mixinB',
|
2019-09-06 08:59:45 +08:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
b: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created(this: any) {
|
|
|
|
calls.push('mixinB created')
|
|
|
|
expect(this.a).toBe(1)
|
|
|
|
expect(this.b).toBe(2)
|
|
|
|
expect(this.c).toBe(3)
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
calls.push('mixinB mounted')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const Comp = {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
c: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created(this: any) {
|
|
|
|
calls.push('comp created')
|
|
|
|
expect(this.a).toBe(1)
|
|
|
|
expect(this.b).toBe(2)
|
|
|
|
expect(this.c).toBe(3)
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
calls.push('comp mounted')
|
|
|
|
},
|
|
|
|
render(this: any) {
|
|
|
|
return `${this.a}${this.b}${this.c}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Comp)
|
2019-09-06 08:59:45 +08:00
|
|
|
app.mixin(mixinA)
|
|
|
|
app.mixin(mixinB)
|
|
|
|
|
2019-10-29 04:22:03 +08:00
|
|
|
app.mixin(mixinA)
|
|
|
|
app.mixin(mixinB)
|
|
|
|
expect(
|
|
|
|
'Mixin has already been applied to target app'
|
|
|
|
).toHaveBeenWarnedTimes(2)
|
|
|
|
expect(
|
|
|
|
'Mixin has already been applied to target app: mixinB'
|
|
|
|
).toHaveBeenWarnedTimes(1)
|
|
|
|
|
2019-09-06 08:59:45 +08:00
|
|
|
const root = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
app.mount(root)
|
2019-09-06 08:59:45 +08:00
|
|
|
|
|
|
|
expect(serializeInner(root)).toBe(`123`)
|
|
|
|
expect(calls).toEqual([
|
|
|
|
'mixinA created',
|
|
|
|
'mixinB created',
|
|
|
|
'comp created',
|
|
|
|
'mixinA mounted',
|
|
|
|
'mixinB mounted',
|
|
|
|
'comp mounted'
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2019-09-04 06:11:04 +08:00
|
|
|
test('use', () => {
|
|
|
|
const PluginA: Plugin = app => app.provide('foo', 1)
|
|
|
|
const PluginB: Plugin = {
|
2019-12-24 23:33:47 +08:00
|
|
|
install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
|
2019-09-04 06:11:04 +08:00
|
|
|
}
|
2020-01-09 01:40:24 +08:00
|
|
|
class PluginC {
|
|
|
|
someProperty = {}
|
|
|
|
static install() {
|
|
|
|
app.provide('baz', 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const PluginD: any = undefined
|
2019-09-04 06:11:04 +08:00
|
|
|
|
|
|
|
const Root = {
|
|
|
|
setup() {
|
|
|
|
const foo = inject('foo')
|
|
|
|
const bar = inject('bar')
|
|
|
|
return () => `${foo},${bar}`
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 04:05:38 +08:00
|
|
|
|
|
|
|
const app = createApp(Root)
|
|
|
|
app.use(PluginA)
|
|
|
|
app.use(PluginB, 1, 1)
|
|
|
|
app.use(PluginC)
|
|
|
|
|
2019-09-04 06:11:04 +08:00
|
|
|
const root = nodeOps.createElement('div')
|
2020-01-24 04:05:38 +08:00
|
|
|
app.mount(root)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(serializeInner(root)).toBe(`1,2`)
|
2019-11-05 23:49:00 +08:00
|
|
|
|
|
|
|
app.use(PluginA)
|
|
|
|
expect(
|
|
|
|
`Plugin has already been applied to target app`
|
|
|
|
).toHaveBeenWarnedTimes(1)
|
2019-11-27 22:18:03 +08:00
|
|
|
|
2020-01-09 01:40:24 +08:00
|
|
|
app.use(PluginD)
|
2019-11-27 22:18:03 +08:00
|
|
|
expect(
|
|
|
|
`A plugin must either be a function or an object with an "install" ` +
|
|
|
|
`function.`
|
|
|
|
).toHaveBeenWarnedTimes(1)
|
2019-09-04 06:11:04 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('config.errorHandler', () => {
|
|
|
|
const error = new Error()
|
|
|
|
const count = ref(0)
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const handler = jest.fn((err, instance, info) => {
|
|
|
|
expect(err).toBe(error)
|
|
|
|
expect((instance as any).count).toBe(count.value)
|
|
|
|
expect(info).toBe(`render function`)
|
|
|
|
})
|
2019-09-04 06:11:04 +08:00
|
|
|
|
|
|
|
const Root = {
|
|
|
|
setup() {
|
|
|
|
const count = ref(0)
|
|
|
|
return {
|
|
|
|
count
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Root)
|
|
|
|
app.config.errorHandler = handler
|
|
|
|
app.mount(nodeOps.createElement('div'))
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(handler).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('config.warnHandler', () => {
|
2019-09-06 08:59:45 +08:00
|
|
|
let ctx: any
|
2020-01-24 04:05:38 +08:00
|
|
|
const handler = jest.fn((msg, instance, trace) => {
|
|
|
|
expect(msg).toMatch(`Component is missing template or render function`)
|
|
|
|
expect(instance).toBe(ctx.proxy)
|
|
|
|
expect(trace).toMatch(`Hello`)
|
|
|
|
})
|
2019-09-04 06:11:04 +08:00
|
|
|
|
|
|
|
const Root = {
|
2019-09-06 08:59:45 +08:00
|
|
|
name: 'Hello',
|
|
|
|
setup() {
|
|
|
|
ctx = getCurrentInstance()
|
|
|
|
}
|
2019-09-04 06:11:04 +08:00
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Root)
|
|
|
|
app.config.warnHandler = handler
|
|
|
|
app.mount(nodeOps.createElement('div'))
|
2019-09-06 08:59:45 +08:00
|
|
|
expect(handler).toHaveBeenCalledTimes(1)
|
2019-09-04 06:11:04 +08:00
|
|
|
})
|
2019-10-15 03:36:30 +08:00
|
|
|
|
|
|
|
describe('config.isNativeTag', () => {
|
|
|
|
const isNativeTag = jest.fn(tag => tag === 'div')
|
|
|
|
|
|
|
|
test('Component.name', () => {
|
|
|
|
const Root = {
|
|
|
|
name: 'div',
|
|
|
|
render() {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Root)
|
2019-10-15 03:36:30 +08:00
|
|
|
|
|
|
|
Object.defineProperty(app.config, 'isNativeTag', {
|
|
|
|
value: isNativeTag,
|
|
|
|
writable: false
|
|
|
|
})
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
app.mount(nodeOps.createElement('div'))
|
|
|
|
expect(
|
|
|
|
`Do not use built-in or reserved HTML elements as component id: div`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Component.components', () => {
|
2019-10-15 03:36:30 +08:00
|
|
|
const Root = {
|
|
|
|
components: {
|
|
|
|
div: () => 'div'
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Root)
|
|
|
|
Object.defineProperty(app.config, 'isNativeTag', {
|
|
|
|
value: isNativeTag,
|
|
|
|
writable: false
|
|
|
|
})
|
|
|
|
|
|
|
|
app.mount(nodeOps.createElement('div'))
|
2019-10-15 03:36:30 +08:00
|
|
|
expect(
|
|
|
|
`Do not use built-in or reserved HTML elements as component id: div`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
2019-10-19 00:34:45 +08:00
|
|
|
test('Component.directives', () => {
|
|
|
|
const Root = {
|
|
|
|
directives: {
|
|
|
|
bind: () => {}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp(Root)
|
|
|
|
Object.defineProperty(app.config, 'isNativeTag', {
|
|
|
|
value: isNativeTag,
|
|
|
|
writable: false
|
|
|
|
})
|
|
|
|
|
|
|
|
app.mount(nodeOps.createElement('div'))
|
2019-10-19 00:34:45 +08:00
|
|
|
expect(
|
|
|
|
`Do not use built-in directive ids as custom directive id: bind`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
2019-10-15 03:36:30 +08:00
|
|
|
test('register using app.component', () => {
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp({
|
|
|
|
render() {}
|
|
|
|
})
|
|
|
|
|
2019-10-15 03:36:30 +08:00
|
|
|
Object.defineProperty(app.config, 'isNativeTag', {
|
|
|
|
value: isNativeTag,
|
|
|
|
writable: false
|
|
|
|
})
|
|
|
|
|
|
|
|
app.component('div', () => 'div')
|
2020-01-24 04:05:38 +08:00
|
|
|
app.mount(nodeOps.createElement('div'))
|
2019-10-15 03:36:30 +08:00
|
|
|
expect(
|
|
|
|
`Do not use built-in or reserved HTML elements as component id: div`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
})
|
2020-03-25 21:28:37 +08:00
|
|
|
|
|
|
|
test('config.optionMergeStrategies', () => {
|
|
|
|
let merged: string
|
|
|
|
const App = defineComponent({
|
|
|
|
render() {},
|
|
|
|
mixins: [{ foo: 'mixin' }],
|
|
|
|
extends: { foo: 'extends' },
|
|
|
|
foo: 'local',
|
|
|
|
beforeCreate() {
|
|
|
|
merged = this.$options.foo
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const app = createApp(App)
|
|
|
|
app.mixin({
|
|
|
|
foo: 'global'
|
|
|
|
})
|
|
|
|
app.config.optionMergeStrategies.foo = (a, b) => (a ? `${a},` : ``) + b
|
|
|
|
|
|
|
|
app.mount(nodeOps.createElement('div'))
|
|
|
|
expect(merged!).toBe('global,extends,mixin,local')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('config.globalProperties', () => {
|
|
|
|
const app = createApp({
|
|
|
|
render() {
|
|
|
|
return this.foo
|
|
|
|
}
|
|
|
|
})
|
|
|
|
app.config.globalProperties.foo = 'hello'
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('hello')
|
|
|
|
})
|
2019-09-04 06:11:04 +08:00
|
|
|
})
|