apiOptions.spec.ts 26 KB

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