apiOptions.spec.ts 36 KB

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