apiOptions.spec.ts 36 KB

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