apiCreateApp.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. import {
  2. createApp,
  3. h,
  4. nodeOps,
  5. serializeInner,
  6. provide,
  7. inject,
  8. resolveComponent,
  9. resolveDirective,
  10. withDirectives,
  11. Plugin,
  12. ref,
  13. getCurrentInstance,
  14. defineComponent
  15. } from '@vue/runtime-test'
  16. describe('api: createApp', () => {
  17. test('mount', () => {
  18. const Comp = defineComponent({
  19. props: {
  20. count: {
  21. default: 0
  22. }
  23. },
  24. setup(props) {
  25. return () => props.count
  26. }
  27. })
  28. const root1 = nodeOps.createElement('div')
  29. createApp(Comp).mount(root1)
  30. expect(serializeInner(root1)).toBe(`0`)
  31. //#5571 mount multiple apps to the same host element
  32. createApp(Comp).mount(root1)
  33. expect(
  34. `There is already an app instance mounted on the host container`
  35. ).toHaveBeenWarned()
  36. // mount with props
  37. const root2 = nodeOps.createElement('div')
  38. const app2 = createApp(Comp, { count: 1 })
  39. app2.mount(root2)
  40. expect(serializeInner(root2)).toBe(`1`)
  41. // remount warning
  42. const root3 = nodeOps.createElement('div')
  43. app2.mount(root3)
  44. expect(serializeInner(root3)).toBe(``)
  45. expect(`already been mounted`).toHaveBeenWarned()
  46. })
  47. test('unmount', () => {
  48. const Comp = defineComponent({
  49. props: {
  50. count: {
  51. default: 0
  52. }
  53. },
  54. setup(props) {
  55. return () => props.count
  56. }
  57. })
  58. const root = nodeOps.createElement('div')
  59. const app = createApp(Comp)
  60. // warning
  61. app.unmount()
  62. expect(`that is not mounted`).toHaveBeenWarned()
  63. app.mount(root)
  64. app.unmount()
  65. expect(serializeInner(root)).toBe(``)
  66. })
  67. test('provide', () => {
  68. const Root = {
  69. setup() {
  70. // test override
  71. provide('foo', 3)
  72. return () => h(Child)
  73. }
  74. }
  75. const Child = {
  76. setup() {
  77. const foo = inject('foo')
  78. const bar = inject('bar')
  79. try {
  80. inject('__proto__')
  81. } catch (e: any) {}
  82. return () => `${foo},${bar}`
  83. }
  84. }
  85. const app = createApp(Root)
  86. app.provide('foo', 1)
  87. app.provide('bar', 2)
  88. const root = nodeOps.createElement('div')
  89. app.mount(root)
  90. expect(serializeInner(root)).toBe(`3,2`)
  91. expect('[Vue warn]: injection "__proto__" not found.').toHaveBeenWarned()
  92. const app2 = createApp(Root)
  93. app2.provide('bar', 1)
  94. app2.provide('bar', 2)
  95. expect(`App already provides property with key "bar".`).toHaveBeenWarned()
  96. })
  97. test('runWithContext', () => {
  98. const app = createApp({
  99. setup() {
  100. provide('foo', 'should not be seen')
  101. return () => h('div')
  102. }
  103. })
  104. app.provide('foo', 1)
  105. expect(app.runWithContext(() => inject('foo'))).toBe(1)
  106. // ensure the context is restored
  107. inject('foo')
  108. expect('inject() can only be used inside setup').toHaveBeenWarned()
  109. })
  110. test('component', () => {
  111. const Root = {
  112. // local override
  113. components: {
  114. BarBaz: () => 'barbaz-local!'
  115. },
  116. setup() {
  117. // resolve in setup
  118. const FooBar = resolveComponent('foo-bar') as any
  119. return () => {
  120. // resolve in render
  121. const BarBaz = resolveComponent('bar-baz') as any
  122. return h('div', [h(FooBar), h(BarBaz)])
  123. }
  124. }
  125. }
  126. const app = createApp(Root)
  127. const FooBar = () => 'foobar!'
  128. app.component('FooBar', FooBar)
  129. expect(app.component('FooBar')).toBe(FooBar)
  130. app.component('BarBaz', () => 'barbaz!')
  131. app.component('BarBaz', () => 'barbaz!')
  132. expect(
  133. 'Component "BarBaz" has already been registered in target app.'
  134. ).toHaveBeenWarnedTimes(1)
  135. const root = nodeOps.createElement('div')
  136. app.mount(root)
  137. expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
  138. })
  139. test('directive', () => {
  140. const spy1 = vi.fn()
  141. const spy2 = vi.fn()
  142. const spy3 = vi.fn()
  143. const Root = {
  144. // local override
  145. directives: {
  146. BarBaz: { mounted: spy3 }
  147. },
  148. setup() {
  149. // resolve in setup
  150. const FooBar = resolveDirective('foo-bar')!
  151. return () => {
  152. // resolve in render
  153. const BarBaz = resolveDirective('bar-baz')!
  154. return withDirectives(h('div'), [[FooBar], [BarBaz]])
  155. }
  156. }
  157. }
  158. const app = createApp(Root)
  159. const FooBar = { mounted: spy1 }
  160. app.directive('FooBar', FooBar)
  161. expect(app.directive('FooBar')).toBe(FooBar)
  162. app.directive('BarBaz', {
  163. mounted: spy2
  164. })
  165. app.directive('BarBaz', {
  166. mounted: spy2
  167. })
  168. expect(
  169. 'Directive "BarBaz" has already been registered in target app.'
  170. ).toHaveBeenWarnedTimes(1)
  171. const root = nodeOps.createElement('div')
  172. app.mount(root)
  173. expect(spy1).toHaveBeenCalled()
  174. expect(spy2).not.toHaveBeenCalled()
  175. expect(spy3).toHaveBeenCalled()
  176. app.directive('bind', FooBar)
  177. expect(
  178. `Do not use built-in directive ids as custom directive id: bind`
  179. ).toHaveBeenWarned()
  180. })
  181. test('mixin', () => {
  182. const calls: string[] = []
  183. const mixinA = {
  184. data() {
  185. return {
  186. a: 1
  187. }
  188. },
  189. created(this: any) {
  190. calls.push('mixinA created')
  191. expect(this.a).toBe(1)
  192. expect(this.b).toBe(2)
  193. expect(this.c).toBe(3)
  194. },
  195. mounted() {
  196. calls.push('mixinA mounted')
  197. }
  198. }
  199. const mixinB = {
  200. name: 'mixinB',
  201. data() {
  202. return {
  203. b: 2
  204. }
  205. },
  206. created(this: any) {
  207. calls.push('mixinB created')
  208. expect(this.a).toBe(1)
  209. expect(this.b).toBe(2)
  210. expect(this.c).toBe(3)
  211. },
  212. mounted() {
  213. calls.push('mixinB mounted')
  214. }
  215. }
  216. const Comp = {
  217. data() {
  218. return {
  219. c: 3
  220. }
  221. },
  222. created(this: any) {
  223. calls.push('comp created')
  224. expect(this.a).toBe(1)
  225. expect(this.b).toBe(2)
  226. expect(this.c).toBe(3)
  227. },
  228. mounted() {
  229. calls.push('comp mounted')
  230. },
  231. render(this: any) {
  232. return `${this.a}${this.b}${this.c}`
  233. }
  234. }
  235. const app = createApp(Comp)
  236. app.mixin(mixinA)
  237. app.mixin(mixinB)
  238. app.mixin(mixinA)
  239. app.mixin(mixinB)
  240. expect(
  241. 'Mixin has already been applied to target app'
  242. ).toHaveBeenWarnedTimes(2)
  243. expect(
  244. 'Mixin has already been applied to target app: mixinB'
  245. ).toHaveBeenWarnedTimes(1)
  246. const root = nodeOps.createElement('div')
  247. app.mount(root)
  248. expect(serializeInner(root)).toBe(`123`)
  249. expect(calls).toEqual([
  250. 'mixinA created',
  251. 'mixinB created',
  252. 'comp created',
  253. 'mixinA mounted',
  254. 'mixinB mounted',
  255. 'comp mounted'
  256. ])
  257. })
  258. test('use', () => {
  259. const PluginA: Plugin = app => app.provide('foo', 1)
  260. const PluginB: Plugin = {
  261. install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
  262. }
  263. class PluginC {
  264. someProperty = {}
  265. static install() {
  266. app.provide('baz', 2)
  267. }
  268. }
  269. const PluginD: any = undefined
  270. const Root = {
  271. setup() {
  272. const foo = inject('foo')
  273. const bar = inject('bar')
  274. return () => `${foo},${bar}`
  275. }
  276. }
  277. const app = createApp(Root)
  278. app.use(PluginA)
  279. app.use(PluginB, 1, 1)
  280. app.use(PluginC)
  281. const root = nodeOps.createElement('div')
  282. app.mount(root)
  283. expect(serializeInner(root)).toBe(`1,2`)
  284. app.use(PluginA)
  285. expect(
  286. `Plugin has already been applied to target app`
  287. ).toHaveBeenWarnedTimes(1)
  288. app.use(PluginD)
  289. expect(
  290. `A plugin must either be a function or an object with an "install" ` +
  291. `function.`
  292. ).toHaveBeenWarnedTimes(1)
  293. })
  294. test('config.errorHandler', () => {
  295. const error = new Error()
  296. const count = ref(0)
  297. const handler = vi.fn((err, instance, info) => {
  298. expect(err).toBe(error)
  299. expect((instance as any).count).toBe(count.value)
  300. expect(info).toBe(`render function`)
  301. })
  302. const Root = {
  303. setup() {
  304. const count = ref(0)
  305. return {
  306. count
  307. }
  308. },
  309. render() {
  310. throw error
  311. }
  312. }
  313. const app = createApp(Root)
  314. app.config.errorHandler = handler
  315. app.mount(nodeOps.createElement('div'))
  316. expect(handler).toHaveBeenCalled()
  317. })
  318. test('config.warnHandler', () => {
  319. let ctx: any
  320. const handler = vi.fn((msg, instance, trace) => {
  321. expect(msg).toMatch(`Component is missing template or render function`)
  322. expect(instance).toBe(ctx.proxy)
  323. expect(trace).toMatch(`Hello`)
  324. })
  325. const Root = {
  326. name: 'Hello',
  327. setup() {
  328. ctx = getCurrentInstance()
  329. }
  330. }
  331. const app = createApp(Root)
  332. app.config.warnHandler = handler
  333. app.mount(nodeOps.createElement('div'))
  334. expect(handler).toHaveBeenCalledTimes(1)
  335. })
  336. describe('config.isNativeTag', () => {
  337. const isNativeTag = vi.fn(tag => tag === 'div')
  338. test('Component.name', () => {
  339. const Root = {
  340. name: 'div',
  341. render() {
  342. return null
  343. }
  344. }
  345. const app = createApp(Root)
  346. Object.defineProperty(app.config, 'isNativeTag', {
  347. value: isNativeTag,
  348. writable: false
  349. })
  350. app.mount(nodeOps.createElement('div'))
  351. expect(
  352. `Do not use built-in or reserved HTML elements as component id: div`
  353. ).toHaveBeenWarned()
  354. })
  355. test('Component.components', () => {
  356. const Root = {
  357. components: {
  358. div: () => 'div'
  359. },
  360. render() {
  361. return null
  362. }
  363. }
  364. const app = createApp(Root)
  365. Object.defineProperty(app.config, 'isNativeTag', {
  366. value: isNativeTag,
  367. writable: false
  368. })
  369. app.mount(nodeOps.createElement('div'))
  370. expect(
  371. `Do not use built-in or reserved HTML elements as component id: div`
  372. ).toHaveBeenWarned()
  373. })
  374. test('Component.directives', () => {
  375. const Root = {
  376. directives: {
  377. bind: () => {}
  378. },
  379. render() {
  380. return null
  381. }
  382. }
  383. const app = createApp(Root)
  384. Object.defineProperty(app.config, 'isNativeTag', {
  385. value: isNativeTag,
  386. writable: false
  387. })
  388. app.mount(nodeOps.createElement('div'))
  389. expect(
  390. `Do not use built-in directive ids as custom directive id: bind`
  391. ).toHaveBeenWarned()
  392. })
  393. test('register using app.component', () => {
  394. const app = createApp({
  395. render() {}
  396. })
  397. Object.defineProperty(app.config, 'isNativeTag', {
  398. value: isNativeTag,
  399. writable: false
  400. })
  401. app.component('div', () => 'div')
  402. app.mount(nodeOps.createElement('div'))
  403. expect(
  404. `Do not use built-in or reserved HTML elements as component id: div`
  405. ).toHaveBeenWarned()
  406. })
  407. })
  408. test('config.optionMergeStrategies', () => {
  409. let merged: string
  410. const App = defineComponent({
  411. render() {},
  412. mixins: [{ foo: 'mixin' }],
  413. extends: { foo: 'extends' },
  414. foo: 'local',
  415. beforeCreate() {
  416. merged = this.$options.foo
  417. }
  418. })
  419. const app = createApp(App)
  420. app.mixin({
  421. foo: 'global'
  422. })
  423. app.config.optionMergeStrategies.foo = (a, b) => (a ? `${a},` : ``) + b
  424. app.mount(nodeOps.createElement('div'))
  425. expect(merged!).toBe('global,extends,mixin,local')
  426. })
  427. test('config.globalProperties', () => {
  428. const app = createApp({
  429. render() {
  430. return this.foo
  431. }
  432. })
  433. app.config.globalProperties.foo = 'hello'
  434. const root = nodeOps.createElement('div')
  435. app.mount(root)
  436. expect(serializeInner(root)).toBe('hello')
  437. })
  438. test('return property "_" should not overwrite "ctx._", __isScriptSetup: false', () => {
  439. const Comp = defineComponent({
  440. setup() {
  441. return {
  442. _: ref(0) // return property "_" should not overwrite "ctx._"
  443. }
  444. },
  445. render() {
  446. return h('input', {
  447. ref: 'input'
  448. })
  449. }
  450. })
  451. const root1 = nodeOps.createElement('div')
  452. createApp(Comp).mount(root1)
  453. expect(
  454. `setup() return property "_" should not start with "$" or "_" which are reserved prefixes for Vue internals.`
  455. ).toHaveBeenWarned()
  456. })
  457. test('return property "_" should not overwrite "ctx._", __isScriptSetup: true', () => {
  458. const Comp = defineComponent({
  459. setup() {
  460. return {
  461. _: ref(0), // return property "_" should not overwrite "ctx._"
  462. __isScriptSetup: true // mock __isScriptSetup = true
  463. }
  464. },
  465. render() {
  466. return h('input', {
  467. ref: 'input'
  468. })
  469. }
  470. })
  471. const root1 = nodeOps.createElement('div')
  472. const app = createApp(Comp).mount(root1)
  473. // trigger
  474. app.$refs.input
  475. expect(
  476. `TypeError: Cannot read property '__isScriptSetup' of undefined`
  477. ).not.toHaveBeenWarned()
  478. })
  479. // config.compilerOptions is tested in packages/vue since it is only
  480. // supported in the full build.
  481. })