apiOptions.spec.ts 24 KB

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