apiOptions.spec.ts 25 KB

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