apiCreateApp.spec.ts 9.7 KB

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