apiApp.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. } from '@vue/runtime-test'
  15. describe('api: createApp', () => {
  16. mockWarn()
  17. test('mount', () => {
  18. const Comp = {
  19. props: {
  20. count: {
  21. default: 0
  22. }
  23. },
  24. render() {
  25. return this.count
  26. }
  27. }
  28. const root1 = nodeOps.createElement('div')
  29. createApp().mount(Comp, root1)
  30. expect(serializeInner(root1)).toBe(`0`)
  31. // mount with props
  32. const root2 = nodeOps.createElement('div')
  33. const app2 = createApp()
  34. app2.mount(Comp, root2, { count: 1 })
  35. expect(serializeInner(root2)).toBe(`1`)
  36. // remount warning
  37. const root3 = nodeOps.createElement('div')
  38. app2.mount(Comp, root3)
  39. expect(serializeInner(root3)).toBe(``)
  40. expect(`already been mounted`).toHaveBeenWarned()
  41. })
  42. test('provide', () => {
  43. const app = createApp()
  44. app.provide('foo', 1)
  45. app.provide('bar', 2)
  46. const Root = {
  47. setup() {
  48. // test override
  49. provide('foo', 3)
  50. return () => h(Child)
  51. }
  52. }
  53. const Child = {
  54. setup() {
  55. const foo = inject('foo')
  56. const bar = inject('bar')
  57. return () => `${foo},${bar}`
  58. }
  59. }
  60. const root = nodeOps.createElement('div')
  61. app.mount(Root, root)
  62. expect(serializeInner(root)).toBe(`3,2`)
  63. })
  64. test('component', () => {
  65. const app = createApp()
  66. const FooBar = () => 'foobar!'
  67. app.component('FooBar', FooBar)
  68. expect(app.component('FooBar')).toBe(FooBar)
  69. app.component('BarBaz', () => 'barbaz!')
  70. const Root = {
  71. // local override
  72. components: {
  73. BarBaz: () => 'barbaz-local!'
  74. },
  75. setup() {
  76. // resolve in setup
  77. const FooBar = resolveComponent('foo-bar') as any
  78. return () => {
  79. // resolve in render
  80. const BarBaz = resolveComponent('bar-baz') as any
  81. return h('div', [h(FooBar), h(BarBaz)])
  82. }
  83. }
  84. }
  85. const root = nodeOps.createElement('div')
  86. app.mount(Root, root)
  87. expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
  88. })
  89. test('directive', () => {
  90. const app = createApp()
  91. const spy1 = jest.fn()
  92. const spy2 = jest.fn()
  93. const spy3 = jest.fn()
  94. const FooBar = { mounted: spy1 }
  95. app.directive('FooBar', FooBar)
  96. expect(app.directive('FooBar')).toBe(FooBar)
  97. app.directive('BarBaz', {
  98. mounted: spy2
  99. })
  100. const Root = {
  101. // local override
  102. directives: {
  103. BarBaz: { mounted: spy3 }
  104. },
  105. setup() {
  106. // resolve in setup
  107. const FooBar = resolveDirective('foo-bar') as any
  108. return () => {
  109. // resolve in render
  110. const BarBaz = resolveDirective('bar-baz') as any
  111. return applyDirectives(h('div'), [[FooBar], [BarBaz]])
  112. }
  113. }
  114. }
  115. const root = nodeOps.createElement('div')
  116. app.mount(Root, root)
  117. expect(spy1).toHaveBeenCalled()
  118. expect(spy2).not.toHaveBeenCalled()
  119. expect(spy3).toHaveBeenCalled()
  120. })
  121. test('use', () => {
  122. const PluginA: Plugin = app => app.provide('foo', 1)
  123. const PluginB: Plugin = {
  124. install: app => app.provide('bar', 2)
  125. }
  126. const app = createApp()
  127. app.use(PluginA)
  128. app.use(PluginB)
  129. const Root = {
  130. setup() {
  131. const foo = inject('foo')
  132. const bar = inject('bar')
  133. return () => `${foo},${bar}`
  134. }
  135. }
  136. const root = nodeOps.createElement('div')
  137. app.mount(Root, root)
  138. expect(serializeInner(root)).toBe(`1,2`)
  139. })
  140. test('config.errorHandler', () => {
  141. const app = createApp()
  142. const error = new Error()
  143. const count = ref(0)
  144. const handler = (app.config.errorHandler = jest.fn(
  145. (err, instance, info) => {
  146. expect(err).toBe(error)
  147. expect((instance as any).count).toBe(count.value)
  148. expect(info).toBe(`render function`)
  149. }
  150. ))
  151. const Root = {
  152. setup() {
  153. const count = ref(0)
  154. return {
  155. count
  156. }
  157. },
  158. render() {
  159. throw error
  160. }
  161. }
  162. app.mount(Root, nodeOps.createElement('div'))
  163. expect(handler).toHaveBeenCalled()
  164. })
  165. test('config.warnHandler', () => {
  166. const app = createApp()
  167. const handler = (app.config.warnHandler = jest.fn(
  168. (msg, instance, trace) => {}
  169. ))
  170. const Root = {
  171. setup() {}
  172. }
  173. app.mount(Root, nodeOps.createElement('div'))
  174. expect(handler).toHaveBeenCalled()
  175. })
  176. test.todo('mixin')
  177. })