apiOptions.spec.ts 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. import {
  2. h,
  3. nodeOps,
  4. render,
  5. serializeInner,
  6. triggerEvent,
  7. TestElement,
  8. nextTick,
  9. renderToString,
  10. ref,
  11. defineComponent
  12. } from '@vue/runtime-test'
  13. import { mockWarn } from '@vue/shared'
  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. return {
  77. foo: 1
  78. }
  79. },
  80. methods: {
  81. inc() {
  82. this.foo++
  83. }
  84. },
  85. render() {
  86. return h(
  87. 'div',
  88. {
  89. onClick: this.inc
  90. },
  91. this.foo
  92. )
  93. }
  94. })
  95. const root = nodeOps.createElement('div')
  96. render(h(Comp), root)
  97. expect(serializeInner(root)).toBe(`<div>1</div>`)
  98. triggerEvent(root.children[0] as TestElement, 'click')
  99. await nextTick()
  100. expect(serializeInner(root)).toBe(`<div>2</div>`)
  101. })
  102. test('watch', async () => {
  103. function returnThis(this: any) {
  104. return this
  105. }
  106. const spyA = jest.fn(returnThis)
  107. const spyB = jest.fn(returnThis)
  108. const spyC = jest.fn(returnThis)
  109. let ctx: any
  110. const Comp = {
  111. data() {
  112. return {
  113. foo: 1,
  114. bar: 2,
  115. baz: {
  116. qux: 3
  117. }
  118. }
  119. },
  120. watch: {
  121. // string method name
  122. foo: 'onFooChange',
  123. // direct function
  124. bar: spyB,
  125. baz: {
  126. handler: spyC,
  127. deep: true
  128. }
  129. },
  130. methods: {
  131. onFooChange: spyA
  132. },
  133. render() {
  134. ctx = this
  135. }
  136. }
  137. const root = nodeOps.createElement('div')
  138. render(h(Comp), root)
  139. function assertCall(spy: jest.Mock, callIndex: number, args: any[]) {
  140. expect(spy.mock.calls[callIndex].slice(0, 2)).toMatchObject(args)
  141. expect(spy).toHaveReturnedWith(ctx)
  142. }
  143. ctx.foo++
  144. await nextTick()
  145. expect(spyA).toHaveBeenCalledTimes(1)
  146. assertCall(spyA, 0, [2, 1])
  147. ctx.bar++
  148. await nextTick()
  149. expect(spyB).toHaveBeenCalledTimes(1)
  150. assertCall(spyB, 0, [3, 2])
  151. ctx.baz.qux++
  152. await nextTick()
  153. expect(spyC).toHaveBeenCalledTimes(1)
  154. // new and old objects have same identity
  155. assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
  156. })
  157. test('watch array', async () => {
  158. function returnThis(this: any) {
  159. return this
  160. }
  161. const spyA = jest.fn(returnThis)
  162. const spyB = jest.fn(returnThis)
  163. const spyC = jest.fn(returnThis)
  164. let ctx: any
  165. const Comp = {
  166. data() {
  167. return {
  168. foo: 1,
  169. bar: 2,
  170. baz: {
  171. qux: 3
  172. }
  173. }
  174. },
  175. watch: {
  176. // string method name
  177. foo: ['onFooChange'],
  178. // direct function
  179. bar: [spyB],
  180. baz: [
  181. {
  182. handler: spyC,
  183. deep: true
  184. }
  185. ]
  186. },
  187. methods: {
  188. onFooChange: spyA
  189. },
  190. render() {
  191. ctx = this
  192. }
  193. }
  194. const root = nodeOps.createElement('div')
  195. render(h(Comp), root)
  196. function assertCall(spy: jest.Mock, callIndex: number, args: any[]) {
  197. expect(spy.mock.calls[callIndex].slice(0, 2)).toMatchObject(args)
  198. expect(spy).toHaveReturnedWith(ctx)
  199. }
  200. ctx.foo++
  201. await nextTick()
  202. expect(spyA).toHaveBeenCalledTimes(1)
  203. assertCall(spyA, 0, [2, 1])
  204. ctx.bar++
  205. await nextTick()
  206. expect(spyB).toHaveBeenCalledTimes(1)
  207. assertCall(spyB, 0, [3, 2])
  208. ctx.baz.qux++
  209. await nextTick()
  210. expect(spyC).toHaveBeenCalledTimes(1)
  211. // new and old objects have same identity
  212. assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
  213. })
  214. test('provide/inject', () => {
  215. const Root = {
  216. data() {
  217. return {
  218. a: 1
  219. }
  220. },
  221. provide() {
  222. return {
  223. a: this.a
  224. }
  225. },
  226. render() {
  227. return [h(ChildA), h(ChildB), h(ChildC), h(ChildD)]
  228. }
  229. } as any
  230. const ChildA = {
  231. inject: ['a'],
  232. render() {
  233. return this.a
  234. }
  235. } as any
  236. const ChildB = {
  237. // object alias
  238. inject: { b: 'a' },
  239. render() {
  240. return this.b
  241. }
  242. } as any
  243. const ChildC = {
  244. inject: {
  245. b: {
  246. from: 'a'
  247. }
  248. },
  249. render() {
  250. return this.b
  251. }
  252. } as any
  253. const ChildD = {
  254. inject: {
  255. b: {
  256. from: 'c',
  257. default: 2
  258. }
  259. },
  260. render() {
  261. return this.b
  262. }
  263. } as any
  264. expect(renderToString(h(Root))).toBe(`1112`)
  265. })
  266. test('lifecycle', async () => {
  267. const count = ref(0)
  268. const root = nodeOps.createElement('div')
  269. const calls: string[] = []
  270. const Root = {
  271. beforeCreate() {
  272. calls.push('root beforeCreate')
  273. },
  274. created() {
  275. calls.push('root created')
  276. },
  277. beforeMount() {
  278. calls.push('root onBeforeMount')
  279. },
  280. mounted() {
  281. calls.push('root onMounted')
  282. },
  283. beforeUpdate() {
  284. calls.push('root onBeforeUpdate')
  285. },
  286. updated() {
  287. calls.push('root onUpdated')
  288. },
  289. beforeUnmount() {
  290. calls.push('root onBeforeUnmount')
  291. },
  292. unmounted() {
  293. calls.push('root onUnmounted')
  294. },
  295. render() {
  296. return h(Mid, { count: count.value })
  297. }
  298. }
  299. const Mid = {
  300. beforeCreate() {
  301. calls.push('mid beforeCreate')
  302. },
  303. created() {
  304. calls.push('mid created')
  305. },
  306. beforeMount() {
  307. calls.push('mid onBeforeMount')
  308. },
  309. mounted() {
  310. calls.push('mid onMounted')
  311. },
  312. beforeUpdate() {
  313. calls.push('mid onBeforeUpdate')
  314. },
  315. updated() {
  316. calls.push('mid onUpdated')
  317. },
  318. beforeUnmount() {
  319. calls.push('mid onBeforeUnmount')
  320. },
  321. unmounted() {
  322. calls.push('mid onUnmounted')
  323. },
  324. render(this: any) {
  325. return h(Child, { count: this.$props.count })
  326. }
  327. }
  328. const Child = {
  329. beforeCreate() {
  330. calls.push('child beforeCreate')
  331. },
  332. created() {
  333. calls.push('child created')
  334. },
  335. beforeMount() {
  336. calls.push('child onBeforeMount')
  337. },
  338. mounted() {
  339. calls.push('child onMounted')
  340. },
  341. beforeUpdate() {
  342. calls.push('child onBeforeUpdate')
  343. },
  344. updated() {
  345. calls.push('child onUpdated')
  346. },
  347. beforeUnmount() {
  348. calls.push('child onBeforeUnmount')
  349. },
  350. unmounted() {
  351. calls.push('child onUnmounted')
  352. },
  353. render(this: any) {
  354. return h('div', this.$props.count)
  355. }
  356. }
  357. // mount
  358. render(h(Root), root)
  359. expect(calls).toEqual([
  360. 'root beforeCreate',
  361. 'root created',
  362. 'root onBeforeMount',
  363. 'mid beforeCreate',
  364. 'mid created',
  365. 'mid onBeforeMount',
  366. 'child beforeCreate',
  367. 'child created',
  368. 'child onBeforeMount',
  369. 'child onMounted',
  370. 'mid onMounted',
  371. 'root onMounted'
  372. ])
  373. calls.length = 0
  374. // update
  375. count.value++
  376. await nextTick()
  377. expect(calls).toEqual([
  378. 'root onBeforeUpdate',
  379. 'mid onBeforeUpdate',
  380. 'child onBeforeUpdate',
  381. 'child onUpdated',
  382. 'mid onUpdated',
  383. 'root onUpdated'
  384. ])
  385. calls.length = 0
  386. // unmount
  387. render(null, root)
  388. expect(calls).toEqual([
  389. 'root onBeforeUnmount',
  390. 'mid onBeforeUnmount',
  391. 'child onBeforeUnmount',
  392. 'child onUnmounted',
  393. 'mid onUnmounted',
  394. 'root onUnmounted'
  395. ])
  396. })
  397. test('mixins', () => {
  398. const calls: string[] = []
  399. const mixinA = {
  400. data() {
  401. return {
  402. a: 1
  403. }
  404. },
  405. created(this: any) {
  406. calls.push('mixinA created')
  407. expect(this.a).toBe(1)
  408. expect(this.b).toBe(2)
  409. expect(this.c).toBe(3)
  410. },
  411. mounted() {
  412. calls.push('mixinA mounted')
  413. }
  414. }
  415. const mixinB = {
  416. props: {
  417. bP: {
  418. type: String
  419. }
  420. },
  421. data() {
  422. return {
  423. b: 2
  424. }
  425. },
  426. created(this: any) {
  427. calls.push('mixinB created')
  428. expect(this.a).toBe(1)
  429. expect(this.b).toBe(2)
  430. expect(this.bP).toBeUndefined()
  431. expect(this.c).toBe(3)
  432. expect(this.cP1).toBeUndefined()
  433. },
  434. mounted() {
  435. calls.push('mixinB mounted')
  436. }
  437. }
  438. const mixinC = defineComponent({
  439. props: ['cP1', 'cP2'],
  440. data() {
  441. return {
  442. c: 3
  443. }
  444. },
  445. created() {
  446. calls.push('mixinC created')
  447. expect(this.c).toBe(3)
  448. expect(this.cP1).toBeUndefined()
  449. },
  450. mounted() {
  451. calls.push('mixinC mounted')
  452. }
  453. })
  454. const Comp = defineComponent({
  455. props: {
  456. aaa: String
  457. },
  458. mixins: [defineComponent(mixinA), defineComponent(mixinB), mixinC],
  459. data() {
  460. return {
  461. z: 4
  462. }
  463. },
  464. created() {
  465. calls.push('comp created')
  466. expect(this.a).toBe(1)
  467. expect(this.b).toBe(2)
  468. expect(this.bP).toBeUndefined()
  469. expect(this.c).toBe(3)
  470. expect(this.cP2).toBeUndefined()
  471. expect(this.z).toBe(4)
  472. },
  473. mounted() {
  474. calls.push('comp mounted')
  475. },
  476. render() {
  477. return `${this.a}${this.b}${this.c}`
  478. }
  479. })
  480. expect(renderToString(h(Comp))).toBe(`123`)
  481. expect(calls).toEqual([
  482. 'mixinA created',
  483. 'mixinB created',
  484. 'mixinC created',
  485. 'comp created',
  486. 'mixinA mounted',
  487. 'mixinB mounted',
  488. 'mixinC mounted',
  489. 'comp mounted'
  490. ])
  491. })
  492. test('render from mixin', () => {
  493. const Comp = {
  494. mixins: [
  495. {
  496. render: () => 'from mixin'
  497. }
  498. ]
  499. }
  500. expect(renderToString(h(Comp))).toBe('from mixin')
  501. })
  502. test('extends', () => {
  503. const calls: string[] = []
  504. const Base = {
  505. data() {
  506. return {
  507. a: 1
  508. }
  509. },
  510. methods: {
  511. sayA() {}
  512. },
  513. mounted(this: any) {
  514. expect(this.a).toBe(1)
  515. expect(this.b).toBe(2)
  516. calls.push('base')
  517. }
  518. }
  519. const Comp = defineComponent({
  520. extends: defineComponent(Base),
  521. data() {
  522. return {
  523. b: 2
  524. }
  525. },
  526. mounted() {
  527. calls.push('comp')
  528. },
  529. render() {
  530. return `${this.a}${this.b}`
  531. }
  532. })
  533. expect(renderToString(h(Comp))).toBe(`12`)
  534. expect(calls).toEqual(['base', 'comp'])
  535. })
  536. test('extends with mixins', () => {
  537. const calls: string[] = []
  538. const Base = {
  539. data() {
  540. return {
  541. a: 1
  542. }
  543. },
  544. methods: {
  545. sayA() {}
  546. },
  547. mounted(this: any) {
  548. expect(this.a).toBe(1)
  549. expect(this.b).toBeTruthy()
  550. expect(this.c).toBe(2)
  551. calls.push('base')
  552. }
  553. }
  554. const Base2 = {
  555. data() {
  556. return {
  557. b: true
  558. }
  559. },
  560. mounted(this: any) {
  561. expect(this.a).toBe(1)
  562. expect(this.b).toBeTruthy()
  563. expect(this.c).toBe(2)
  564. calls.push('base2')
  565. }
  566. }
  567. const Comp = defineComponent({
  568. extends: defineComponent(Base),
  569. mixins: [defineComponent(Base2)],
  570. data() {
  571. return {
  572. c: 2
  573. }
  574. },
  575. mounted() {
  576. calls.push('comp')
  577. },
  578. render() {
  579. return `${this.a}${this.b}${this.c}`
  580. }
  581. })
  582. expect(renderToString(h(Comp))).toBe(`1true2`)
  583. expect(calls).toEqual(['base', 'base2', 'comp'])
  584. })
  585. test('accessing setup() state from options', async () => {
  586. const Comp = defineComponent({
  587. setup() {
  588. return {
  589. count: ref(0)
  590. }
  591. },
  592. data() {
  593. return {
  594. plusOne: (this as any).count + 1
  595. }
  596. },
  597. computed: {
  598. plusTwo(): number {
  599. return this.count + 2
  600. }
  601. },
  602. methods: {
  603. inc() {
  604. this.count++
  605. }
  606. },
  607. render() {
  608. return h(
  609. 'div',
  610. {
  611. onClick: this.inc
  612. },
  613. `${this.count},${this.plusOne},${this.plusTwo}`
  614. )
  615. }
  616. })
  617. const root = nodeOps.createElement('div')
  618. render(h(Comp), root)
  619. expect(serializeInner(root)).toBe(`<div>0,1,2</div>`)
  620. triggerEvent(root.children[0] as TestElement, 'click')
  621. await nextTick()
  622. expect(serializeInner(root)).toBe(`<div>1,1,3</div>`)
  623. })
  624. // #1016
  625. test('watcher initialization should be deferred in mixins', async () => {
  626. const mixin1 = {
  627. data() {
  628. return {
  629. mixin1Data: 'mixin1'
  630. }
  631. },
  632. methods: {}
  633. }
  634. const watchSpy = jest.fn()
  635. const mixin2 = {
  636. watch: {
  637. mixin3Data: watchSpy
  638. }
  639. }
  640. const mixin3 = {
  641. data() {
  642. return {
  643. mixin3Data: 'mixin3'
  644. }
  645. },
  646. methods: {}
  647. }
  648. let vm: any
  649. const Comp = {
  650. mixins: [mixin1, mixin2, mixin3],
  651. render() {},
  652. created() {
  653. vm = this
  654. }
  655. }
  656. const root = nodeOps.createElement('div')
  657. render(h(Comp), root)
  658. // should have no warnings
  659. vm.mixin3Data = 'hello'
  660. await nextTick()
  661. expect(watchSpy.mock.calls[0].slice(0, 2)).toEqual(['hello', 'mixin3'])
  662. })
  663. describe('warnings', () => {
  664. mockWarn()
  665. test('Expected a function as watch handler', () => {
  666. const Comp = {
  667. watch: {
  668. foo: 'notExistingMethod'
  669. },
  670. render() {}
  671. }
  672. const root = nodeOps.createElement('div')
  673. render(h(Comp), root)
  674. expect(
  675. 'Invalid watch handler specified by key "notExistingMethod"'
  676. ).toHaveBeenWarned()
  677. })
  678. test('Invalid watch option', () => {
  679. const Comp = {
  680. watch: { foo: true },
  681. render() {}
  682. }
  683. const root = nodeOps.createElement('div')
  684. // @ts-ignore
  685. render(h(Comp), root)
  686. expect('Invalid watch option: "foo"').toHaveBeenWarned()
  687. })
  688. test('computed with setter and no getter', () => {
  689. const Comp = {
  690. computed: {
  691. foo: {
  692. set() {}
  693. }
  694. },
  695. render() {}
  696. }
  697. const root = nodeOps.createElement('div')
  698. render(h(Comp), root)
  699. expect('Computed property "foo" has no getter.').toHaveBeenWarned()
  700. })
  701. test('assigning to computed with no setter', () => {
  702. let instance: any
  703. const Comp = {
  704. computed: {
  705. foo: {
  706. get() {}
  707. }
  708. },
  709. mounted() {
  710. instance = this
  711. },
  712. render() {}
  713. }
  714. const root = nodeOps.createElement('div')
  715. render(h(Comp), root)
  716. instance.foo = 1
  717. expect(
  718. 'Write operation failed: computed property "foo" is readonly'
  719. ).toHaveBeenWarned()
  720. })
  721. test('inject property is already declared in props', () => {
  722. const Comp = {
  723. data() {
  724. return {
  725. a: 1
  726. }
  727. },
  728. provide() {
  729. return {
  730. a: this.a
  731. }
  732. },
  733. render() {
  734. return [h(ChildA)]
  735. }
  736. } as any
  737. const ChildA = {
  738. props: { a: Number },
  739. inject: ['a'],
  740. render() {
  741. return this.a
  742. }
  743. } as any
  744. const root = nodeOps.createElement('div')
  745. render(h(Comp), root)
  746. expect(
  747. `Inject property "a" is already defined in Props.`
  748. ).toHaveBeenWarned()
  749. })
  750. test('methods property is not a function', () => {
  751. const Comp = {
  752. methods: {
  753. foo: 1
  754. },
  755. render() {}
  756. }
  757. const root = nodeOps.createElement('div')
  758. render(h(Comp), root)
  759. expect(
  760. `Method "foo" has type "number" in the component definition. ` +
  761. `Did you reference the function correctly?`
  762. ).toHaveBeenWarned()
  763. })
  764. test('methods property is already declared in props', () => {
  765. const Comp = {
  766. props: {
  767. foo: Number
  768. },
  769. methods: {
  770. foo() {}
  771. },
  772. render() {}
  773. }
  774. const root = nodeOps.createElement('div')
  775. render(h(Comp), root)
  776. expect(
  777. `Methods property "foo" is already defined in Props.`
  778. ).toHaveBeenWarned()
  779. })
  780. test('methods property is already declared in inject', () => {
  781. const Comp = {
  782. data() {
  783. return {
  784. a: 1
  785. }
  786. },
  787. provide() {
  788. return {
  789. a: this.a
  790. }
  791. },
  792. render() {
  793. return [h(ChildA)]
  794. }
  795. } as any
  796. const ChildA = {
  797. methods: {
  798. a: () => null
  799. },
  800. inject: ['a'],
  801. render() {
  802. return this.a
  803. }
  804. } as any
  805. const root = nodeOps.createElement('div')
  806. render(h(Comp), root)
  807. expect(
  808. `Methods property "a" is already defined in Inject.`
  809. ).toHaveBeenWarned()
  810. })
  811. test('data property is already declared in props', () => {
  812. const Comp = {
  813. props: { foo: Number },
  814. data: () => ({
  815. foo: 1
  816. }),
  817. render() {}
  818. }
  819. const root = nodeOps.createElement('div')
  820. render(h(Comp), root)
  821. expect(
  822. `Data property "foo" is already defined in Props.`
  823. ).toHaveBeenWarned()
  824. })
  825. test('data property is already declared in inject', () => {
  826. const Comp = {
  827. data() {
  828. return {
  829. a: 1
  830. }
  831. },
  832. provide() {
  833. return {
  834. a: this.a
  835. }
  836. },
  837. render() {
  838. return [h(ChildA)]
  839. }
  840. } as any
  841. const ChildA = {
  842. data() {
  843. return {
  844. a: 1
  845. }
  846. },
  847. inject: ['a'],
  848. render() {
  849. return this.a
  850. }
  851. } as any
  852. const root = nodeOps.createElement('div')
  853. render(h(Comp), root)
  854. expect(
  855. `Data property "a" is already defined in Inject.`
  856. ).toHaveBeenWarned()
  857. })
  858. test('data property is already declared in methods', () => {
  859. const Comp = {
  860. data: () => ({
  861. foo: 1
  862. }),
  863. methods: {
  864. foo() {}
  865. },
  866. render() {}
  867. }
  868. const root = nodeOps.createElement('div')
  869. render(h(Comp), root)
  870. expect(
  871. `Data property "foo" is already defined in Methods.`
  872. ).toHaveBeenWarned()
  873. })
  874. test('computed property is already declared in props', () => {
  875. const Comp = {
  876. props: { foo: Number },
  877. computed: {
  878. foo() {}
  879. },
  880. render() {}
  881. }
  882. const root = nodeOps.createElement('div')
  883. render(h(Comp), root)
  884. expect(
  885. `Computed property "foo" is already defined in Props.`
  886. ).toHaveBeenWarned()
  887. })
  888. test('computed property is already declared in inject', () => {
  889. const Comp = {
  890. data() {
  891. return {
  892. a: 1
  893. }
  894. },
  895. provide() {
  896. return {
  897. a: this.a
  898. }
  899. },
  900. render() {
  901. return [h(ChildA)]
  902. }
  903. } as any
  904. const ChildA = {
  905. computed: {
  906. a: {
  907. get() {},
  908. set() {}
  909. }
  910. },
  911. inject: ['a'],
  912. render() {
  913. return this.a
  914. }
  915. } as any
  916. const root = nodeOps.createElement('div')
  917. render(h(Comp), root)
  918. expect(
  919. `Computed property "a" is already defined in Inject.`
  920. ).toHaveBeenWarned()
  921. })
  922. test('computed property is already declared in methods', () => {
  923. const Comp = {
  924. computed: {
  925. foo() {}
  926. },
  927. methods: {
  928. foo() {}
  929. },
  930. render() {}
  931. }
  932. const root = nodeOps.createElement('div')
  933. render(h(Comp), root)
  934. expect(
  935. `Computed property "foo" is already defined in Methods.`
  936. ).toHaveBeenWarned()
  937. })
  938. test('computed property is already declared in data', () => {
  939. const Comp = {
  940. data: () => ({
  941. foo: 1
  942. }),
  943. computed: {
  944. foo() {}
  945. },
  946. render() {}
  947. }
  948. const root = nodeOps.createElement('div')
  949. render(h(Comp), root)
  950. expect(
  951. `Computed property "foo" is already defined in Data.`
  952. ).toHaveBeenWarned()
  953. })
  954. })
  955. })