apiApp.spec.ts 9.4 KB

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