apiApp.spec.ts 9.7 KB

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