apiOptions.spec.ts 19 KB

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