apiOptions.spec.ts 17 KB

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