apiApp.spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import {
  2. createApp,
  3. h,
  4. nodeOps,
  5. serializeInner,
  6. mockWarn,
  7. provide,
  8. inject,
  9. resolveComponent,
  10. resolveDirective,
  11. applyDirectives,
  12. Plugin,
  13. ref,
  14. getCurrentInstance
  15. } from '@vue/runtime-test'
  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().mount(Comp, root1)
  31. expect(serializeInner(root1)).toBe(`0`)
  32. // mount with props
  33. const root2 = nodeOps.createElement('div')
  34. const app2 = createApp()
  35. app2.mount(Comp, root2, { count: 1 })
  36. expect(serializeInner(root2)).toBe(`1`)
  37. // remount warning
  38. const root3 = nodeOps.createElement('div')
  39. app2.mount(Comp, root3)
  40. expect(serializeInner(root3)).toBe(``)
  41. expect(`already been mounted`).toHaveBeenWarned()
  42. })
  43. test('provide', () => {
  44. const app = createApp()
  45. app.provide('foo', 1)
  46. app.provide('bar', 2)
  47. const Root = {
  48. setup() {
  49. // test override
  50. provide('foo', 3)
  51. return () => h(Child)
  52. }
  53. }
  54. const Child = {
  55. setup() {
  56. const foo = inject('foo')
  57. const bar = inject('bar')
  58. return () => `${foo},${bar}`
  59. }
  60. }
  61. const root = nodeOps.createElement('div')
  62. app.mount(Root, root)
  63. expect(serializeInner(root)).toBe(`3,2`)
  64. })
  65. test('component', () => {
  66. const app = createApp()
  67. const FooBar = () => 'foobar!'
  68. app.component('FooBar', FooBar)
  69. expect(app.component('FooBar')).toBe(FooBar)
  70. app.component('BarBaz', () => 'barbaz!')
  71. const Root = {
  72. // local override
  73. components: {
  74. BarBaz: () => 'barbaz-local!'
  75. },
  76. setup() {
  77. // resolve in setup
  78. const FooBar = resolveComponent('foo-bar') as any
  79. return () => {
  80. // resolve in render
  81. const BarBaz = resolveComponent('bar-baz') as any
  82. return h('div', [h(FooBar), h(BarBaz)])
  83. }
  84. }
  85. }
  86. const root = nodeOps.createElement('div')
  87. app.mount(Root, root)
  88. expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
  89. })
  90. test('directive', () => {
  91. const app = createApp()
  92. const spy1 = jest.fn()
  93. const spy2 = jest.fn()
  94. const spy3 = jest.fn()
  95. const FooBar = { mounted: spy1 }
  96. app.directive('FooBar', FooBar)
  97. expect(app.directive('FooBar')).toBe(FooBar)
  98. app.directive('BarBaz', {
  99. mounted: spy2
  100. })
  101. const Root = {
  102. // local override
  103. directives: {
  104. BarBaz: { mounted: spy3 }
  105. },
  106. setup() {
  107. // resolve in setup
  108. const FooBar = resolveDirective('foo-bar') as any
  109. return () => {
  110. // resolve in render
  111. const BarBaz = resolveDirective('bar-baz') as any
  112. return applyDirectives(h('div'), [[FooBar], [BarBaz]])
  113. }
  114. }
  115. }
  116. const root = nodeOps.createElement('div')
  117. app.mount(Root, root)
  118. expect(spy1).toHaveBeenCalled()
  119. expect(spy2).not.toHaveBeenCalled()
  120. expect(spy3).toHaveBeenCalled()
  121. })
  122. test('mixin', () => {
  123. const calls: string[] = []
  124. const mixinA = {
  125. data() {
  126. return {
  127. a: 1
  128. }
  129. },
  130. created(this: any) {
  131. calls.push('mixinA created')
  132. expect(this.a).toBe(1)
  133. expect(this.b).toBe(2)
  134. expect(this.c).toBe(3)
  135. },
  136. mounted() {
  137. calls.push('mixinA mounted')
  138. }
  139. }
  140. const mixinB = {
  141. data() {
  142. return {
  143. b: 2
  144. }
  145. },
  146. created(this: any) {
  147. calls.push('mixinB created')
  148. expect(this.a).toBe(1)
  149. expect(this.b).toBe(2)
  150. expect(this.c).toBe(3)
  151. },
  152. mounted() {
  153. calls.push('mixinB mounted')
  154. }
  155. }
  156. const Comp = {
  157. data() {
  158. return {
  159. c: 3
  160. }
  161. },
  162. created(this: any) {
  163. calls.push('comp created')
  164. expect(this.a).toBe(1)
  165. expect(this.b).toBe(2)
  166. expect(this.c).toBe(3)
  167. },
  168. mounted() {
  169. calls.push('comp mounted')
  170. },
  171. render(this: any) {
  172. return `${this.a}${this.b}${this.c}`
  173. }
  174. }
  175. const app = createApp()
  176. app.mixin(mixinA)
  177. app.mixin(mixinB)
  178. const root = nodeOps.createElement('div')
  179. app.mount(Comp, root)
  180. expect(serializeInner(root)).toBe(`123`)
  181. expect(calls).toEqual([
  182. 'mixinA created',
  183. 'mixinB created',
  184. 'comp created',
  185. 'mixinA mounted',
  186. 'mixinB mounted',
  187. 'comp mounted'
  188. ])
  189. })
  190. test('use', () => {
  191. const PluginA: Plugin = app => app.provide('foo', 1)
  192. const PluginB: Plugin = {
  193. install: app => app.provide('bar', 2)
  194. }
  195. const app = createApp()
  196. app.use(PluginA)
  197. app.use(PluginB)
  198. const Root = {
  199. setup() {
  200. const foo = inject('foo')
  201. const bar = inject('bar')
  202. return () => `${foo},${bar}`
  203. }
  204. }
  205. const root = nodeOps.createElement('div')
  206. app.mount(Root, root)
  207. expect(serializeInner(root)).toBe(`1,2`)
  208. })
  209. test('config.errorHandler', () => {
  210. const app = createApp()
  211. const error = new Error()
  212. const count = ref(0)
  213. const handler = (app.config.errorHandler = jest.fn(
  214. (err, instance, info) => {
  215. expect(err).toBe(error)
  216. expect((instance as any).count).toBe(count.value)
  217. expect(info).toBe(`render function`)
  218. }
  219. ))
  220. const Root = {
  221. setup() {
  222. const count = ref(0)
  223. return {
  224. count
  225. }
  226. },
  227. render() {
  228. throw error
  229. }
  230. }
  231. app.mount(Root, nodeOps.createElement('div'))
  232. expect(handler).toHaveBeenCalled()
  233. })
  234. test('config.warnHandler', () => {
  235. const app = createApp()
  236. let ctx: any
  237. const handler = (app.config.warnHandler = jest.fn(
  238. (msg, instance, trace) => {
  239. expect(msg).toMatch(`Component is missing render function`)
  240. expect(instance).toBe(ctx.renderProxy)
  241. expect(trace).toMatch(`Hello`)
  242. }
  243. ))
  244. const Root = {
  245. name: 'Hello',
  246. setup() {
  247. ctx = getCurrentInstance()
  248. }
  249. }
  250. app.mount(Root, nodeOps.createElement('div'))
  251. expect(handler).toHaveBeenCalledTimes(1)
  252. })
  253. })