apiOptions.spec.ts 34 KB

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