apiOptions.spec.ts 27 KB

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