apiOptions.spec.ts 18 KB

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