| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- import {
- type Plugin,
- createApp,
- defineComponent,
- getCurrentInstance,
- h,
- inject,
- nextTick,
- nodeOps,
- onMounted,
- provide,
- ref,
- resolveComponent,
- resolveDirective,
- serializeInner,
- watch,
- withDirectives,
- } from '@vue/runtime-test'
- describe('api: createApp', () => {
- test('mount', () => {
- const Comp = defineComponent({
- props: {
- count: {
- default: 0,
- },
- },
- setup(props) {
- return () => props.count
- },
- })
- const root1 = nodeOps.createElement('div')
- createApp(Comp).mount(root1)
- expect(serializeInner(root1)).toBe(`0`)
- //#5571 mount multiple apps to the same host element
- createApp(Comp).mount(root1)
- expect(
- `There is already an app instance mounted on the host container`,
- ).toHaveBeenWarned()
- // mount with props
- const root2 = nodeOps.createElement('div')
- const app2 = createApp(Comp, { count: 1 })
- app2.mount(root2)
- expect(serializeInner(root2)).toBe(`1`)
- // remount warning
- const root3 = nodeOps.createElement('div')
- app2.mount(root3)
- expect(serializeInner(root3)).toBe(``)
- expect(`already been mounted`).toHaveBeenWarned()
- })
- test('unmount', () => {
- const Comp = defineComponent({
- props: {
- count: {
- default: 0,
- },
- },
- setup(props) {
- return () => props.count
- },
- })
- const root = nodeOps.createElement('div')
- const app = createApp(Comp)
- // warning
- app.unmount()
- expect(`that is not mounted`).toHaveBeenWarned()
- app.mount(root)
- app.unmount()
- expect(serializeInner(root)).toBe(``)
- })
- test('provide', () => {
- const Root = {
- setup() {
- // test override
- provide('foo', 3)
- return () => h(Child)
- },
- }
- const Child = {
- setup() {
- const foo = inject('foo')
- const bar = inject('bar')
- try {
- inject('__proto__')
- } catch (e: any) {}
- return () => `${foo},${bar}`
- },
- }
- const app = createApp(Root)
- app.provide('foo', 1)
- app.provide('bar', 2)
- const root = nodeOps.createElement('div')
- app.mount(root)
- expect(serializeInner(root)).toBe(`3,2`)
- expect('[Vue warn]: injection "__proto__" not found.').toHaveBeenWarned()
- const app2 = createApp(Root)
- app2.provide('bar', 1)
- app2.provide('bar', 2)
- expect(`App already provides property with key "bar".`).toHaveBeenWarned()
- })
- test('runWithContext', () => {
- const app = createApp({
- setup() {
- provide('foo', 'should not be seen')
- // nested createApp
- const childApp = createApp({
- setup() {
- provide('foo', 'foo from child')
- },
- })
- childApp.provide('foo', 2)
- expect(childApp.runWithContext(() => inject('foo'))).toBe(2)
- return () => h('div')
- },
- })
- app.provide('foo', 1)
- expect(app.runWithContext(() => inject('foo'))).toBe(1)
- const root = nodeOps.createElement('div')
- app.mount(root)
- expect(
- app.runWithContext(() => {
- app.runWithContext(() => {})
- return inject('foo')
- }),
- ).toBe(1)
- // ensure the context is restored
- inject('foo')
- expect('inject() can only be used inside setup').toHaveBeenWarned()
- })
- test('component', () => {
- const Root = {
- // local override
- components: {
- BarBaz: () => 'barbaz-local!',
- },
- setup() {
- // resolve in setup
- const FooBar = resolveComponent('foo-bar')
- return () => {
- // resolve in render
- const BarBaz = resolveComponent('bar-baz')
- return h('div', [h(FooBar), h(BarBaz)])
- }
- },
- }
- 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)
- const root = nodeOps.createElement('div')
- app.mount(root)
- expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
- })
- test('directive', () => {
- const spy1 = vi.fn()
- const spy2 = vi.fn()
- const spy3 = vi.fn()
- const Root = {
- // local override
- directives: {
- BarBaz: { mounted: spy3 },
- },
- setup() {
- // resolve in setup
- const FooBar = resolveDirective('foo-bar')
- return () => {
- // resolve in render
- const BarBaz = resolveDirective('bar-baz')
- return withDirectives(h('div'), [[FooBar], [BarBaz]])
- }
- },
- }
- 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)
- const root = nodeOps.createElement('div')
- app.mount(root)
- expect(spy1).toHaveBeenCalled()
- expect(spy2).not.toHaveBeenCalled()
- expect(spy3).toHaveBeenCalled()
- app.directive('bind', FooBar)
- expect(
- `Do not use built-in directive ids as custom directive id: bind`,
- ).toHaveBeenWarned()
- })
- 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 = {
- name: 'mixinB',
- 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}`
- },
- }
- const app = createApp(Comp)
- app.mixin(mixinA)
- app.mixin(mixinB)
- 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)
- const root = nodeOps.createElement('div')
- app.mount(root)
- expect(serializeInner(root)).toBe(`123`)
- expect(calls).toEqual([
- 'mixinA created',
- 'mixinB created',
- 'comp created',
- 'mixinA mounted',
- 'mixinB mounted',
- 'comp mounted',
- ])
- })
- test('use', () => {
- const PluginA: Plugin = app => app.provide('foo', 1)
- const PluginB: Plugin = {
- install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2),
- }
- class PluginC {
- someProperty = {}
- static install() {
- app.provide('baz', 2)
- }
- }
- const PluginD: any = undefined
- const Root = {
- setup() {
- const foo = inject('foo')
- const bar = inject('bar')
- return () => `${foo},${bar}`
- },
- }
- const app = createApp(Root)
- app.use(PluginA)
- app.use(PluginB, 1, 1)
- app.use(PluginC)
- const root = nodeOps.createElement('div')
- app.mount(root)
- expect(serializeInner(root)).toBe(`1,2`)
- app.use(PluginA)
- expect(
- `Plugin has already been applied to target app`,
- ).toHaveBeenWarnedTimes(1)
- app.use(PluginD)
- expect(
- `A plugin must either be a function or an object with an "install" ` +
- `function.`,
- ).toHaveBeenWarnedTimes(1)
- })
- test('onUnmount', () => {
- const cleanup = vi.fn().mockName('plugin cleanup')
- const PluginA: Plugin = app => {
- app.provide('foo', 1)
- app.onUnmount(cleanup)
- }
- const PluginB: Plugin = {
- install: (app, arg1, arg2) => {
- app.provide('bar', arg1 + arg2)
- app.onUnmount(cleanup)
- },
- }
- const app = createApp({
- render: () => `Test`,
- })
- app.use(PluginA)
- app.use(PluginB)
- const root = nodeOps.createElement('div')
- app.mount(root)
- //also can be added after mount
- app.onUnmount(cleanup)
- app.unmount()
- expect(cleanup).toHaveBeenCalledTimes(3)
- })
- test('config.errorHandler', () => {
- const error = new Error()
- const count = ref(0)
- const handler = vi.fn((err, instance, info) => {
- expect(err).toBe(error)
- expect(instance.count).toBe(count.value)
- expect(info).toBe(`render function`)
- })
- const Root = {
- setup() {
- const count = ref(0)
- return {
- count,
- }
- },
- render() {
- throw error
- },
- }
- const app = createApp(Root)
- app.config.errorHandler = handler
- app.mount(nodeOps.createElement('div'))
- expect(handler).toHaveBeenCalled()
- })
- test('config.warnHandler', () => {
- let ctx: any
- const handler = vi.fn((msg, instance, trace) => {
- expect(msg).toMatch(`Component is missing template or render function`)
- expect(instance).toBe(ctx.proxy)
- expect(trace).toMatch(`Hello`)
- })
- const Root = {
- name: 'Hello',
- setup() {
- ctx = getCurrentInstance()
- },
- }
- const app = createApp(Root)
- app.config.warnHandler = handler
- app.mount(nodeOps.createElement('div'))
- expect(handler).toHaveBeenCalledTimes(1)
- })
- describe('config.isNativeTag', () => {
- const isNativeTag = vi.fn(tag => tag === 'div')
- test('Component.name', () => {
- const Root = {
- name: 'div',
- render() {
- return null
- },
- }
- const app = createApp(Root)
- Object.defineProperty(app.config, 'isNativeTag', {
- value: isNativeTag,
- writable: false,
- })
- app.mount(nodeOps.createElement('div'))
- expect(
- `Do not use built-in or reserved HTML elements as component id: div`,
- ).toHaveBeenWarned()
- })
- test('Component.components', () => {
- const Root = {
- components: {
- div: () => 'div',
- },
- render() {
- return null
- },
- }
- const app = createApp(Root)
- Object.defineProperty(app.config, 'isNativeTag', {
- value: isNativeTag,
- writable: false,
- })
- app.mount(nodeOps.createElement('div'))
- expect(
- `Do not use built-in or reserved HTML elements as component id: div`,
- ).toHaveBeenWarned()
- })
- test('Component.directives', () => {
- const Root = {
- directives: {
- bind: () => {},
- },
- render() {
- return null
- },
- }
- const app = createApp(Root)
- app.mount(nodeOps.createElement('div'))
- expect(
- `Do not use built-in directive ids as custom directive id: bind`,
- ).toHaveBeenWarned()
- })
- test('register using app.component', () => {
- const app = createApp({
- render() {},
- })
- Object.defineProperty(app.config, 'isNativeTag', {
- value: isNativeTag,
- writable: false,
- })
- app.component('div', () => 'div')
- app.mount(nodeOps.createElement('div'))
- expect(
- `Do not use built-in or reserved HTML elements as component id: div`,
- ).toHaveBeenWarned()
- })
- })
- 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')
- })
- test('config.throwUnhandledErrorInProduction', () => {
- __DEV__ = false
- try {
- const err = new Error()
- const app = createApp({
- setup() {
- throw err
- },
- })
- app.config.throwUnhandledErrorInProduction = true
- const root = nodeOps.createElement('div')
- expect(() => app.mount(root)).toThrow(err)
- } finally {
- __DEV__ = true
- }
- })
- test('return property "_" should not overwrite "ctx._", __isScriptSetup: false', () => {
- const Comp = defineComponent({
- setup() {
- return {
- _: ref(0), // return property "_" should not overwrite "ctx._"
- }
- },
- render() {
- return h('input', {
- ref: 'input',
- })
- },
- })
- const root1 = nodeOps.createElement('div')
- createApp(Comp).mount(root1)
- expect(
- `setup() return property "_" should not start with "$" or "_" which are reserved prefixes for Vue internals.`,
- ).toHaveBeenWarned()
- })
- test('return property "_" should not overwrite "ctx._", __isScriptSetup: true', () => {
- const Comp = defineComponent({
- setup() {
- return {
- _: ref(0), // return property "_" should not overwrite "ctx._"
- __isScriptSetup: true, // mock __isScriptSetup = true
- }
- },
- render() {
- return h('input', {
- ref: 'input',
- })
- },
- })
- const root1 = nodeOps.createElement('div')
- const app = createApp(Comp).mount(root1)
- // trigger
- app.$refs.input
- expect(
- `TypeError: Cannot read property '__isScriptSetup' of undefined`,
- ).not.toHaveBeenWarned()
- })
- // #10005
- test('flush order edge case on nested createApp', async () => {
- const order: string[] = []
- const App = defineComponent({
- setup(props) {
- const message = ref('m1')
- watch(
- message,
- () => {
- order.push('post watcher')
- },
- { flush: 'post' },
- )
- onMounted(() => {
- message.value = 'm2'
- createApp(() => '').mount(nodeOps.createElement('div'))
- })
- return () => {
- order.push('render')
- return h('div', [message.value])
- }
- },
- })
- createApp(App).mount(nodeOps.createElement('div'))
- await nextTick()
- expect(order).toMatchObject(['render', 'render', 'post watcher'])
- })
- // #14215
- test("unmount new app should not trigger other app's watcher", async () => {
- const compWatcherTriggerFn = vi.fn()
- const data = ref(true)
- const foo = ref('')
- const createNewApp = () => {
- const app = createApp({ render: () => h('new app') })
- const wrapper = nodeOps.createElement('div')
- app.mount(wrapper)
- return function destroy() {
- app.unmount()
- }
- }
- const Comp = defineComponent({
- setup() {
- watch(() => foo.value, compWatcherTriggerFn)
- return () => h('div', 'comp')
- },
- })
- const App = defineComponent({
- setup() {
- return () => (data.value ? h(Comp) : null)
- },
- })
- createApp(App).mount(nodeOps.createElement('div'))
- await nextTick()
- data.value = false
- const destroy = createNewApp()
- foo.value = 'bar'
- destroy()
- await nextTick()
- expect(compWatcherTriggerFn).toBeCalledTimes(0)
- })
- // config.compilerOptions is tested in packages/vue since it is only
- // supported in the full build.
- })
|