apiCreateApp.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. // mount with props
  32. const root2 = nodeOps.createElement('div')
  33. const app2 = createApp(Comp, { count: 1 })
  34. app2.mount(root2)
  35. expect(serializeInner(root2)).toBe(`1`)
  36. // remount warning
  37. const root3 = nodeOps.createElement('div')
  38. app2.mount(root3)
  39. expect(serializeInner(root3)).toBe(``)
  40. expect(`already been mounted`).toHaveBeenWarned()
  41. })
  42. test('unmount', () => {
  43. const Comp = defineComponent({
  44. props: {
  45. count: {
  46. default: 0
  47. }
  48. },
  49. setup(props) {
  50. return () => props.count
  51. }
  52. })
  53. const root = nodeOps.createElement('div')
  54. const app = createApp(Comp)
  55. app.mount(root)
  56. app.unmount(root)
  57. expect(serializeInner(root)).toBe(``)
  58. })
  59. test('provide', () => {
  60. const Root = {
  61. setup() {
  62. // test override
  63. provide('foo', 3)
  64. return () => h(Child)
  65. }
  66. }
  67. const Child = {
  68. setup() {
  69. const foo = inject('foo')
  70. const bar = inject('bar')
  71. try {
  72. inject('__proto__')
  73. } catch (e) {}
  74. return () => `${foo},${bar}`
  75. }
  76. }
  77. const app = createApp(Root)
  78. app.provide('foo', 1)
  79. app.provide('bar', 2)
  80. const root = nodeOps.createElement('div')
  81. app.mount(root)
  82. expect(serializeInner(root)).toBe(`3,2`)
  83. expect('[Vue warn]: injection "__proto__" not found.').toHaveBeenWarned()
  84. })
  85. test('component', () => {
  86. const Root = {
  87. // local override
  88. components: {
  89. BarBaz: () => 'barbaz-local!'
  90. },
  91. setup() {
  92. // resolve in setup
  93. const FooBar = resolveComponent('foo-bar') as any
  94. return () => {
  95. // resolve in render
  96. const BarBaz = resolveComponent('bar-baz') as any
  97. return h('div', [h(FooBar), h(BarBaz)])
  98. }
  99. }
  100. }
  101. const app = createApp(Root)
  102. const FooBar = () => 'foobar!'
  103. app.component('FooBar', FooBar)
  104. expect(app.component('FooBar')).toBe(FooBar)
  105. app.component('BarBaz', () => 'barbaz!')
  106. app.component('BarBaz', () => 'barbaz!')
  107. expect(
  108. 'Component "BarBaz" has already been registered in target app.'
  109. ).toHaveBeenWarnedTimes(1)
  110. const root = nodeOps.createElement('div')
  111. app.mount(root)
  112. expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
  113. })
  114. test('directive', () => {
  115. const spy1 = jest.fn()
  116. const spy2 = jest.fn()
  117. const spy3 = jest.fn()
  118. const Root = {
  119. // local override
  120. directives: {
  121. BarBaz: { mounted: spy3 }
  122. },
  123. setup() {
  124. // resolve in setup
  125. const FooBar = resolveDirective('foo-bar')!
  126. return () => {
  127. // resolve in render
  128. const BarBaz = resolveDirective('bar-baz')!
  129. return withDirectives(h('div'), [[FooBar], [BarBaz]])
  130. }
  131. }
  132. }
  133. const app = createApp(Root)
  134. const FooBar = { mounted: spy1 }
  135. app.directive('FooBar', FooBar)
  136. expect(app.directive('FooBar')).toBe(FooBar)
  137. app.directive('BarBaz', {
  138. mounted: spy2
  139. })
  140. app.directive('BarBaz', {
  141. mounted: spy2
  142. })
  143. expect(
  144. 'Directive "BarBaz" has already been registered in target app.'
  145. ).toHaveBeenWarnedTimes(1)
  146. const root = nodeOps.createElement('div')
  147. app.mount(root)
  148. expect(spy1).toHaveBeenCalled()
  149. expect(spy2).not.toHaveBeenCalled()
  150. expect(spy3).toHaveBeenCalled()
  151. app.directive('bind', FooBar)
  152. expect(
  153. `Do not use built-in directive ids as custom directive id: bind`
  154. ).toHaveBeenWarned()
  155. })
  156. test('mixin', () => {
  157. const calls: string[] = []
  158. const mixinA = {
  159. data() {
  160. return {
  161. a: 1
  162. }
  163. },
  164. created(this: any) {
  165. calls.push('mixinA created')
  166. expect(this.a).toBe(1)
  167. expect(this.b).toBe(2)
  168. expect(this.c).toBe(3)
  169. },
  170. mounted() {
  171. calls.push('mixinA mounted')
  172. }
  173. }
  174. const mixinB = {
  175. name: 'mixinB',
  176. data() {
  177. return {
  178. b: 2
  179. }
  180. },
  181. created(this: any) {
  182. calls.push('mixinB created')
  183. expect(this.a).toBe(1)
  184. expect(this.b).toBe(2)
  185. expect(this.c).toBe(3)
  186. },
  187. mounted() {
  188. calls.push('mixinB mounted')
  189. }
  190. }
  191. const Comp = {
  192. data() {
  193. return {
  194. c: 3
  195. }
  196. },
  197. created(this: any) {
  198. calls.push('comp created')
  199. expect(this.a).toBe(1)
  200. expect(this.b).toBe(2)
  201. expect(this.c).toBe(3)
  202. },
  203. mounted() {
  204. calls.push('comp mounted')
  205. },
  206. render(this: any) {
  207. return `${this.a}${this.b}${this.c}`
  208. }
  209. }
  210. const app = createApp(Comp)
  211. app.mixin(mixinA)
  212. app.mixin(mixinB)
  213. app.mixin(mixinA)
  214. app.mixin(mixinB)
  215. expect(
  216. 'Mixin has already been applied to target app'
  217. ).toHaveBeenWarnedTimes(2)
  218. expect(
  219. 'Mixin has already been applied to target app: mixinB'
  220. ).toHaveBeenWarnedTimes(1)
  221. const root = nodeOps.createElement('div')
  222. app.mount(root)
  223. expect(serializeInner(root)).toBe(`123`)
  224. expect(calls).toEqual([
  225. 'mixinA created',
  226. 'mixinB created',
  227. 'comp created',
  228. 'mixinA mounted',
  229. 'mixinB mounted',
  230. 'comp mounted'
  231. ])
  232. })
  233. test('use', () => {
  234. const PluginA: Plugin = app => app.provide('foo', 1)
  235. const PluginB: Plugin = {
  236. install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
  237. }
  238. class PluginC {
  239. someProperty = {}
  240. static install() {
  241. app.provide('baz', 2)
  242. }
  243. }
  244. const PluginD: any = undefined
  245. const Root = {
  246. setup() {
  247. const foo = inject('foo')
  248. const bar = inject('bar')
  249. return () => `${foo},${bar}`
  250. }
  251. }
  252. const app = createApp(Root)
  253. app.use(PluginA)
  254. app.use(PluginB, 1, 1)
  255. app.use(PluginC)
  256. const root = nodeOps.createElement('div')
  257. app.mount(root)
  258. expect(serializeInner(root)).toBe(`1,2`)
  259. app.use(PluginA)
  260. expect(
  261. `Plugin has already been applied to target app`
  262. ).toHaveBeenWarnedTimes(1)
  263. app.use(PluginD)
  264. expect(
  265. `A plugin must either be a function or an object with an "install" ` +
  266. `function.`
  267. ).toHaveBeenWarnedTimes(1)
  268. })
  269. test('config.errorHandler', () => {
  270. const error = new Error()
  271. const count = ref(0)
  272. const handler = jest.fn((err, instance, info) => {
  273. expect(err).toBe(error)
  274. expect((instance as any).count).toBe(count.value)
  275. expect(info).toBe(`render function`)
  276. })
  277. const Root = {
  278. setup() {
  279. const count = ref(0)
  280. return {
  281. count
  282. }
  283. },
  284. render() {
  285. throw error
  286. }
  287. }
  288. const app = createApp(Root)
  289. app.config.errorHandler = handler
  290. app.mount(nodeOps.createElement('div'))
  291. expect(handler).toHaveBeenCalled()
  292. })
  293. test('config.warnHandler', () => {
  294. let ctx: any
  295. const handler = jest.fn((msg, instance, trace) => {
  296. expect(msg).toMatch(`Component is missing template or render function`)
  297. expect(instance).toBe(ctx.proxy)
  298. expect(trace).toMatch(`Hello`)
  299. })
  300. const Root = {
  301. name: 'Hello',
  302. setup() {
  303. ctx = getCurrentInstance()
  304. }
  305. }
  306. const app = createApp(Root)
  307. app.config.warnHandler = handler
  308. app.mount(nodeOps.createElement('div'))
  309. expect(handler).toHaveBeenCalledTimes(1)
  310. })
  311. describe('config.isNativeTag', () => {
  312. const isNativeTag = jest.fn(tag => tag === 'div')
  313. test('Component.name', () => {
  314. const Root = {
  315. name: 'div',
  316. render() {
  317. return null
  318. }
  319. }
  320. const app = createApp(Root)
  321. Object.defineProperty(app.config, 'isNativeTag', {
  322. value: isNativeTag,
  323. writable: false
  324. })
  325. app.mount(nodeOps.createElement('div'))
  326. expect(
  327. `Do not use built-in or reserved HTML elements as component id: div`
  328. ).toHaveBeenWarned()
  329. })
  330. test('Component.components', () => {
  331. const Root = {
  332. components: {
  333. div: () => 'div'
  334. },
  335. render() {
  336. return null
  337. }
  338. }
  339. const app = createApp(Root)
  340. Object.defineProperty(app.config, 'isNativeTag', {
  341. value: isNativeTag,
  342. writable: false
  343. })
  344. app.mount(nodeOps.createElement('div'))
  345. expect(
  346. `Do not use built-in or reserved HTML elements as component id: div`
  347. ).toHaveBeenWarned()
  348. })
  349. test('Component.directives', () => {
  350. const Root = {
  351. directives: {
  352. bind: () => {}
  353. },
  354. render() {
  355. return null
  356. }
  357. }
  358. const app = createApp(Root)
  359. Object.defineProperty(app.config, 'isNativeTag', {
  360. value: isNativeTag,
  361. writable: false
  362. })
  363. app.mount(nodeOps.createElement('div'))
  364. expect(
  365. `Do not use built-in directive ids as custom directive id: bind`
  366. ).toHaveBeenWarned()
  367. })
  368. test('register using app.component', () => {
  369. const app = createApp({
  370. render() {}
  371. })
  372. Object.defineProperty(app.config, 'isNativeTag', {
  373. value: isNativeTag,
  374. writable: false
  375. })
  376. app.component('div', () => 'div')
  377. app.mount(nodeOps.createElement('div'))
  378. expect(
  379. `Do not use built-in or reserved HTML elements as component id: div`
  380. ).toHaveBeenWarned()
  381. })
  382. })
  383. test('config.optionMergeStrategies', () => {
  384. let merged: string
  385. const App = defineComponent({
  386. render() {},
  387. mixins: [{ foo: 'mixin' }],
  388. extends: { foo: 'extends' },
  389. foo: 'local',
  390. beforeCreate() {
  391. merged = this.$options.foo
  392. }
  393. })
  394. const app = createApp(App)
  395. app.mixin({
  396. foo: 'global'
  397. })
  398. app.config.optionMergeStrategies.foo = (a, b) => (a ? `${a},` : ``) + b
  399. app.mount(nodeOps.createElement('div'))
  400. expect(merged!).toBe('global,extends,mixin,local')
  401. })
  402. test('config.globalProperties', () => {
  403. const app = createApp({
  404. render() {
  405. return this.foo
  406. }
  407. })
  408. app.config.globalProperties.foo = 'hello'
  409. const root = nodeOps.createElement('div')
  410. app.mount(root)
  411. expect(serializeInner(root)).toBe('hello')
  412. })
  413. })