apiOptions.spec.ts 29 KB

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