apiOptions.spec.ts 27 KB

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