apiOptions.spec.ts 26 KB

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