apiApp.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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('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()
  56. app.mount(Comp, root)
  57. app.unmount(root)
  58. expect(serializeInner(root)).toBe(``)
  59. })
  60. test('provide', () => {
  61. const app = createApp()
  62. app.provide('foo', 1)
  63. app.provide('bar', 2)
  64. const Root = {
  65. setup() {
  66. // test override
  67. provide('foo', 3)
  68. return () => h(Child)
  69. }
  70. }
  71. const Child = {
  72. setup() {
  73. const foo = inject('foo')
  74. const bar = inject('bar')
  75. return () => `${foo},${bar}`
  76. }
  77. }
  78. const root = nodeOps.createElement('div')
  79. app.mount(Root, root)
  80. expect(serializeInner(root)).toBe(`3,2`)
  81. })
  82. test('component', () => {
  83. const app = createApp()
  84. const FooBar = () => 'foobar!'
  85. app.component('FooBar', FooBar)
  86. expect(app.component('FooBar')).toBe(FooBar)
  87. app.component('BarBaz', () => 'barbaz!')
  88. app.component('BarBaz', () => 'barbaz!')
  89. expect(
  90. 'Component "BarBaz" has already been registered in target app.'
  91. ).toHaveBeenWarnedTimes(1)
  92. const Root = {
  93. // local override
  94. components: {
  95. BarBaz: () => 'barbaz-local!'
  96. },
  97. setup() {
  98. // resolve in setup
  99. const FooBar = resolveComponent('foo-bar') as any
  100. return () => {
  101. // resolve in render
  102. const BarBaz = resolveComponent('bar-baz') as any
  103. return h('div', [h(FooBar), h(BarBaz)])
  104. }
  105. }
  106. }
  107. const root = nodeOps.createElement('div')
  108. app.mount(Root, root)
  109. expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
  110. })
  111. test('directive', () => {
  112. const app = createApp()
  113. const spy1 = jest.fn()
  114. const spy2 = jest.fn()
  115. const spy3 = jest.fn()
  116. const FooBar = { mounted: spy1 }
  117. app.directive('FooBar', FooBar)
  118. expect(app.directive('FooBar')).toBe(FooBar)
  119. app.directive('BarBaz', {
  120. mounted: spy2
  121. })
  122. app.directive('BarBaz', {
  123. mounted: spy2
  124. })
  125. expect(
  126. 'Directive "BarBaz" has already been registered in target app.'
  127. ).toHaveBeenWarnedTimes(1)
  128. const Root = {
  129. // local override
  130. directives: {
  131. BarBaz: { mounted: spy3 }
  132. },
  133. setup() {
  134. // resolve in setup
  135. const FooBar = resolveDirective('foo-bar')!
  136. return () => {
  137. // resolve in render
  138. const BarBaz = resolveDirective('bar-baz')!
  139. return withDirectives(h('div'), [[FooBar], [BarBaz]])
  140. }
  141. }
  142. }
  143. const root = nodeOps.createElement('div')
  144. app.mount(Root, 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()
  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(Comp, 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 app = createApp()
  243. app.use(PluginA)
  244. app.use(PluginB, 1, 1)
  245. app.use(PluginC)
  246. const Root = {
  247. setup() {
  248. const foo = inject('foo')
  249. const bar = inject('bar')
  250. return () => `${foo},${bar}`
  251. }
  252. }
  253. const root = nodeOps.createElement('div')
  254. app.mount(Root, 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 app = createApp()
  268. const error = new Error()
  269. const count = ref(0)
  270. const handler = (app.config.errorHandler = jest.fn(
  271. (err, instance, info) => {
  272. expect(err).toBe(error)
  273. expect((instance as any).count).toBe(count.value)
  274. expect(info).toBe(`render function`)
  275. }
  276. ))
  277. const Root = {
  278. setup() {
  279. const count = ref(0)
  280. return {
  281. count
  282. }
  283. },
  284. render() {
  285. throw error
  286. }
  287. }
  288. app.mount(Root, nodeOps.createElement('div'))
  289. expect(handler).toHaveBeenCalled()
  290. })
  291. test('config.warnHandler', () => {
  292. const app = createApp()
  293. let ctx: any
  294. const handler = (app.config.warnHandler = jest.fn(
  295. (msg, instance, trace) => {
  296. expect(msg).toMatch(`Component is missing template or render function`)
  297. expect(instance).toBe(ctx.proxy)
  298. expect(trace).toMatch(`Hello`)
  299. }
  300. ))
  301. const Root = {
  302. name: 'Hello',
  303. setup() {
  304. ctx = getCurrentInstance()
  305. }
  306. }
  307. app.mount(Root, nodeOps.createElement('div'))
  308. expect(handler).toHaveBeenCalledTimes(1)
  309. })
  310. describe('config.isNativeTag', () => {
  311. const isNativeTag = jest.fn(tag => tag === 'div')
  312. test('Component.name', () => {
  313. const app = createApp()
  314. Object.defineProperty(app.config, 'isNativeTag', {
  315. value: isNativeTag,
  316. writable: false
  317. })
  318. const Root = {
  319. name: 'div',
  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.components', () => {
  335. const app = createApp()
  336. Object.defineProperty(app.config, 'isNativeTag', {
  337. value: isNativeTag,
  338. writable: false
  339. })
  340. const Root = {
  341. components: {
  342. div: () => 'div'
  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 or reserved HTML elements as component id: div`
  356. ).toHaveBeenWarned()
  357. })
  358. test('Component.directives', () => {
  359. const app = createApp()
  360. Object.defineProperty(app.config, 'isNativeTag', {
  361. value: isNativeTag,
  362. writable: false
  363. })
  364. const Root = {
  365. directives: {
  366. bind: () => {}
  367. },
  368. setup() {
  369. return {
  370. count: ref(0)
  371. }
  372. },
  373. render() {
  374. return null
  375. }
  376. }
  377. app.mount(Root, nodeOps.createElement('div'))
  378. expect(
  379. `Do not use built-in directive ids as custom directive id: bind`
  380. ).toHaveBeenWarned()
  381. })
  382. test('register using app.component', () => {
  383. const app = createApp()
  384. Object.defineProperty(app.config, 'isNativeTag', {
  385. value: isNativeTag,
  386. writable: false
  387. })
  388. const Root = {
  389. setup() {
  390. return {
  391. count: ref(0)
  392. }
  393. },
  394. render() {
  395. return null
  396. }
  397. }
  398. app.component('div', () => 'div')
  399. app.mount(Root, nodeOps.createElement('div'))
  400. expect(
  401. `Do not use built-in or reserved HTML elements as component id: div`
  402. ).toHaveBeenWarned()
  403. })
  404. })
  405. })