apiOptions.spec.ts 21 KB

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