apiApp.spec.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. })
  232. test('config.errorHandler', () => {
  233. const app = createApp()
  234. const error = new Error()
  235. const count = ref(0)
  236. const handler = (app.config.errorHandler = jest.fn(
  237. (err, instance, info) => {
  238. expect(err).toBe(error)
  239. expect((instance as any).count).toBe(count.value)
  240. expect(info).toBe(`render function`)
  241. }
  242. ))
  243. const Root = {
  244. setup() {
  245. const count = ref(0)
  246. return {
  247. count
  248. }
  249. },
  250. render() {
  251. throw error
  252. }
  253. }
  254. app.mount(Root, nodeOps.createElement('div'))
  255. expect(handler).toHaveBeenCalled()
  256. })
  257. test('config.warnHandler', () => {
  258. const app = createApp()
  259. let ctx: any
  260. const handler = (app.config.warnHandler = jest.fn(
  261. (msg, instance, trace) => {
  262. expect(msg).toMatch(`Component is missing template or render function`)
  263. expect(instance).toBe(ctx.renderProxy)
  264. expect(trace).toMatch(`Hello`)
  265. }
  266. ))
  267. const Root = {
  268. name: 'Hello',
  269. setup() {
  270. ctx = getCurrentInstance()
  271. }
  272. }
  273. app.mount(Root, nodeOps.createElement('div'))
  274. expect(handler).toHaveBeenCalledTimes(1)
  275. })
  276. describe('config.isNativeTag', () => {
  277. const isNativeTag = jest.fn(tag => tag === 'div')
  278. test('Component.name', () => {
  279. const app = createApp()
  280. Object.defineProperty(app.config, 'isNativeTag', {
  281. value: isNativeTag,
  282. writable: false
  283. })
  284. const Root = {
  285. name: 'div',
  286. setup() {
  287. return {
  288. count: ref(0)
  289. }
  290. },
  291. render() {
  292. return null
  293. }
  294. }
  295. app.mount(Root, nodeOps.createElement('div'))
  296. expect(
  297. `Do not use built-in or reserved HTML elements as component id: div`
  298. ).toHaveBeenWarned()
  299. })
  300. test('Component.components', () => {
  301. const app = createApp()
  302. Object.defineProperty(app.config, 'isNativeTag', {
  303. value: isNativeTag,
  304. writable: false
  305. })
  306. const Root = {
  307. components: {
  308. div: () => 'div'
  309. },
  310. setup() {
  311. return {
  312. count: ref(0)
  313. }
  314. },
  315. render() {
  316. return null
  317. }
  318. }
  319. app.mount(Root, nodeOps.createElement('div'))
  320. expect(
  321. `Do not use built-in or reserved HTML elements as component id: div`
  322. ).toHaveBeenWarned()
  323. })
  324. test('Component.directives', () => {
  325. const app = createApp()
  326. Object.defineProperty(app.config, 'isNativeTag', {
  327. value: isNativeTag,
  328. writable: false
  329. })
  330. const Root = {
  331. directives: {
  332. bind: () => {}
  333. },
  334. setup() {
  335. return {
  336. count: ref(0)
  337. }
  338. },
  339. render() {
  340. return null
  341. }
  342. }
  343. app.mount(Root, nodeOps.createElement('div'))
  344. expect(
  345. `Do not use built-in directive ids as custom directive id: bind`
  346. ).toHaveBeenWarned()
  347. })
  348. test('register using app.component', () => {
  349. const app = createApp()
  350. Object.defineProperty(app.config, 'isNativeTag', {
  351. value: isNativeTag,
  352. writable: false
  353. })
  354. const Root = {
  355. setup() {
  356. return {
  357. count: ref(0)
  358. }
  359. },
  360. render() {
  361. return null
  362. }
  363. }
  364. app.component('div', () => 'div')
  365. app.mount(Root, nodeOps.createElement('div'))
  366. expect(
  367. `Do not use built-in or reserved HTML elements as component id: div`
  368. ).toHaveBeenWarned()
  369. })
  370. })
  371. })