apiOptions.spec.ts 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. import { vi } from 'vitest'
  2. import {
  3. h,
  4. nodeOps,
  5. render,
  6. serializeInner,
  7. triggerEvent,
  8. TestElement,
  9. nextTick,
  10. renderToString,
  11. ref,
  12. defineComponent,
  13. createApp,
  14. computed
  15. } from '@vue/runtime-test'
  16. import { render as domRender } from 'vue'
  17. describe('api: options', () => {
  18. test('data', async () => {
  19. const Comp = defineComponent({
  20. data() {
  21. return {
  22. foo: 1
  23. }
  24. },
  25. render() {
  26. return h(
  27. 'div',
  28. {
  29. onClick: () => {
  30. this.foo++
  31. }
  32. },
  33. this.foo
  34. )
  35. }
  36. })
  37. const root = nodeOps.createElement('div')
  38. render(h(Comp), root)
  39. expect(serializeInner(root)).toBe(`<div>1</div>`)
  40. triggerEvent(root.children[0] as TestElement, 'click')
  41. await nextTick()
  42. expect(serializeInner(root)).toBe(`<div>2</div>`)
  43. })
  44. test('computed', async () => {
  45. const Comp = defineComponent({
  46. data() {
  47. return {
  48. foo: 1
  49. }
  50. },
  51. computed: {
  52. bar(): number {
  53. return this.foo + 1
  54. },
  55. baz: (vm): number => vm.bar + 1
  56. },
  57. render() {
  58. return h(
  59. 'div',
  60. {
  61. onClick: () => {
  62. this.foo++
  63. }
  64. },
  65. this.bar + this.baz
  66. )
  67. }
  68. })
  69. const root = nodeOps.createElement('div')
  70. render(h(Comp), root)
  71. expect(serializeInner(root)).toBe(`<div>5</div>`)
  72. triggerEvent(root.children[0] as TestElement, 'click')
  73. await nextTick()
  74. expect(serializeInner(root)).toBe(`<div>7</div>`)
  75. })
  76. test('methods', async () => {
  77. const Comp = defineComponent({
  78. data() {
  79. // #3300 method on ctx should be overwritable
  80. this.incBy = this.incBy.bind(this, 2)
  81. return {
  82. foo: 1
  83. }
  84. },
  85. methods: {
  86. inc() {
  87. this.foo++
  88. },
  89. incBy(n = 0) {
  90. this.foo += n
  91. }
  92. },
  93. render() {
  94. return h(
  95. 'div',
  96. {
  97. onClick: this.inc,
  98. onFoo: this.incBy
  99. },
  100. this.foo
  101. )
  102. }
  103. })
  104. const root = nodeOps.createElement('div')
  105. render(h(Comp), root)
  106. expect(serializeInner(root)).toBe(`<div>1</div>`)
  107. triggerEvent(root.children[0] as TestElement, 'click')
  108. await nextTick()
  109. expect(serializeInner(root)).toBe(`<div>2</div>`)
  110. triggerEvent(root.children[0] as TestElement, 'foo')
  111. await nextTick()
  112. expect(serializeInner(root)).toBe(`<div>4</div>`)
  113. })
  114. test('component’s own methods have higher priority than global properties', async () => {
  115. const app = createApp({
  116. methods: {
  117. foo() {
  118. return 'foo'
  119. }
  120. },
  121. render() {
  122. return this.foo()
  123. }
  124. })
  125. app.config.globalProperties.foo = () => 'bar'
  126. const root = nodeOps.createElement('div')
  127. app.mount(root)
  128. expect(serializeInner(root)).toBe(`foo`)
  129. })
  130. test('watch', async () => {
  131. function returnThis(this: any) {
  132. return this
  133. }
  134. const spyA = vi.fn(returnThis)
  135. const spyB = vi.fn(returnThis)
  136. const spyC = vi.fn(returnThis)
  137. const spyD = vi.fn(returnThis)
  138. const spyE = vi.fn(returnThis)
  139. let ctx: any
  140. const Comp = {
  141. data() {
  142. return {
  143. foo: 1,
  144. bar: 2,
  145. baz: {
  146. qux: 3
  147. },
  148. qux: 4,
  149. dot: {
  150. path: 5
  151. }
  152. }
  153. },
  154. watch: {
  155. // string method name
  156. foo: 'onFooChange',
  157. // direct function
  158. bar: spyB,
  159. baz: {
  160. handler: spyC,
  161. deep: true
  162. },
  163. qux: {
  164. handler: 'onQuxChange'
  165. },
  166. 'dot.path': spyE
  167. },
  168. methods: {
  169. onFooChange: spyA,
  170. onQuxChange: spyD
  171. },
  172. render() {
  173. ctx = this
  174. }
  175. }
  176. const root = nodeOps.createElement('div')
  177. render(h(Comp), root)
  178. function assertCall(spy: vi.Mock, callIndex: number, args: any[]) {
  179. expect(spy.mock.calls[callIndex].slice(0, 2)).toMatchObject(args)
  180. expect(spy).toHaveReturnedWith(ctx)
  181. }
  182. ctx.foo++
  183. await nextTick()
  184. expect(spyA).toHaveBeenCalledTimes(1)
  185. assertCall(spyA, 0, [2, 1])
  186. ctx.bar++
  187. await nextTick()
  188. expect(spyB).toHaveBeenCalledTimes(1)
  189. assertCall(spyB, 0, [3, 2])
  190. ctx.baz.qux++
  191. await nextTick()
  192. expect(spyC).toHaveBeenCalledTimes(1)
  193. // new and old objects have same identity
  194. assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
  195. ctx.qux++
  196. await nextTick()
  197. expect(spyD).toHaveBeenCalledTimes(1)
  198. assertCall(spyD, 0, [5, 4])
  199. ctx.dot.path++
  200. await nextTick()
  201. expect(spyE).toHaveBeenCalledTimes(1)
  202. assertCall(spyE, 0, [6, 5])
  203. })
  204. test('watch array', async () => {
  205. function returnThis(this: any) {
  206. return this
  207. }
  208. const spyA = vi.fn(returnThis)
  209. const spyB = vi.fn(returnThis)
  210. const spyC = vi.fn(returnThis)
  211. let ctx: any
  212. const Comp = {
  213. data() {
  214. return {
  215. foo: 1,
  216. bar: 2,
  217. baz: {
  218. qux: 3
  219. }
  220. }
  221. },
  222. watch: {
  223. // string method name
  224. foo: ['onFooChange'],
  225. // direct function
  226. bar: [spyB],
  227. baz: [
  228. {
  229. handler: spyC,
  230. deep: true
  231. }
  232. ]
  233. },
  234. methods: {
  235. onFooChange: spyA
  236. },
  237. render() {
  238. ctx = this
  239. }
  240. }
  241. const root = nodeOps.createElement('div')
  242. render(h(Comp), root)
  243. function assertCall(spy: vi.Mock, callIndex: number, args: any[]) {
  244. expect(spy.mock.calls[callIndex].slice(0, 2)).toMatchObject(args)
  245. expect(spy).toHaveReturnedWith(ctx)
  246. }
  247. ctx.foo++
  248. await nextTick()
  249. expect(spyA).toHaveBeenCalledTimes(1)
  250. assertCall(spyA, 0, [2, 1])
  251. ctx.bar++
  252. await nextTick()
  253. expect(spyB).toHaveBeenCalledTimes(1)
  254. assertCall(spyB, 0, [3, 2])
  255. ctx.baz.qux++
  256. await nextTick()
  257. expect(spyC).toHaveBeenCalledTimes(1)
  258. // new and old objects have same identity
  259. assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
  260. })
  261. // #3966
  262. test('watch merging from mixins', async () => {
  263. const mixinA = {
  264. data() {
  265. return {
  266. fromMixinA: ''
  267. }
  268. },
  269. watch: {
  270. obj: {
  271. handler(this: any, to: any) {
  272. this.fromMixinA = to
  273. }
  274. }
  275. }
  276. }
  277. const mixinB = {
  278. data() {
  279. return {
  280. fromMixinB: ''
  281. }
  282. },
  283. watch: {
  284. obj: 'setMixinB'
  285. },
  286. methods: {
  287. setMixinB(this: any, to: any) {
  288. this.fromMixinB = to
  289. }
  290. }
  291. }
  292. let vm: any
  293. const Comp = {
  294. render() {},
  295. mixins: [mixinA, mixinB],
  296. data: () => ({
  297. obj: 'foo',
  298. fromComp: ''
  299. }),
  300. watch: {
  301. obj(this: any, to: any) {
  302. this.fromComp = to
  303. }
  304. },
  305. mounted() {
  306. vm = this
  307. }
  308. }
  309. const root = nodeOps.createElement('div')
  310. render(h(Comp), root)
  311. vm.obj = 'bar'
  312. await nextTick()
  313. expect(vm.fromComp).toBe('bar')
  314. expect(vm.fromMixinA).toBe('bar')
  315. expect(vm.fromMixinB).toBe('bar')
  316. })
  317. test('provide/inject', () => {
  318. const symbolKey = Symbol()
  319. const Root = defineComponent({
  320. data() {
  321. return {
  322. a: 1
  323. }
  324. },
  325. provide() {
  326. return {
  327. a: this.a,
  328. [symbolKey]: 2
  329. }
  330. },
  331. render() {
  332. return [
  333. h(ChildA),
  334. h(ChildB),
  335. h(ChildC),
  336. h(ChildD),
  337. h(ChildE),
  338. h(ChildF),
  339. h(ChildG),
  340. h(ChildH),
  341. h(ChildI),
  342. h(ChildJ)
  343. ]
  344. }
  345. })
  346. const defineChild = (injectOptions: any, injectedKey = 'b') =>
  347. ({
  348. inject: injectOptions,
  349. render() {
  350. return this[injectedKey]
  351. }
  352. } as any)
  353. const ChildA = defineChild(['a'], 'a')
  354. const ChildB = defineChild({ b: 'a' })
  355. const ChildC = defineChild({
  356. b: {
  357. from: 'a'
  358. }
  359. })
  360. const ChildD = defineChild(
  361. {
  362. a: {
  363. default: () => 0
  364. }
  365. },
  366. 'a'
  367. )
  368. const ChildE = defineChild({
  369. b: {
  370. from: 'c',
  371. default: 2
  372. }
  373. })
  374. const ChildF = defineChild({
  375. b: {
  376. from: 'c',
  377. default: () => 3
  378. }
  379. })
  380. const ChildG = defineChild({
  381. b: {
  382. default: 4
  383. }
  384. })
  385. const ChildH = defineChild({
  386. b: {
  387. default: () => 5
  388. }
  389. })
  390. const ChildI = defineChild({
  391. b: symbolKey
  392. })
  393. const ChildJ = defineChild({
  394. b: {
  395. from: symbolKey
  396. }
  397. })
  398. expect(renderToString(h(Root))).toBe(`1111234522`)
  399. })
  400. test('provide/inject refs', async () => {
  401. const n = ref(0)
  402. const np = computed(() => n.value + 1)
  403. const Parent = defineComponent({
  404. provide() {
  405. return {
  406. n,
  407. np
  408. }
  409. },
  410. render: () => h(Child)
  411. })
  412. const Child = defineComponent({
  413. inject: ['n', 'np'],
  414. render(this: any) {
  415. return this.n + this.np
  416. }
  417. })
  418. const app = createApp(Parent)
  419. // TODO remove in 3.3
  420. app.config.unwrapInjectedRef = true
  421. const root = nodeOps.createElement('div')
  422. app.mount(root)
  423. expect(serializeInner(root)).toBe(`1`)
  424. n.value++
  425. await nextTick()
  426. expect(serializeInner(root)).toBe(`3`)
  427. })
  428. // TODO remove in 3.3
  429. test('provide/inject refs (compat)', async () => {
  430. const n = ref(0)
  431. const np = computed(() => n.value + 1)
  432. const Parent = defineComponent({
  433. provide() {
  434. return {
  435. n,
  436. np
  437. }
  438. },
  439. render: () => h(Child)
  440. })
  441. const Child = defineComponent({
  442. inject: ['n', 'np'],
  443. render(this: any) {
  444. return this.n.value + this.np.value
  445. }
  446. })
  447. const app = createApp(Parent)
  448. const root = nodeOps.createElement('div')
  449. app.mount(root)
  450. expect(serializeInner(root)).toBe(`1`)
  451. n.value++
  452. await nextTick()
  453. expect(serializeInner(root)).toBe(`3`)
  454. expect(`injected property "n" is a ref`).toHaveBeenWarned()
  455. expect(`injected property "np" is a ref`).toHaveBeenWarned()
  456. })
  457. test('provide accessing data in extends', () => {
  458. const Base = defineComponent({
  459. data() {
  460. return {
  461. a: 1
  462. }
  463. },
  464. provide() {
  465. return {
  466. a: this.a
  467. }
  468. }
  469. })
  470. const Child = {
  471. inject: ['a'],
  472. render() {
  473. return (this as any).a
  474. }
  475. }
  476. const Root = defineComponent({
  477. extends: Base,
  478. render() {
  479. return h(Child)
  480. }
  481. })
  482. expect(renderToString(h(Root))).toBe(`1`)
  483. })
  484. test('lifecycle', async () => {
  485. const count = ref(0)
  486. const root = nodeOps.createElement('div')
  487. const calls: string[] = []
  488. const Root = {
  489. beforeCreate() {
  490. calls.push('root beforeCreate')
  491. },
  492. created() {
  493. calls.push('root created')
  494. },
  495. beforeMount() {
  496. calls.push('root onBeforeMount')
  497. },
  498. mounted() {
  499. calls.push('root onMounted')
  500. },
  501. beforeUpdate() {
  502. calls.push('root onBeforeUpdate')
  503. },
  504. updated() {
  505. calls.push('root onUpdated')
  506. },
  507. beforeUnmount() {
  508. calls.push('root onBeforeUnmount')
  509. },
  510. unmounted() {
  511. calls.push('root onUnmounted')
  512. },
  513. render() {
  514. return h(Mid, { count: count.value })
  515. }
  516. }
  517. const Mid = {
  518. beforeCreate() {
  519. calls.push('mid beforeCreate')
  520. },
  521. created() {
  522. calls.push('mid created')
  523. },
  524. beforeMount() {
  525. calls.push('mid onBeforeMount')
  526. },
  527. mounted() {
  528. calls.push('mid onMounted')
  529. },
  530. beforeUpdate() {
  531. calls.push('mid onBeforeUpdate')
  532. },
  533. updated() {
  534. calls.push('mid onUpdated')
  535. },
  536. beforeUnmount() {
  537. calls.push('mid onBeforeUnmount')
  538. },
  539. unmounted() {
  540. calls.push('mid onUnmounted')
  541. },
  542. render(this: any) {
  543. return h(Child, { count: this.$props.count })
  544. }
  545. }
  546. const Child = {
  547. beforeCreate() {
  548. calls.push('child beforeCreate')
  549. },
  550. created() {
  551. calls.push('child created')
  552. },
  553. beforeMount() {
  554. calls.push('child onBeforeMount')
  555. },
  556. mounted() {
  557. calls.push('child onMounted')
  558. },
  559. beforeUpdate() {
  560. calls.push('child onBeforeUpdate')
  561. },
  562. updated() {
  563. calls.push('child onUpdated')
  564. },
  565. beforeUnmount() {
  566. calls.push('child onBeforeUnmount')
  567. },
  568. unmounted() {
  569. calls.push('child onUnmounted')
  570. },
  571. render(this: any) {
  572. return h('div', this.$props.count)
  573. }
  574. }
  575. // mount
  576. render(h(Root), root)
  577. expect(calls).toEqual([
  578. 'root beforeCreate',
  579. 'root created',
  580. 'root onBeforeMount',
  581. 'mid beforeCreate',
  582. 'mid created',
  583. 'mid onBeforeMount',
  584. 'child beforeCreate',
  585. 'child created',
  586. 'child onBeforeMount',
  587. 'child onMounted',
  588. 'mid onMounted',
  589. 'root onMounted'
  590. ])
  591. calls.length = 0
  592. // update
  593. count.value++
  594. await nextTick()
  595. expect(calls).toEqual([
  596. 'root onBeforeUpdate',
  597. 'mid onBeforeUpdate',
  598. 'child onBeforeUpdate',
  599. 'child onUpdated',
  600. 'mid onUpdated',
  601. 'root onUpdated'
  602. ])
  603. calls.length = 0
  604. // unmount
  605. render(null, root)
  606. expect(calls).toEqual([
  607. 'root onBeforeUnmount',
  608. 'mid onBeforeUnmount',
  609. 'child onBeforeUnmount',
  610. 'child onUnmounted',
  611. 'mid onUnmounted',
  612. 'root onUnmounted'
  613. ])
  614. })
  615. test('mixins', () => {
  616. const calls: string[] = []
  617. const mixinA = {
  618. data() {
  619. return {
  620. a: 1
  621. }
  622. },
  623. created(this: any) {
  624. calls.push('mixinA created')
  625. expect(this.a).toBe(1)
  626. expect(this.b).toBe(2)
  627. expect(this.c).toBe(4)
  628. },
  629. mounted() {
  630. calls.push('mixinA mounted')
  631. }
  632. }
  633. const mixinB = {
  634. props: {
  635. bP: {
  636. type: String
  637. }
  638. },
  639. data() {
  640. return {
  641. b: 2
  642. }
  643. },
  644. created(this: any) {
  645. calls.push('mixinB created')
  646. expect(this.a).toBe(1)
  647. expect(this.b).toBe(2)
  648. expect(this.bP).toBeUndefined()
  649. expect(this.c).toBe(4)
  650. expect(this.cP1).toBeUndefined()
  651. },
  652. mounted() {
  653. calls.push('mixinB mounted')
  654. }
  655. }
  656. const mixinC = defineComponent({
  657. props: ['cP1', 'cP2'],
  658. data() {
  659. return {
  660. c: 3
  661. }
  662. },
  663. created() {
  664. calls.push('mixinC created')
  665. // component data() should overwrite mixin field with same key
  666. expect(this.c).toBe(4)
  667. expect(this.cP1).toBeUndefined()
  668. },
  669. mounted() {
  670. calls.push('mixinC mounted')
  671. }
  672. })
  673. const Comp = defineComponent({
  674. props: {
  675. aaa: String
  676. },
  677. mixins: [defineComponent(mixinA), defineComponent(mixinB), mixinC],
  678. data() {
  679. return {
  680. c: 4,
  681. z: 4
  682. }
  683. },
  684. created() {
  685. calls.push('comp created')
  686. expect(this.a).toBe(1)
  687. expect(this.b).toBe(2)
  688. expect(this.bP).toBeUndefined()
  689. expect(this.c).toBe(4)
  690. expect(this.cP2).toBeUndefined()
  691. expect(this.z).toBe(4)
  692. },
  693. mounted() {
  694. calls.push('comp mounted')
  695. },
  696. render() {
  697. return `${this.a}${this.b}${this.c}`
  698. }
  699. })
  700. expect(renderToString(h(Comp))).toBe(`124`)
  701. expect(calls).toEqual([
  702. 'mixinA created',
  703. 'mixinB created',
  704. 'mixinC created',
  705. 'comp created',
  706. 'mixinA mounted',
  707. 'mixinB mounted',
  708. 'mixinC mounted',
  709. 'comp mounted'
  710. ])
  711. })
  712. test('render from mixin', () => {
  713. const Comp = {
  714. mixins: [
  715. {
  716. render: () => 'from mixin'
  717. }
  718. ]
  719. }
  720. expect(renderToString(h(Comp))).toBe('from mixin')
  721. })
  722. test('chained mixins in extends', () => {
  723. const calls: string[] = []
  724. const mixinA = {
  725. beforeCreate() {
  726. calls.push('mixinA beforeCreate')
  727. },
  728. created() {
  729. calls.push('mixinA created')
  730. }
  731. }
  732. const extendA = {
  733. mixins: [mixinA],
  734. beforeCreate() {
  735. calls.push('extendA beforeCreate')
  736. },
  737. created() {
  738. calls.push('extendA created')
  739. }
  740. }
  741. const Comp = {
  742. extends: extendA,
  743. render: () => '123',
  744. beforeCreate() {
  745. calls.push('self beforeCreate')
  746. },
  747. created() {
  748. calls.push('self created')
  749. }
  750. }
  751. expect(renderToString(h(Comp))).toBe(`123`)
  752. expect(calls).toEqual([
  753. 'mixinA beforeCreate',
  754. 'extendA beforeCreate',
  755. 'self beforeCreate',
  756. 'mixinA created',
  757. 'extendA created',
  758. 'self created'
  759. ])
  760. })
  761. test('chained extends in mixins', () => {
  762. const calls: string[] = []
  763. const extendA = {
  764. beforeCreate() {
  765. calls.push('extendA beforeCreate')
  766. },
  767. created() {
  768. calls.push('extendA created')
  769. }
  770. }
  771. const mixinA = {
  772. extends: extendA,
  773. beforeCreate() {
  774. calls.push('mixinA beforeCreate')
  775. },
  776. created() {
  777. calls.push('mixinA created')
  778. }
  779. }
  780. const Comp = {
  781. mixins: [mixinA],
  782. render: () => '123',
  783. beforeCreate() {
  784. calls.push('self beforeCreate')
  785. },
  786. created() {
  787. calls.push('self created')
  788. }
  789. }
  790. expect(renderToString(h(Comp))).toBe(`123`)
  791. expect(calls).toEqual([
  792. 'extendA beforeCreate',
  793. 'mixinA beforeCreate',
  794. 'self beforeCreate',
  795. 'extendA created',
  796. 'mixinA created',
  797. 'self created'
  798. ])
  799. })
  800. test('extends', () => {
  801. const calls: string[] = []
  802. const Base = {
  803. data() {
  804. return {
  805. a: 1,
  806. b: 1
  807. }
  808. },
  809. methods: {
  810. sayA() {}
  811. },
  812. mounted(this: any) {
  813. expect(this.a).toBe(1)
  814. expect(this.b).toBe(2)
  815. calls.push('base')
  816. }
  817. }
  818. const Comp = defineComponent({
  819. extends: defineComponent(Base),
  820. data() {
  821. return {
  822. b: 2
  823. }
  824. },
  825. mounted() {
  826. calls.push('comp')
  827. },
  828. render() {
  829. return `${this.a}${this.b}`
  830. }
  831. })
  832. expect(renderToString(h(Comp))).toBe(`12`)
  833. expect(calls).toEqual(['base', 'comp'])
  834. })
  835. test('extends with mixins', () => {
  836. const calls: string[] = []
  837. const Base = {
  838. data() {
  839. return {
  840. a: 1,
  841. x: 'base'
  842. }
  843. },
  844. methods: {
  845. sayA() {}
  846. },
  847. mounted(this: any) {
  848. expect(this.a).toBe(1)
  849. expect(this.b).toBeTruthy()
  850. expect(this.c).toBe(2)
  851. calls.push('base')
  852. }
  853. }
  854. const Mixin = {
  855. data() {
  856. return {
  857. b: true,
  858. x: 'mixin'
  859. }
  860. },
  861. mounted(this: any) {
  862. expect(this.a).toBe(1)
  863. expect(this.b).toBeTruthy()
  864. expect(this.c).toBe(2)
  865. calls.push('mixin')
  866. }
  867. }
  868. const Comp = defineComponent({
  869. extends: defineComponent(Base),
  870. mixins: [defineComponent(Mixin)],
  871. data() {
  872. return {
  873. c: 2
  874. }
  875. },
  876. mounted() {
  877. calls.push('comp')
  878. },
  879. render() {
  880. return `${this.a}${this.b}${this.c}${this.x}`
  881. }
  882. })
  883. expect(renderToString(h(Comp))).toBe(`1true2mixin`)
  884. expect(calls).toEqual(['base', 'mixin', 'comp'])
  885. })
  886. test('beforeCreate/created in extends and mixins', () => {
  887. const calls: string[] = []
  888. const BaseA = {
  889. beforeCreate() {
  890. calls.push('beforeCreateA')
  891. },
  892. created() {
  893. calls.push('createdA')
  894. }
  895. }
  896. const BaseB = {
  897. extends: BaseA,
  898. beforeCreate() {
  899. calls.push('beforeCreateB')
  900. },
  901. created() {
  902. calls.push('createdB')
  903. }
  904. }
  905. const MixinA = {
  906. beforeCreate() {
  907. calls.push('beforeCreateC')
  908. },
  909. created() {
  910. calls.push('createdC')
  911. }
  912. }
  913. const MixinB = {
  914. mixins: [MixinA],
  915. beforeCreate() {
  916. calls.push('beforeCreateD')
  917. },
  918. created() {
  919. calls.push('createdD')
  920. }
  921. }
  922. const Comp = {
  923. extends: BaseB,
  924. mixins: [MixinB],
  925. beforeCreate() {
  926. calls.push('selfBeforeCreate')
  927. },
  928. created() {
  929. calls.push('selfCreated')
  930. },
  931. render() {}
  932. }
  933. renderToString(h(Comp))
  934. expect(calls).toEqual([
  935. 'beforeCreateA',
  936. 'beforeCreateB',
  937. 'beforeCreateC',
  938. 'beforeCreateD',
  939. 'selfBeforeCreate',
  940. 'createdA',
  941. 'createdB',
  942. 'createdC',
  943. 'createdD',
  944. 'selfCreated'
  945. ])
  946. })
  947. test('flatten merged options', async () => {
  948. const MixinBase = {
  949. msg1: 'base'
  950. }
  951. const ExtendsBase = {
  952. msg2: 'base'
  953. }
  954. const Mixin = {
  955. mixins: [MixinBase]
  956. }
  957. const Extends = {
  958. extends: ExtendsBase
  959. }
  960. const Comp = defineComponent({
  961. extends: defineComponent(Extends),
  962. mixins: [defineComponent(Mixin)],
  963. render() {
  964. return `${this.$options.msg1},${this.$options.msg2}`
  965. }
  966. })
  967. expect(renderToString(h(Comp))).toBe('base,base')
  968. })
  969. test('extends template', () => {
  970. const Comp = {
  971. extends: {
  972. template: `<h1>Foo</h1>`
  973. }
  974. }
  975. const root = document.createElement('div') as any
  976. domRender(h(Comp), root)
  977. expect(root.innerHTML).toBe(`<h1>Foo</h1>`)
  978. })
  979. test('options defined in component have higher priority', async () => {
  980. const Mixin = {
  981. msg1: 'base'
  982. }
  983. const Extends = {
  984. msg2: 'base'
  985. }
  986. const Comp = defineComponent({
  987. msg1: 'local',
  988. msg2: 'local',
  989. extends: defineComponent(Extends),
  990. mixins: [defineComponent(Mixin)],
  991. render() {
  992. return `${this.$options.msg1},${this.$options.msg2}`
  993. }
  994. })
  995. expect(renderToString(h(Comp))).toBe('local,local')
  996. })
  997. test('accessing setup() state from options', async () => {
  998. const Comp = defineComponent({
  999. setup() {
  1000. return {
  1001. count: ref(0)
  1002. }
  1003. },
  1004. data() {
  1005. return {
  1006. plusOne: (this as any).count + 1
  1007. }
  1008. },
  1009. computed: {
  1010. plusTwo(): number {
  1011. return this.count + 2
  1012. }
  1013. },
  1014. methods: {
  1015. inc() {
  1016. this.count++
  1017. }
  1018. },
  1019. render() {
  1020. return h(
  1021. 'div',
  1022. {
  1023. onClick: this.inc
  1024. },
  1025. `${this.count},${this.plusOne},${this.plusTwo}`
  1026. )
  1027. }
  1028. })
  1029. const root = nodeOps.createElement('div')
  1030. render(h(Comp), root)
  1031. expect(serializeInner(root)).toBe(`<div>0,1,2</div>`)
  1032. triggerEvent(root.children[0] as TestElement, 'click')
  1033. await nextTick()
  1034. expect(serializeInner(root)).toBe(`<div>1,1,3</div>`)
  1035. })
  1036. // #1016
  1037. test('watcher initialization should be deferred in mixins', async () => {
  1038. const mixin1 = {
  1039. data() {
  1040. return {
  1041. mixin1Data: 'mixin1'
  1042. }
  1043. },
  1044. methods: {}
  1045. }
  1046. const watchSpy = vi.fn()
  1047. const mixin2 = {
  1048. watch: {
  1049. mixin3Data: watchSpy
  1050. }
  1051. }
  1052. const mixin3 = {
  1053. data() {
  1054. return {
  1055. mixin3Data: 'mixin3'
  1056. }
  1057. },
  1058. methods: {}
  1059. }
  1060. let vm: any
  1061. const Comp = {
  1062. mixins: [mixin1, mixin2, mixin3],
  1063. render() {},
  1064. created() {
  1065. vm = this
  1066. }
  1067. }
  1068. const root = nodeOps.createElement('div')
  1069. render(h(Comp), root)
  1070. // should have no warnings
  1071. vm.mixin3Data = 'hello'
  1072. await nextTick()
  1073. expect(watchSpy.mock.calls[0].slice(0, 2)).toEqual(['hello', 'mixin3'])
  1074. })
  1075. test('injection from closest ancestor', () => {
  1076. const Root = defineComponent({
  1077. provide: {
  1078. a: 'root'
  1079. },
  1080. render() {
  1081. return [h(Mid), ' ', h(MidWithProvide), ' ', h(MidWithMixinProvide)]
  1082. }
  1083. })
  1084. const Mid = {
  1085. render() {
  1086. return h(Child)
  1087. }
  1088. } as any
  1089. const MidWithProvide = {
  1090. provide: {
  1091. a: 'midWithProvide'
  1092. },
  1093. render() {
  1094. return h(Child)
  1095. }
  1096. } as any
  1097. const mixin = {
  1098. provide: {
  1099. a: 'midWithMixinProvide'
  1100. }
  1101. }
  1102. const MidWithMixinProvide = {
  1103. mixins: [mixin],
  1104. render() {
  1105. return h(Child)
  1106. }
  1107. } as any
  1108. const Child = {
  1109. inject: ['a'],
  1110. render() {
  1111. return this.a
  1112. }
  1113. } as any
  1114. expect(renderToString(h(Root))).toBe(
  1115. 'root midWithProvide midWithMixinProvide'
  1116. )
  1117. })
  1118. describe('options merge strategies', () => {
  1119. test('this.$options.data', () => {
  1120. const mixin = {
  1121. data() {
  1122. return { foo: 1, bar: 2 }
  1123. }
  1124. }
  1125. createApp({
  1126. mixins: [mixin],
  1127. data() {
  1128. return {
  1129. foo: 3,
  1130. baz: 4
  1131. }
  1132. },
  1133. created() {
  1134. expect(this.$options.data).toBeInstanceOf(Function)
  1135. expect(this.$options.data()).toEqual({
  1136. foo: 3,
  1137. bar: 2,
  1138. baz: 4
  1139. })
  1140. },
  1141. render: () => null
  1142. }).mount(nodeOps.createElement('div'))
  1143. })
  1144. test('this.$options.inject', () => {
  1145. const mixin = {
  1146. inject: ['a']
  1147. }
  1148. const app = createApp({
  1149. mixins: [mixin],
  1150. inject: { b: 'b', c: { from: 'd' } },
  1151. created() {
  1152. expect(this.$options.inject.a).toEqual('a')
  1153. expect(this.$options.inject.b).toEqual('b')
  1154. expect(this.$options.inject.c).toEqual({ from: 'd' })
  1155. expect(this.a).toBe(1)
  1156. expect(this.b).toBe(2)
  1157. expect(this.c).toBe(3)
  1158. },
  1159. render: () => null
  1160. })
  1161. app.provide('a', 1)
  1162. app.provide('b', 2)
  1163. app.provide('d', 3)
  1164. app.mount(nodeOps.createElement('div'))
  1165. })
  1166. test('this.$options.provide', () => {
  1167. const mixin = {
  1168. provide: {
  1169. a: 1
  1170. }
  1171. }
  1172. createApp({
  1173. mixins: [mixin],
  1174. provide() {
  1175. return {
  1176. b: 2
  1177. }
  1178. },
  1179. created() {
  1180. expect(this.$options.provide).toBeInstanceOf(Function)
  1181. expect(this.$options.provide()).toEqual({ a: 1, b: 2 })
  1182. },
  1183. render: () => null
  1184. }).mount(nodeOps.createElement('div'))
  1185. })
  1186. test('this.$options[lifecycle-name]', () => {
  1187. const mixin = {
  1188. mounted() {},
  1189. beforeUnmount() {},
  1190. unmounted() {}
  1191. }
  1192. createApp({
  1193. mixins: [mixin],
  1194. mounted() {},
  1195. beforeUnmount() {},
  1196. unmounted() {},
  1197. created() {
  1198. expect(this.$options.mounted).toBeInstanceOf(Array)
  1199. expect(this.$options.mounted.length).toBe(2)
  1200. expect(this.$options.beforeUnmount).toBeInstanceOf(Array)
  1201. expect(this.$options.beforeUnmount.length).toBe(2)
  1202. expect(this.$options.unmounted).toBeInstanceOf(Array)
  1203. expect(this.$options.unmounted.length).toBe(2)
  1204. },
  1205. render: () => null
  1206. }).mount(nodeOps.createElement('div'))
  1207. })
  1208. test('this.$options[asset-name]', () => {
  1209. const mixin = {
  1210. components: {
  1211. a: {}
  1212. },
  1213. directives: {
  1214. d1: {}
  1215. }
  1216. }
  1217. createApp({
  1218. mixins: [mixin],
  1219. components: {
  1220. b: {}
  1221. },
  1222. directives: {
  1223. d2: {}
  1224. },
  1225. created() {
  1226. expect('a' in this.$options.components).toBe(true)
  1227. expect('b' in this.$options.components).toBe(true)
  1228. expect('d1' in this.$options.directives).toBe(true)
  1229. expect('d2' in this.$options.directives).toBe(true)
  1230. },
  1231. render: () => null
  1232. }).mount(nodeOps.createElement('div'))
  1233. })
  1234. test('this.$options.methods', () => {
  1235. const mixin = {
  1236. methods: {
  1237. fn1() {}
  1238. }
  1239. }
  1240. createApp({
  1241. mixins: [mixin],
  1242. methods: {
  1243. fn2() {}
  1244. },
  1245. created() {
  1246. expect(this.$options.methods.fn1).toBeInstanceOf(Function)
  1247. expect(this.$options.methods.fn2).toBeInstanceOf(Function)
  1248. },
  1249. render: () => null
  1250. }).mount(nodeOps.createElement('div'))
  1251. })
  1252. test('this.$options.computed', () => {
  1253. const mixin = {
  1254. computed: {
  1255. c1() {}
  1256. }
  1257. }
  1258. createApp({
  1259. mixins: [mixin],
  1260. computed: {
  1261. c2() {}
  1262. },
  1263. created() {
  1264. expect(this.$options.computed.c1).toBeInstanceOf(Function)
  1265. expect(this.$options.computed.c2).toBeInstanceOf(Function)
  1266. },
  1267. render: () => null
  1268. }).mount(nodeOps.createElement('div'))
  1269. })
  1270. // #2791
  1271. test('modify $options in the beforeCreate hook', async () => {
  1272. const count = ref(0)
  1273. const mixin = {
  1274. data() {
  1275. return { foo: 1 }
  1276. },
  1277. beforeCreate(this: any) {
  1278. if (!this.$options.computed) {
  1279. this.$options.computed = {}
  1280. }
  1281. this.$options.computed.value = () => count.value
  1282. }
  1283. }
  1284. const root = nodeOps.createElement('div')
  1285. createApp({
  1286. mixins: [mixin],
  1287. render(this: any) {
  1288. return this.value
  1289. }
  1290. }).mount(root)
  1291. expect(serializeInner(root)).toBe('0')
  1292. count.value++
  1293. await nextTick()
  1294. expect(serializeInner(root)).toBe('1')
  1295. })
  1296. })
  1297. describe('warnings', () => {
  1298. test('Expected a function as watch handler', () => {
  1299. const Comp = {
  1300. watch: {
  1301. foo: 'notExistingMethod',
  1302. foo2: {
  1303. handler: 'notExistingMethod2'
  1304. }
  1305. },
  1306. render() {}
  1307. }
  1308. const root = nodeOps.createElement('div')
  1309. render(h(Comp), root)
  1310. expect(
  1311. 'Invalid watch handler specified by key "notExistingMethod"'
  1312. ).toHaveBeenWarned()
  1313. expect(
  1314. 'Invalid watch handler specified by key "notExistingMethod2"'
  1315. ).toHaveBeenWarned()
  1316. })
  1317. test('Invalid watch option', () => {
  1318. const Comp = {
  1319. watch: { foo: true },
  1320. render() {}
  1321. }
  1322. const root = nodeOps.createElement('div')
  1323. // @ts-expect-error
  1324. render(h(Comp), root)
  1325. expect('Invalid watch option: "foo"').toHaveBeenWarned()
  1326. })
  1327. test('computed with setter and no getter', () => {
  1328. const Comp = {
  1329. computed: {
  1330. foo: {
  1331. set() {}
  1332. }
  1333. },
  1334. render() {}
  1335. }
  1336. const root = nodeOps.createElement('div')
  1337. render(h(Comp), root)
  1338. expect('Computed property "foo" has no getter.').toHaveBeenWarned()
  1339. })
  1340. test('assigning to computed with no setter', () => {
  1341. let instance: any
  1342. const Comp = {
  1343. computed: {
  1344. foo: {
  1345. get() {}
  1346. }
  1347. },
  1348. mounted() {
  1349. instance = this
  1350. },
  1351. render() {}
  1352. }
  1353. const root = nodeOps.createElement('div')
  1354. render(h(Comp), root)
  1355. instance.foo = 1
  1356. expect(
  1357. 'Write operation failed: computed property "foo" is readonly'
  1358. ).toHaveBeenWarned()
  1359. })
  1360. test('inject property is already declared in props', () => {
  1361. const Comp = {
  1362. data() {
  1363. return {
  1364. a: 1
  1365. }
  1366. },
  1367. provide() {
  1368. return {
  1369. a: this.a
  1370. }
  1371. },
  1372. render() {
  1373. return [h(ChildA)]
  1374. }
  1375. } as any
  1376. const ChildA = {
  1377. props: { a: Number },
  1378. inject: ['a'],
  1379. render() {
  1380. return this.a
  1381. }
  1382. } as any
  1383. const root = nodeOps.createElement('div')
  1384. render(h(Comp), root)
  1385. expect(
  1386. `Inject property "a" is already defined in Props.`
  1387. ).toHaveBeenWarned()
  1388. })
  1389. test('methods property is not a function', () => {
  1390. const Comp = {
  1391. methods: {
  1392. foo: 1
  1393. },
  1394. render() {}
  1395. }
  1396. const root = nodeOps.createElement('div')
  1397. render(h(Comp), root)
  1398. expect(
  1399. `Method "foo" has type "number" in the component definition. ` +
  1400. `Did you reference the function correctly?`
  1401. ).toHaveBeenWarned()
  1402. })
  1403. test('methods property is already declared in props', () => {
  1404. const Comp = {
  1405. props: {
  1406. foo: Number
  1407. },
  1408. methods: {
  1409. foo() {}
  1410. },
  1411. render() {}
  1412. }
  1413. const root = nodeOps.createElement('div')
  1414. render(h(Comp), root)
  1415. expect(
  1416. `Methods property "foo" is already defined in Props.`
  1417. ).toHaveBeenWarned()
  1418. })
  1419. test('methods property is already declared in inject', () => {
  1420. const Comp = {
  1421. data() {
  1422. return {
  1423. a: 1
  1424. }
  1425. },
  1426. provide() {
  1427. return {
  1428. a: this.a
  1429. }
  1430. },
  1431. render() {
  1432. return [h(ChildA)]
  1433. }
  1434. } as any
  1435. const ChildA = {
  1436. methods: {
  1437. a: () => null
  1438. },
  1439. inject: ['a'],
  1440. render() {
  1441. return this.a
  1442. }
  1443. } as any
  1444. const root = nodeOps.createElement('div')
  1445. render(h(Comp), root)
  1446. expect(
  1447. `Methods property "a" is already defined in Inject.`
  1448. ).toHaveBeenWarned()
  1449. })
  1450. test('data property is already declared in props', () => {
  1451. const Comp = {
  1452. props: { foo: Number },
  1453. data: () => ({
  1454. foo: 1
  1455. }),
  1456. render() {}
  1457. }
  1458. const root = nodeOps.createElement('div')
  1459. render(h(Comp), root)
  1460. expect(
  1461. `Data property "foo" is already defined in Props.`
  1462. ).toHaveBeenWarned()
  1463. })
  1464. test('data property is already declared in inject', () => {
  1465. const Comp = {
  1466. data() {
  1467. return {
  1468. a: 1
  1469. }
  1470. },
  1471. provide() {
  1472. return {
  1473. a: this.a
  1474. }
  1475. },
  1476. render() {
  1477. return [h(ChildA)]
  1478. }
  1479. } as any
  1480. const ChildA = {
  1481. data() {
  1482. return {
  1483. a: 1
  1484. }
  1485. },
  1486. inject: ['a'],
  1487. render() {
  1488. return this.a
  1489. }
  1490. } as any
  1491. const root = nodeOps.createElement('div')
  1492. render(h(Comp), root)
  1493. expect(
  1494. `Data property "a" is already defined in Inject.`
  1495. ).toHaveBeenWarned()
  1496. })
  1497. test('data property is already declared in methods', () => {
  1498. const Comp = {
  1499. data: () => ({
  1500. foo: 1
  1501. }),
  1502. methods: {
  1503. foo() {}
  1504. },
  1505. render() {}
  1506. }
  1507. const root = nodeOps.createElement('div')
  1508. render(h(Comp), root)
  1509. expect(
  1510. `Data property "foo" is already defined in Methods.`
  1511. ).toHaveBeenWarned()
  1512. })
  1513. test('computed property is already declared in props', () => {
  1514. const Comp = {
  1515. props: { foo: Number },
  1516. computed: {
  1517. foo() {}
  1518. },
  1519. render() {}
  1520. }
  1521. const root = nodeOps.createElement('div')
  1522. render(h(Comp), root)
  1523. expect(
  1524. `Computed property "foo" is already defined in Props.`
  1525. ).toHaveBeenWarned()
  1526. })
  1527. test('computed property is already declared in inject', () => {
  1528. const Comp = {
  1529. data() {
  1530. return {
  1531. a: 1
  1532. }
  1533. },
  1534. provide() {
  1535. return {
  1536. a: this.a
  1537. }
  1538. },
  1539. render() {
  1540. return [h(ChildA)]
  1541. }
  1542. } as any
  1543. const ChildA = {
  1544. computed: {
  1545. a: {
  1546. get() {},
  1547. set() {}
  1548. }
  1549. },
  1550. inject: ['a'],
  1551. render() {
  1552. return this.a
  1553. }
  1554. } as any
  1555. const root = nodeOps.createElement('div')
  1556. render(h(Comp), root)
  1557. expect(
  1558. `Computed property "a" is already defined in Inject.`
  1559. ).toHaveBeenWarned()
  1560. })
  1561. test('computed property is already declared in methods', () => {
  1562. const Comp = {
  1563. computed: {
  1564. foo() {}
  1565. },
  1566. methods: {
  1567. foo() {}
  1568. },
  1569. render() {}
  1570. }
  1571. const root = nodeOps.createElement('div')
  1572. render(h(Comp), root)
  1573. expect(
  1574. `Computed property "foo" is already defined in Methods.`
  1575. ).toHaveBeenWarned()
  1576. })
  1577. test('computed property is already declared in data', () => {
  1578. const Comp = {
  1579. data: () => ({
  1580. foo: 1
  1581. }),
  1582. computed: {
  1583. foo() {}
  1584. },
  1585. render() {}
  1586. }
  1587. const root = nodeOps.createElement('div')
  1588. render(h(Comp), root)
  1589. expect(
  1590. `Computed property "foo" is already defined in Data.`
  1591. ).toHaveBeenWarned()
  1592. })
  1593. })
  1594. })