apiCreateApp.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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('component', () => {
  98. const Root = {
  99. // local override
  100. components: {
  101. BarBaz: () => 'barbaz-local!'
  102. },
  103. setup() {
  104. // resolve in setup
  105. const FooBar = resolveComponent('foo-bar') as any
  106. return () => {
  107. // resolve in render
  108. const BarBaz = resolveComponent('bar-baz') as any
  109. return h('div', [h(FooBar), h(BarBaz)])
  110. }
  111. }
  112. }
  113. const app = createApp(Root)
  114. const FooBar = () => 'foobar!'
  115. app.component('FooBar', FooBar)
  116. expect(app.component('FooBar')).toBe(FooBar)
  117. app.component('BarBaz', () => 'barbaz!')
  118. app.component('BarBaz', () => 'barbaz!')
  119. expect(
  120. 'Component "BarBaz" has already been registered in target app.'
  121. ).toHaveBeenWarnedTimes(1)
  122. const root = nodeOps.createElement('div')
  123. app.mount(root)
  124. expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
  125. })
  126. test('directive', () => {
  127. const spy1 = jest.fn()
  128. const spy2 = jest.fn()
  129. const spy3 = jest.fn()
  130. const Root = {
  131. // local override
  132. directives: {
  133. BarBaz: { mounted: spy3 }
  134. },
  135. setup() {
  136. // resolve in setup
  137. const FooBar = resolveDirective('foo-bar')!
  138. return () => {
  139. // resolve in render
  140. const BarBaz = resolveDirective('bar-baz')!
  141. return withDirectives(h('div'), [[FooBar], [BarBaz]])
  142. }
  143. }
  144. }
  145. const app = createApp(Root)
  146. const FooBar = { mounted: spy1 }
  147. app.directive('FooBar', FooBar)
  148. expect(app.directive('FooBar')).toBe(FooBar)
  149. app.directive('BarBaz', {
  150. mounted: spy2
  151. })
  152. app.directive('BarBaz', {
  153. mounted: spy2
  154. })
  155. expect(
  156. 'Directive "BarBaz" has already been registered in target app.'
  157. ).toHaveBeenWarnedTimes(1)
  158. const root = nodeOps.createElement('div')
  159. app.mount(root)
  160. expect(spy1).toHaveBeenCalled()
  161. expect(spy2).not.toHaveBeenCalled()
  162. expect(spy3).toHaveBeenCalled()
  163. app.directive('bind', FooBar)
  164. expect(
  165. `Do not use built-in directive ids as custom directive id: bind`
  166. ).toHaveBeenWarned()
  167. })
  168. test('mixin', () => {
  169. const calls: string[] = []
  170. const mixinA = {
  171. data() {
  172. return {
  173. a: 1
  174. }
  175. },
  176. created(this: any) {
  177. calls.push('mixinA created')
  178. expect(this.a).toBe(1)
  179. expect(this.b).toBe(2)
  180. expect(this.c).toBe(3)
  181. },
  182. mounted() {
  183. calls.push('mixinA mounted')
  184. }
  185. }
  186. const mixinB = {
  187. name: 'mixinB',
  188. data() {
  189. return {
  190. b: 2
  191. }
  192. },
  193. created(this: any) {
  194. calls.push('mixinB created')
  195. expect(this.a).toBe(1)
  196. expect(this.b).toBe(2)
  197. expect(this.c).toBe(3)
  198. },
  199. mounted() {
  200. calls.push('mixinB mounted')
  201. }
  202. }
  203. const Comp = {
  204. data() {
  205. return {
  206. c: 3
  207. }
  208. },
  209. created(this: any) {
  210. calls.push('comp created')
  211. expect(this.a).toBe(1)
  212. expect(this.b).toBe(2)
  213. expect(this.c).toBe(3)
  214. },
  215. mounted() {
  216. calls.push('comp mounted')
  217. },
  218. render(this: any) {
  219. return `${this.a}${this.b}${this.c}`
  220. }
  221. }
  222. const app = createApp(Comp)
  223. app.mixin(mixinA)
  224. app.mixin(mixinB)
  225. app.mixin(mixinA)
  226. app.mixin(mixinB)
  227. expect(
  228. 'Mixin has already been applied to target app'
  229. ).toHaveBeenWarnedTimes(2)
  230. expect(
  231. 'Mixin has already been applied to target app: mixinB'
  232. ).toHaveBeenWarnedTimes(1)
  233. const root = nodeOps.createElement('div')
  234. app.mount(root)
  235. expect(serializeInner(root)).toBe(`123`)
  236. expect(calls).toEqual([
  237. 'mixinA created',
  238. 'mixinB created',
  239. 'comp created',
  240. 'mixinA mounted',
  241. 'mixinB mounted',
  242. 'comp mounted'
  243. ])
  244. })
  245. test('use', () => {
  246. const PluginA: Plugin = app => app.provide('foo', 1)
  247. const PluginB: Plugin = {
  248. install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
  249. }
  250. class PluginC {
  251. someProperty = {}
  252. static install() {
  253. app.provide('baz', 2)
  254. }
  255. }
  256. const PluginD: any = undefined
  257. const Root = {
  258. setup() {
  259. const foo = inject('foo')
  260. const bar = inject('bar')
  261. return () => `${foo},${bar}`
  262. }
  263. }
  264. const app = createApp(Root)
  265. app.use(PluginA)
  266. app.use(PluginB, 1, 1)
  267. app.use(PluginC)
  268. const root = nodeOps.createElement('div')
  269. app.mount(root)
  270. expect(serializeInner(root)).toBe(`1,2`)
  271. app.use(PluginA)
  272. expect(
  273. `Plugin has already been applied to target app`
  274. ).toHaveBeenWarnedTimes(1)
  275. app.use(PluginD)
  276. expect(
  277. `A plugin must either be a function or an object with an "install" ` +
  278. `function.`
  279. ).toHaveBeenWarnedTimes(1)
  280. })
  281. test('config.errorHandler', () => {
  282. const error = new Error()
  283. const count = ref(0)
  284. const handler = jest.fn((err, instance, info) => {
  285. expect(err).toBe(error)
  286. expect((instance as any).count).toBe(count.value)
  287. expect(info).toBe(`render function`)
  288. })
  289. const Root = {
  290. setup() {
  291. const count = ref(0)
  292. return {
  293. count
  294. }
  295. },
  296. render() {
  297. throw error
  298. }
  299. }
  300. const app = createApp(Root)
  301. app.config.errorHandler = handler
  302. app.mount(nodeOps.createElement('div'))
  303. expect(handler).toHaveBeenCalled()
  304. })
  305. test('config.warnHandler', () => {
  306. let ctx: any
  307. const handler = jest.fn((msg, instance, trace) => {
  308. expect(msg).toMatch(`Component is missing template or render function`)
  309. expect(instance).toBe(ctx.proxy)
  310. expect(trace).toMatch(`Hello`)
  311. })
  312. const Root = {
  313. name: 'Hello',
  314. setup() {
  315. ctx = getCurrentInstance()
  316. }
  317. }
  318. const app = createApp(Root)
  319. app.config.warnHandler = handler
  320. app.mount(nodeOps.createElement('div'))
  321. expect(handler).toHaveBeenCalledTimes(1)
  322. })
  323. describe('config.isNativeTag', () => {
  324. const isNativeTag = jest.fn(tag => tag === 'div')
  325. test('Component.name', () => {
  326. const Root = {
  327. name: 'div',
  328. render() {
  329. return null
  330. }
  331. }
  332. const app = createApp(Root)
  333. Object.defineProperty(app.config, 'isNativeTag', {
  334. value: isNativeTag,
  335. writable: false
  336. })
  337. app.mount(nodeOps.createElement('div'))
  338. expect(
  339. `Do not use built-in or reserved HTML elements as component id: div`
  340. ).toHaveBeenWarned()
  341. })
  342. test('Component.components', () => {
  343. const Root = {
  344. components: {
  345. div: () => 'div'
  346. },
  347. render() {
  348. return null
  349. }
  350. }
  351. const app = createApp(Root)
  352. Object.defineProperty(app.config, 'isNativeTag', {
  353. value: isNativeTag,
  354. writable: false
  355. })
  356. app.mount(nodeOps.createElement('div'))
  357. expect(
  358. `Do not use built-in or reserved HTML elements as component id: div`
  359. ).toHaveBeenWarned()
  360. })
  361. test('Component.directives', () => {
  362. const Root = {
  363. directives: {
  364. bind: () => {}
  365. },
  366. render() {
  367. return null
  368. }
  369. }
  370. const app = createApp(Root)
  371. Object.defineProperty(app.config, 'isNativeTag', {
  372. value: isNativeTag,
  373. writable: false
  374. })
  375. app.mount(nodeOps.createElement('div'))
  376. expect(
  377. `Do not use built-in directive ids as custom directive id: bind`
  378. ).toHaveBeenWarned()
  379. })
  380. test('register using app.component', () => {
  381. const app = createApp({
  382. render() {}
  383. })
  384. Object.defineProperty(app.config, 'isNativeTag', {
  385. value: isNativeTag,
  386. writable: false
  387. })
  388. app.component('div', () => 'div')
  389. app.mount(nodeOps.createElement('div'))
  390. expect(
  391. `Do not use built-in or reserved HTML elements as component id: div`
  392. ).toHaveBeenWarned()
  393. })
  394. })
  395. test('config.optionMergeStrategies', () => {
  396. let merged: string
  397. const App = defineComponent({
  398. render() {},
  399. mixins: [{ foo: 'mixin' }],
  400. extends: { foo: 'extends' },
  401. foo: 'local',
  402. beforeCreate() {
  403. merged = this.$options.foo
  404. }
  405. })
  406. const app = createApp(App)
  407. app.mixin({
  408. foo: 'global'
  409. })
  410. app.config.optionMergeStrategies.foo = (a, b) => (a ? `${a},` : ``) + b
  411. app.mount(nodeOps.createElement('div'))
  412. expect(merged!).toBe('global,extends,mixin,local')
  413. })
  414. test('config.globalProperties', () => {
  415. const app = createApp({
  416. render() {
  417. return this.foo
  418. }
  419. })
  420. app.config.globalProperties.foo = 'hello'
  421. const root = nodeOps.createElement('div')
  422. app.mount(root)
  423. expect(serializeInner(root)).toBe('hello')
  424. })
  425. test('return property "_" should not overwrite "ctx._", __isScriptSetup: false', () => {
  426. const Comp = defineComponent({
  427. setup() {
  428. return {
  429. _: ref(0) // return property "_" should not overwrite "ctx._"
  430. }
  431. },
  432. render() {
  433. return h('input', {
  434. ref: 'input'
  435. })
  436. }
  437. })
  438. const root1 = nodeOps.createElement('div')
  439. createApp(Comp).mount(root1)
  440. expect(
  441. `setup() return property "_" should not start with "$" or "_" which are reserved prefixes for Vue internals.`
  442. ).toHaveBeenWarned()
  443. })
  444. test('return property "_" should not overwrite "ctx._", __isScriptSetup: true', () => {
  445. const Comp = defineComponent({
  446. setup() {
  447. return {
  448. _: ref(0), // return property "_" should not overwrite "ctx._"
  449. __isScriptSetup: true // mock __isScriptSetup = true
  450. }
  451. },
  452. render() {
  453. return h('input', {
  454. ref: 'input'
  455. })
  456. }
  457. })
  458. const root1 = nodeOps.createElement('div')
  459. const app = createApp(Comp).mount(root1)
  460. // trigger
  461. app.$refs.input
  462. expect(
  463. `TypeError: Cannot read property '__isScriptSetup' of undefined`
  464. ).not.toHaveBeenWarned()
  465. })
  466. // config.compilerOptions is tested in packages/vue since it is only
  467. // supported in the full build.
  468. })