apiApp.spec.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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, arg1, arg2) => app.provide('bar', arg1 + arg2)
  217. }
  218. const PluginC: any = undefined
  219. const app = createApp()
  220. app.use(PluginA)
  221. app.use(PluginB, 1, 1)
  222. const Root = {
  223. setup() {
  224. const foo = inject('foo')
  225. const bar = inject('bar')
  226. return () => `${foo},${bar}`
  227. }
  228. }
  229. const root = nodeOps.createElement('div')
  230. app.mount(Root, root)
  231. expect(serializeInner(root)).toBe(`1,2`)
  232. app.use(PluginA)
  233. expect(
  234. `Plugin has already been applied to target app`
  235. ).toHaveBeenWarnedTimes(1)
  236. app.use(PluginC)
  237. expect(
  238. `A plugin must either be a function or an object with an "install" ` +
  239. `function.`
  240. ).toHaveBeenWarnedTimes(1)
  241. })
  242. test('config.errorHandler', () => {
  243. const app = createApp()
  244. const error = new Error()
  245. const count = ref(0)
  246. const handler = (app.config.errorHandler = jest.fn(
  247. (err, instance, info) => {
  248. expect(err).toBe(error)
  249. expect((instance as any).count).toBe(count.value)
  250. expect(info).toBe(`render function`)
  251. }
  252. ))
  253. const Root = {
  254. setup() {
  255. const count = ref(0)
  256. return {
  257. count
  258. }
  259. },
  260. render() {
  261. throw error
  262. }
  263. }
  264. app.mount(Root, nodeOps.createElement('div'))
  265. expect(handler).toHaveBeenCalled()
  266. })
  267. test('config.warnHandler', () => {
  268. const app = createApp()
  269. let ctx: any
  270. const handler = (app.config.warnHandler = jest.fn(
  271. (msg, instance, trace) => {
  272. expect(msg).toMatch(`Component is missing template or render function`)
  273. expect(instance).toBe(ctx.proxy)
  274. expect(trace).toMatch(`Hello`)
  275. }
  276. ))
  277. const Root = {
  278. name: 'Hello',
  279. setup() {
  280. ctx = getCurrentInstance()
  281. }
  282. }
  283. app.mount(Root, nodeOps.createElement('div'))
  284. expect(handler).toHaveBeenCalledTimes(1)
  285. })
  286. describe('config.isNativeTag', () => {
  287. const isNativeTag = jest.fn(tag => tag === 'div')
  288. test('Component.name', () => {
  289. const app = createApp()
  290. Object.defineProperty(app.config, 'isNativeTag', {
  291. value: isNativeTag,
  292. writable: false
  293. })
  294. const Root = {
  295. name: 'div',
  296. setup() {
  297. return {
  298. count: ref(0)
  299. }
  300. },
  301. render() {
  302. return null
  303. }
  304. }
  305. app.mount(Root, nodeOps.createElement('div'))
  306. expect(
  307. `Do not use built-in or reserved HTML elements as component id: div`
  308. ).toHaveBeenWarned()
  309. })
  310. test('Component.components', () => {
  311. const app = createApp()
  312. Object.defineProperty(app.config, 'isNativeTag', {
  313. value: isNativeTag,
  314. writable: false
  315. })
  316. const Root = {
  317. components: {
  318. div: () => 'div'
  319. },
  320. setup() {
  321. return {
  322. count: ref(0)
  323. }
  324. },
  325. render() {
  326. return null
  327. }
  328. }
  329. app.mount(Root, nodeOps.createElement('div'))
  330. expect(
  331. `Do not use built-in or reserved HTML elements as component id: div`
  332. ).toHaveBeenWarned()
  333. })
  334. test('Component.directives', () => {
  335. const app = createApp()
  336. Object.defineProperty(app.config, 'isNativeTag', {
  337. value: isNativeTag,
  338. writable: false
  339. })
  340. const Root = {
  341. directives: {
  342. bind: () => {}
  343. },
  344. setup() {
  345. return {
  346. count: ref(0)
  347. }
  348. },
  349. render() {
  350. return null
  351. }
  352. }
  353. app.mount(Root, nodeOps.createElement('div'))
  354. expect(
  355. `Do not use built-in directive ids as custom directive id: bind`
  356. ).toHaveBeenWarned()
  357. })
  358. test('register using app.component', () => {
  359. const app = createApp()
  360. Object.defineProperty(app.config, 'isNativeTag', {
  361. value: isNativeTag,
  362. writable: false
  363. })
  364. const Root = {
  365. setup() {
  366. return {
  367. count: ref(0)
  368. }
  369. },
  370. render() {
  371. return null
  372. }
  373. }
  374. app.component('div', () => 'div')
  375. app.mount(Root, nodeOps.createElement('div'))
  376. expect(
  377. `Do not use built-in or reserved HTML elements as component id: div`
  378. ).toHaveBeenWarned()
  379. })
  380. })
  381. })