apiCreateApp.spec.ts 9.8 KB

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