apiOptions.spec.ts 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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. computed
  14. } from '@vue/runtime-test'
  15. import { render as domRender } from 'vue'
  16. describe('api: options', () => {
  17. test('data', async () => {
  18. const Comp = defineComponent({
  19. data() {
  20. return {
  21. foo: 1
  22. }
  23. },
  24. render() {
  25. return h(
  26. 'div',
  27. {
  28. onClick: () => {
  29. this.foo++
  30. }
  31. },
  32. this.foo
  33. )
  34. }
  35. })
  36. const root = nodeOps.createElement('div')
  37. render(h(Comp), root)
  38. expect(serializeInner(root)).toBe(`<div>1</div>`)
  39. triggerEvent(root.children[0] as TestElement, 'click')
  40. await nextTick()
  41. expect(serializeInner(root)).toBe(`<div>2</div>`)
  42. })
  43. test('computed', async () => {
  44. const Comp = defineComponent({
  45. data() {
  46. return {
  47. foo: 1
  48. }
  49. },
  50. computed: {
  51. bar(): number {
  52. return this.foo + 1
  53. },
  54. baz: (vm): number => vm.bar + 1
  55. },
  56. render() {
  57. return h(
  58. 'div',
  59. {
  60. onClick: () => {
  61. this.foo++
  62. }
  63. },
  64. this.bar + this.baz
  65. )
  66. }
  67. })
  68. const root = nodeOps.createElement('div')
  69. render(h(Comp), root)
  70. expect(serializeInner(root)).toBe(`<div>5</div>`)
  71. triggerEvent(root.children[0] as TestElement, 'click')
  72. await nextTick()
  73. expect(serializeInner(root)).toBe(`<div>7</div>`)
  74. })
  75. test('methods', async () => {
  76. const Comp = defineComponent({
  77. data() {
  78. // #3300 method on ctx should be overwritable
  79. this.incBy = this.incBy.bind(this, 2)
  80. return {
  81. foo: 1
  82. }
  83. },
  84. methods: {
  85. inc() {
  86. this.foo++
  87. },
  88. incBy(n = 0) {
  89. this.foo += n
  90. }
  91. },
  92. render() {
  93. return h(
  94. 'div',
  95. {
  96. onClick: this.inc,
  97. onFoo: this.incBy
  98. },
  99. this.foo
  100. )
  101. }
  102. })
  103. const root = nodeOps.createElement('div')
  104. render(h(Comp), root)
  105. expect(serializeInner(root)).toBe(`<div>1</div>`)
  106. triggerEvent(root.children[0] as TestElement, 'click')
  107. await nextTick()
  108. expect(serializeInner(root)).toBe(`<div>2</div>`)
  109. triggerEvent(root.children[0] as TestElement, 'foo')
  110. await nextTick()
  111. expect(serializeInner(root)).toBe(`<div>4</div>`)
  112. })
  113. test('component’s own methods have higher priority than global properties', async () => {
  114. const app = createApp({
  115. methods: {
  116. foo() {
  117. return 'foo'
  118. }
  119. },
  120. render() {
  121. return this.foo()
  122. }
  123. })
  124. app.config.globalProperties.foo = () => 'bar'
  125. const root = nodeOps.createElement('div')
  126. app.mount(root)
  127. expect(serializeInner(root)).toBe(`foo`)
  128. })
  129. test('watch', async () => {
  130. function returnThis(this: any) {
  131. return this
  132. }
  133. const spyA = jest.fn(returnThis)
  134. const spyB = jest.fn(returnThis)
  135. const spyC = jest.fn(returnThis)
  136. const spyD = jest.fn(returnThis)
  137. const spyE = jest.fn(returnThis)
  138. let ctx: any
  139. const Comp = {
  140. data() {
  141. return {
  142. foo: 1,
  143. bar: 2,
  144. baz: {
  145. qux: 3
  146. },
  147. qux: 4,
  148. dot: {
  149. path: 5
  150. }
  151. }
  152. },
  153. watch: {
  154. // string method name
  155. foo: 'onFooChange',
  156. // direct function
  157. bar: spyB,
  158. baz: {
  159. handler: spyC,
  160. deep: true
  161. },
  162. qux: {
  163. handler: 'onQuxChange'
  164. },
  165. 'dot.path': spyE
  166. },
  167. methods: {
  168. onFooChange: spyA,
  169. onQuxChange: spyD
  170. },
  171. render() {
  172. ctx = this
  173. }
  174. }
  175. const root = nodeOps.createElement('div')
  176. render(h(Comp), root)
  177. function assertCall(spy: jest.Mock, callIndex: number, args: any[]) {
  178. expect(spy.mock.calls[callIndex].slice(0, 2)).toMatchObject(args)
  179. expect(spy).toHaveReturnedWith(ctx)
  180. }
  181. ctx.foo++
  182. await nextTick()
  183. expect(spyA).toHaveBeenCalledTimes(1)
  184. assertCall(spyA, 0, [2, 1])
  185. ctx.bar++
  186. await nextTick()
  187. expect(spyB).toHaveBeenCalledTimes(1)
  188. assertCall(spyB, 0, [3, 2])
  189. ctx.baz.qux++
  190. await nextTick()
  191. expect(spyC).toHaveBeenCalledTimes(1)
  192. // new and old objects have same identity
  193. assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
  194. ctx.qux++
  195. await nextTick()
  196. expect(spyD).toHaveBeenCalledTimes(1)
  197. assertCall(spyD, 0, [5, 4])
  198. ctx.dot.path++
  199. await nextTick()
  200. expect(spyE).toHaveBeenCalledTimes(1)
  201. assertCall(spyE, 0, [6, 5])
  202. })
  203. test('watch array', async () => {
  204. function returnThis(this: any) {
  205. return this
  206. }
  207. const spyA = jest.fn(returnThis)
  208. const spyB = jest.fn(returnThis)
  209. const spyC = jest.fn(returnThis)
  210. let ctx: any
  211. const Comp = {
  212. data() {
  213. return {
  214. foo: 1,
  215. bar: 2,
  216. baz: {
  217. qux: 3
  218. }
  219. }
  220. },
  221. watch: {
  222. // string method name
  223. foo: ['onFooChange'],
  224. // direct function
  225. bar: [spyB],
  226. baz: [
  227. {
  228. handler: spyC,
  229. deep: true
  230. }
  231. ]
  232. },
  233. methods: {
  234. onFooChange: spyA
  235. },
  236. render() {
  237. ctx = this
  238. }
  239. }
  240. const root = nodeOps.createElement('div')
  241. render(h(Comp), root)
  242. function assertCall(spy: jest.Mock, callIndex: number, args: any[]) {
  243. expect(spy.mock.calls[callIndex].slice(0, 2)).toMatchObject(args)
  244. expect(spy).toHaveReturnedWith(ctx)
  245. }
  246. ctx.foo++
  247. await nextTick()
  248. expect(spyA).toHaveBeenCalledTimes(1)
  249. assertCall(spyA, 0, [2, 1])
  250. ctx.bar++
  251. await nextTick()
  252. expect(spyB).toHaveBeenCalledTimes(1)
  253. assertCall(spyB, 0, [3, 2])
  254. ctx.baz.qux++
  255. await nextTick()
  256. expect(spyC).toHaveBeenCalledTimes(1)
  257. // new and old objects have same identity
  258. assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
  259. })
  260. // #3966
  261. test('watch merging from mixins', async () => {
  262. const mixinA = {
  263. data() {
  264. return {
  265. fromMixinA: ''
  266. }
  267. },
  268. watch: {
  269. obj: {
  270. handler(this: any, to: any) {
  271. this.fromMixinA = to
  272. }
  273. }
  274. }
  275. }
  276. const mixinB = {
  277. data() {
  278. return {
  279. fromMixinB: ''
  280. }
  281. },
  282. watch: {
  283. obj: 'setMixinB'
  284. },
  285. methods: {
  286. setMixinB(this: any, to: any) {
  287. this.fromMixinB = to
  288. }
  289. }
  290. }
  291. let vm: any
  292. const Comp = {
  293. render() {},
  294. mixins: [mixinA, mixinB],
  295. data: () => ({
  296. obj: 'foo',
  297. fromComp: ''
  298. }),
  299. watch: {
  300. obj(this: any, to: any) {
  301. this.fromComp = to
  302. }
  303. },
  304. mounted() {
  305. vm = this
  306. }
  307. }
  308. const root = nodeOps.createElement('div')
  309. render(h(Comp), root)
  310. vm.obj = 'bar'
  311. await nextTick()
  312. expect(vm.fromComp).toBe('bar')
  313. expect(vm.fromMixinA).toBe('bar')
  314. expect(vm.fromMixinB).toBe('bar')
  315. })
  316. test('provide/inject', () => {
  317. const symbolKey = Symbol()
  318. const Root = defineComponent({
  319. data() {
  320. return {
  321. a: 1
  322. }
  323. },
  324. provide() {
  325. return {
  326. a: this.a,
  327. [symbolKey]: 2
  328. }
  329. },
  330. render() {
  331. return [
  332. h(ChildA),
  333. h(ChildB),
  334. h(ChildC),
  335. h(ChildD),
  336. h(ChildE),
  337. h(ChildF),
  338. h(ChildG),
  339. h(ChildH),
  340. h(ChildI),
  341. h(ChildJ)
  342. ]
  343. }
  344. })
  345. const defineChild = (injectOptions: any, injectedKey = 'b') =>
  346. ({
  347. inject: injectOptions,
  348. render() {
  349. return this[injectedKey]
  350. }
  351. } as any)
  352. const ChildA = defineChild(['a'], 'a')
  353. const ChildB = defineChild({ b: 'a' })
  354. const ChildC = defineChild({
  355. b: {
  356. from: 'a'
  357. }
  358. })
  359. const ChildD = defineChild(
  360. {
  361. a: {
  362. default: () => 0
  363. }
  364. },
  365. 'a'
  366. )
  367. const ChildE = defineChild({
  368. b: {
  369. from: 'c',
  370. default: 2
  371. }
  372. })
  373. const ChildF = defineChild({
  374. b: {
  375. from: 'c',
  376. default: () => 3
  377. }
  378. })
  379. const ChildG = defineChild({
  380. b: {
  381. default: 4
  382. }
  383. })
  384. const ChildH = defineChild({
  385. b: {
  386. default: () => 5
  387. }
  388. })
  389. const ChildI = defineChild({
  390. b: symbolKey
  391. })
  392. const ChildJ = defineChild({
  393. b: {
  394. from: symbolKey
  395. }
  396. })
  397. expect(renderToString(h(Root))).toBe(`1111234522`)
  398. })
  399. test('provide/inject refs', async () => {
  400. const n = ref(0)
  401. const np = computed(() => n.value + 1)
  402. const Parent = defineComponent({
  403. provide() {
  404. return {
  405. n,
  406. np
  407. }
  408. },
  409. render: () => h(Child)
  410. })
  411. const Child = defineComponent({
  412. inject: ['n', 'np'],
  413. render(this: any) {
  414. return this.n + this.np
  415. }
  416. })
  417. const app = createApp(Parent)
  418. // TODO remove in 3.3
  419. app.config.unwrapInjectedRef = true
  420. const root = nodeOps.createElement('div')
  421. app.mount(root)
  422. expect(serializeInner(root)).toBe(`1`)
  423. n.value++
  424. await nextTick()
  425. expect(serializeInner(root)).toBe(`3`)
  426. })
  427. // TODO remove in 3.3
  428. test('provide/inject refs (compat)', async () => {
  429. const n = ref(0)
  430. const np = computed(() => n.value + 1)
  431. const Parent = defineComponent({
  432. provide() {
  433. return {
  434. n,
  435. np
  436. }
  437. },
  438. render: () => h(Child)
  439. })
  440. const Child = defineComponent({
  441. inject: ['n', 'np'],
  442. render(this: any) {
  443. return this.n.value + this.np.value
  444. }
  445. })
  446. const app = createApp(Parent)
  447. const root = nodeOps.createElement('div')
  448. app.mount(root)
  449. expect(serializeInner(root)).toBe(`1`)
  450. n.value++
  451. await nextTick()
  452. expect(serializeInner(root)).toBe(`3`)
  453. expect(`injected property "n" is a ref`).toHaveBeenWarned()
  454. expect(`injected property "np" is a ref`).toHaveBeenWarned()
  455. })
  456. test('provide accessing data in extends', () => {
  457. const Base = defineComponent({
  458. data() {
  459. return {
  460. a: 1
  461. }
  462. },
  463. provide() {
  464. return {
  465. a: this.a
  466. }
  467. }
  468. })
  469. const Child = {
  470. inject: ['a'],
  471. render() {
  472. return (this as any).a
  473. }
  474. }
  475. const Root = defineComponent({
  476. extends: Base,
  477. render() {
  478. return h(Child)
  479. }
  480. })
  481. expect(renderToString(h(Root))).toBe(`1`)
  482. })
  483. test('lifecycle', async () => {
  484. const count = ref(0)
  485. const root = nodeOps.createElement('div')
  486. const calls: string[] = []
  487. const Root = {
  488. beforeCreate() {
  489. calls.push('root beforeCreate')
  490. },
  491. created() {
  492. calls.push('root created')
  493. },
  494. beforeMount() {
  495. calls.push('root onBeforeMount')
  496. },
  497. mounted() {
  498. calls.push('root onMounted')
  499. },
  500. beforeUpdate() {
  501. calls.push('root onBeforeUpdate')
  502. },
  503. updated() {
  504. calls.push('root onUpdated')
  505. },
  506. beforeUnmount() {
  507. calls.push('root onBeforeUnmount')
  508. },
  509. unmounted() {
  510. calls.push('root onUnmounted')
  511. },
  512. render() {
  513. return h(Mid, { count: count.value })
  514. }
  515. }
  516. const Mid = {
  517. beforeCreate() {
  518. calls.push('mid beforeCreate')
  519. },
  520. created() {
  521. calls.push('mid created')
  522. },
  523. beforeMount() {
  524. calls.push('mid onBeforeMount')
  525. },
  526. mounted() {
  527. calls.push('mid onMounted')
  528. },
  529. beforeUpdate() {
  530. calls.push('mid onBeforeUpdate')
  531. },
  532. updated() {
  533. calls.push('mid onUpdated')
  534. },
  535. beforeUnmount() {
  536. calls.push('mid onBeforeUnmount')
  537. },
  538. unmounted() {
  539. calls.push('mid onUnmounted')
  540. },
  541. render(this: any) {
  542. return h(Child, { count: this.$props.count })
  543. }
  544. }
  545. const Child = {
  546. beforeCreate() {
  547. calls.push('child beforeCreate')
  548. },
  549. created() {
  550. calls.push('child created')
  551. },
  552. beforeMount() {
  553. calls.push('child onBeforeMount')
  554. },
  555. mounted() {
  556. calls.push('child onMounted')
  557. },
  558. beforeUpdate() {
  559. calls.push('child onBeforeUpdate')
  560. },
  561. updated() {
  562. calls.push('child onUpdated')
  563. },
  564. beforeUnmount() {
  565. calls.push('child onBeforeUnmount')
  566. },
  567. unmounted() {
  568. calls.push('child onUnmounted')
  569. },
  570. render(this: any) {
  571. return h('div', this.$props.count)
  572. }
  573. }
  574. // mount
  575. render(h(Root), root)
  576. expect(calls).toEqual([
  577. 'root beforeCreate',
  578. 'root created',
  579. 'root onBeforeMount',
  580. 'mid beforeCreate',
  581. 'mid created',
  582. 'mid onBeforeMount',
  583. 'child beforeCreate',
  584. 'child created',
  585. 'child onBeforeMount',
  586. 'child onMounted',
  587. 'mid onMounted',
  588. 'root onMounted'
  589. ])
  590. calls.length = 0
  591. // update
  592. count.value++
  593. await nextTick()
  594. expect(calls).toEqual([
  595. 'root onBeforeUpdate',
  596. 'mid onBeforeUpdate',
  597. 'child onBeforeUpdate',
  598. 'child onUpdated',
  599. 'mid onUpdated',
  600. 'root onUpdated'
  601. ])
  602. calls.length = 0
  603. // unmount
  604. render(null, root)
  605. expect(calls).toEqual([
  606. 'root onBeforeUnmount',
  607. 'mid onBeforeUnmount',
  608. 'child onBeforeUnmount',
  609. 'child onUnmounted',
  610. 'mid onUnmounted',
  611. 'root onUnmounted'
  612. ])
  613. })
  614. test('mixins', () => {
  615. const calls: string[] = []
  616. const mixinA = {
  617. data() {
  618. return {
  619. a: 1
  620. }
  621. },
  622. created(this: any) {
  623. calls.push('mixinA created')
  624. expect(this.a).toBe(1)
  625. expect(this.b).toBe(2)
  626. expect(this.c).toBe(4)
  627. },
  628. mounted() {
  629. calls.push('mixinA mounted')
  630. }
  631. }
  632. const mixinB = {
  633. props: {
  634. bP: {
  635. type: String
  636. }
  637. },
  638. data() {
  639. return {
  640. b: 2
  641. }
  642. },
  643. created(this: any) {
  644. calls.push('mixinB created')
  645. expect(this.a).toBe(1)
  646. expect(this.b).toBe(2)
  647. expect(this.bP).toBeUndefined()
  648. expect(this.c).toBe(4)
  649. expect(this.cP1).toBeUndefined()
  650. },
  651. mounted() {
  652. calls.push('mixinB mounted')
  653. }
  654. }
  655. const mixinC = defineComponent({
  656. props: ['cP1', 'cP2'],
  657. data() {
  658. return {
  659. c: 3
  660. }
  661. },
  662. created() {
  663. calls.push('mixinC created')
  664. // component data() should overwrite mixin field with same key
  665. expect(this.c).toBe(4)
  666. expect(this.cP1).toBeUndefined()
  667. },
  668. mounted() {
  669. calls.push('mixinC mounted')
  670. }
  671. })
  672. const Comp = defineComponent({
  673. props: {
  674. aaa: String
  675. },
  676. mixins: [defineComponent(mixinA), defineComponent(mixinB), mixinC],
  677. data() {
  678. return {
  679. c: 4,
  680. z: 4
  681. }
  682. },
  683. created() {
  684. calls.push('comp created')
  685. expect(this.a).toBe(1)
  686. expect(this.b).toBe(2)
  687. expect(this.bP).toBeUndefined()
  688. expect(this.c).toBe(4)
  689. expect(this.cP2).toBeUndefined()
  690. expect(this.z).toBe(4)
  691. },
  692. mounted() {
  693. calls.push('comp mounted')
  694. },
  695. render() {
  696. return `${this.a}${this.b}${this.c}`
  697. }
  698. })
  699. expect(renderToString(h(Comp))).toBe(`124`)
  700. expect(calls).toEqual([
  701. 'mixinA created',
  702. 'mixinB created',
  703. 'mixinC created',
  704. 'comp created',
  705. 'mixinA mounted',
  706. 'mixinB mounted',
  707. 'mixinC mounted',
  708. 'comp mounted'
  709. ])
  710. })
  711. test('render from mixin', () => {
  712. const Comp = {
  713. mixins: [
  714. {
  715. render: () => 'from mixin'
  716. }
  717. ]
  718. }
  719. expect(renderToString(h(Comp))).toBe('from mixin')
  720. })
  721. test('chained mixins in extends', () => {
  722. const calls: string[] = []
  723. const mixinA = {
  724. beforeCreate() {
  725. calls.push('mixinA beforeCreate')
  726. },
  727. created() {
  728. calls.push('mixinA created')
  729. }
  730. }
  731. const extendA = {
  732. mixins: [mixinA],
  733. beforeCreate() {
  734. calls.push('extendA beforeCreate')
  735. },
  736. created() {
  737. calls.push('extendA created')
  738. }
  739. }
  740. const Comp = {
  741. extends: extendA,
  742. render: () => '123',
  743. beforeCreate() {
  744. calls.push('self beforeCreate')
  745. },
  746. created() {
  747. calls.push('self created')
  748. }
  749. }
  750. expect(renderToString(h(Comp))).toBe(`123`)
  751. expect(calls).toEqual([
  752. 'mixinA beforeCreate',
  753. 'extendA beforeCreate',
  754. 'self beforeCreate',
  755. 'mixinA created',
  756. 'extendA created',
  757. 'self created'
  758. ])
  759. })
  760. test('chained extends in mixins', () => {
  761. const calls: string[] = []
  762. const extendA = {
  763. beforeCreate() {
  764. calls.push('extendA beforeCreate')
  765. },
  766. created() {
  767. calls.push('extendA created')
  768. }
  769. }
  770. const mixinA = {
  771. extends: extendA,
  772. beforeCreate() {
  773. calls.push('mixinA beforeCreate')
  774. },
  775. created() {
  776. calls.push('mixinA created')
  777. }
  778. }
  779. const Comp = {
  780. mixins: [mixinA],
  781. render: () => '123',
  782. beforeCreate() {
  783. calls.push('self beforeCreate')
  784. },
  785. created() {
  786. calls.push('self created')
  787. }
  788. }
  789. expect(renderToString(h(Comp))).toBe(`123`)
  790. expect(calls).toEqual([
  791. 'extendA beforeCreate',
  792. 'mixinA beforeCreate',
  793. 'self beforeCreate',
  794. 'extendA created',
  795. 'mixinA created',
  796. 'self created'
  797. ])
  798. })
  799. test('extends', () => {
  800. const calls: string[] = []
  801. const Base = {
  802. data() {
  803. return {
  804. a: 1,
  805. b: 1
  806. }
  807. },
  808. methods: {
  809. sayA() {}
  810. },
  811. mounted(this: any) {
  812. expect(this.a).toBe(1)
  813. expect(this.b).toBe(2)
  814. calls.push('base')
  815. }
  816. }
  817. const Comp = defineComponent({
  818. extends: defineComponent(Base),
  819. data() {
  820. return {
  821. b: 2
  822. }
  823. },
  824. mounted() {
  825. calls.push('comp')
  826. },
  827. render() {
  828. return `${this.a}${this.b}`
  829. }
  830. })
  831. expect(renderToString(h(Comp))).toBe(`12`)
  832. expect(calls).toEqual(['base', 'comp'])
  833. })
  834. test('extends with mixins', () => {
  835. const calls: string[] = []
  836. const Base = {
  837. data() {
  838. return {
  839. a: 1,
  840. x: 'base'
  841. }
  842. },
  843. methods: {
  844. sayA() {}
  845. },
  846. mounted(this: any) {
  847. expect(this.a).toBe(1)
  848. expect(this.b).toBeTruthy()
  849. expect(this.c).toBe(2)
  850. calls.push('base')
  851. }
  852. }
  853. const Mixin = {
  854. data() {
  855. return {
  856. b: true,
  857. x: 'mixin'
  858. }
  859. },
  860. mounted(this: any) {
  861. expect(this.a).toBe(1)
  862. expect(this.b).toBeTruthy()
  863. expect(this.c).toBe(2)
  864. calls.push('mixin')
  865. }
  866. }
  867. const Comp = defineComponent({
  868. extends: defineComponent(Base),
  869. mixins: [defineComponent(Mixin)],
  870. data() {
  871. return {
  872. c: 2
  873. }
  874. },
  875. mounted() {
  876. calls.push('comp')
  877. },
  878. render() {
  879. return `${this.a}${this.b}${this.c}${this.x}`
  880. }
  881. })
  882. expect(renderToString(h(Comp))).toBe(`1true2mixin`)
  883. expect(calls).toEqual(['base', 'mixin', 'comp'])
  884. })
  885. test('beforeCreate/created in extends and mixins', () => {
  886. const calls: string[] = []
  887. const BaseA = {
  888. beforeCreate() {
  889. calls.push('beforeCreateA')
  890. },
  891. created() {
  892. calls.push('createdA')
  893. }
  894. }
  895. const BaseB = {
  896. extends: BaseA,
  897. beforeCreate() {
  898. calls.push('beforeCreateB')
  899. },
  900. created() {
  901. calls.push('createdB')
  902. }
  903. }
  904. const MixinA = {
  905. beforeCreate() {
  906. calls.push('beforeCreateC')
  907. },
  908. created() {
  909. calls.push('createdC')
  910. }
  911. }
  912. const MixinB = {
  913. mixins: [MixinA],
  914. beforeCreate() {
  915. calls.push('beforeCreateD')
  916. },
  917. created() {
  918. calls.push('createdD')
  919. }
  920. }
  921. const Comp = {
  922. extends: BaseB,
  923. mixins: [MixinB],
  924. beforeCreate() {
  925. calls.push('selfBeforeCreate')
  926. },
  927. created() {
  928. calls.push('selfCreated')
  929. },
  930. render() {}
  931. }
  932. renderToString(h(Comp))
  933. expect(calls).toEqual([
  934. 'beforeCreateA',
  935. 'beforeCreateB',
  936. 'beforeCreateC',
  937. 'beforeCreateD',
  938. 'selfBeforeCreate',
  939. 'createdA',
  940. 'createdB',
  941. 'createdC',
  942. 'createdD',
  943. 'selfCreated'
  944. ])
  945. })
  946. test('flatten merged options', async () => {
  947. const MixinBase = {
  948. msg1: 'base'
  949. }
  950. const ExtendsBase = {
  951. msg2: 'base'
  952. }
  953. const Mixin = {
  954. mixins: [MixinBase]
  955. }
  956. const Extends = {
  957. extends: ExtendsBase
  958. }
  959. const Comp = defineComponent({
  960. extends: defineComponent(Extends),
  961. mixins: [defineComponent(Mixin)],
  962. render() {
  963. return `${this.$options.msg1},${this.$options.msg2}`
  964. }
  965. })
  966. expect(renderToString(h(Comp))).toBe('base,base')
  967. })
  968. test('extends template', () => {
  969. const Comp = {
  970. extends: {
  971. template: `<h1>Foo</h1>`
  972. }
  973. }
  974. const root = document.createElement('div') as any
  975. domRender(h(Comp), root)
  976. expect(root.innerHTML).toBe(`<h1>Foo</h1>`)
  977. })
  978. test('options defined in component have higher priority', async () => {
  979. const Mixin = {
  980. msg1: 'base'
  981. }
  982. const Extends = {
  983. msg2: 'base'
  984. }
  985. const Comp = defineComponent({
  986. msg1: 'local',
  987. msg2: 'local',
  988. extends: defineComponent(Extends),
  989. mixins: [defineComponent(Mixin)],
  990. render() {
  991. return `${this.$options.msg1},${this.$options.msg2}`
  992. }
  993. })
  994. expect(renderToString(h(Comp))).toBe('local,local')
  995. })
  996. test('accessing setup() state from options', async () => {
  997. const Comp = defineComponent({
  998. setup() {
  999. return {
  1000. count: ref(0)
  1001. }
  1002. },
  1003. data() {
  1004. return {
  1005. plusOne: (this as any).count + 1
  1006. }
  1007. },
  1008. computed: {
  1009. plusTwo(): number {
  1010. return this.count + 2
  1011. }
  1012. },
  1013. methods: {
  1014. inc() {
  1015. this.count++
  1016. }
  1017. },
  1018. render() {
  1019. return h(
  1020. 'div',
  1021. {
  1022. onClick: this.inc
  1023. },
  1024. `${this.count},${this.plusOne},${this.plusTwo}`
  1025. )
  1026. }
  1027. })
  1028. const root = nodeOps.createElement('div')
  1029. render(h(Comp), root)
  1030. expect(serializeInner(root)).toBe(`<div>0,1,2</div>`)
  1031. triggerEvent(root.children[0] as TestElement, 'click')
  1032. await nextTick()
  1033. expect(serializeInner(root)).toBe(`<div>1,1,3</div>`)
  1034. })
  1035. // #1016
  1036. test('watcher initialization should be deferred in mixins', async () => {
  1037. const mixin1 = {
  1038. data() {
  1039. return {
  1040. mixin1Data: 'mixin1'
  1041. }
  1042. },
  1043. methods: {}
  1044. }
  1045. const watchSpy = jest.fn()
  1046. const mixin2 = {
  1047. watch: {
  1048. mixin3Data: watchSpy
  1049. }
  1050. }
  1051. const mixin3 = {
  1052. data() {
  1053. return {
  1054. mixin3Data: 'mixin3'
  1055. }
  1056. },
  1057. methods: {}
  1058. }
  1059. let vm: any
  1060. const Comp = {
  1061. mixins: [mixin1, mixin2, mixin3],
  1062. render() {},
  1063. created() {
  1064. vm = this
  1065. }
  1066. }
  1067. const root = nodeOps.createElement('div')
  1068. render(h(Comp), root)
  1069. // should have no warnings
  1070. vm.mixin3Data = 'hello'
  1071. await nextTick()
  1072. expect(watchSpy.mock.calls[0].slice(0, 2)).toEqual(['hello', 'mixin3'])
  1073. })
  1074. test('injection from closest ancestor', () => {
  1075. const Root = defineComponent({
  1076. provide: {
  1077. a: 'root'
  1078. },
  1079. render() {
  1080. return [h(Mid), ' ', h(MidWithProvide), ' ', h(MidWithMixinProvide)]
  1081. }
  1082. })
  1083. const Mid = {
  1084. render() {
  1085. return h(Child)
  1086. }
  1087. } as any
  1088. const MidWithProvide = {
  1089. provide: {
  1090. a: 'midWithProvide'
  1091. },
  1092. render() {
  1093. return h(Child)
  1094. }
  1095. } as any
  1096. const mixin = {
  1097. provide: {
  1098. a: 'midWithMixinProvide'
  1099. }
  1100. }
  1101. const MidWithMixinProvide = {
  1102. mixins: [mixin],
  1103. render() {
  1104. return h(Child)
  1105. }
  1106. } as any
  1107. const Child = {
  1108. inject: ['a'],
  1109. render() {
  1110. return this.a
  1111. }
  1112. } as any
  1113. expect(renderToString(h(Root))).toBe(
  1114. 'root midWithProvide midWithMixinProvide'
  1115. )
  1116. })
  1117. describe('options merge strategies', () => {
  1118. test('this.$options.data', () => {
  1119. const mixin = {
  1120. data() {
  1121. return { foo: 1, bar: 2 }
  1122. }
  1123. }
  1124. createApp({
  1125. mixins: [mixin],
  1126. data() {
  1127. return {
  1128. foo: 3,
  1129. baz: 4
  1130. }
  1131. },
  1132. created() {
  1133. expect(this.$options.data).toBeInstanceOf(Function)
  1134. expect(this.$options.data()).toEqual({
  1135. foo: 3,
  1136. bar: 2,
  1137. baz: 4
  1138. })
  1139. },
  1140. render: () => null
  1141. }).mount(nodeOps.createElement('div'))
  1142. })
  1143. test('this.$options.inject', () => {
  1144. const mixin = {
  1145. inject: ['a']
  1146. }
  1147. const app = createApp({
  1148. mixins: [mixin],
  1149. inject: { b: 'b', c: { from: 'd' } },
  1150. created() {
  1151. expect(this.$options.inject.a).toEqual('a')
  1152. expect(this.$options.inject.b).toEqual('b')
  1153. expect(this.$options.inject.c).toEqual({ from: 'd' })
  1154. expect(this.a).toBe(1)
  1155. expect(this.b).toBe(2)
  1156. expect(this.c).toBe(3)
  1157. },
  1158. render: () => null
  1159. })
  1160. app.provide('a', 1)
  1161. app.provide('b', 2)
  1162. app.provide('d', 3)
  1163. app.mount(nodeOps.createElement('div'))
  1164. })
  1165. test('this.$options.provide', () => {
  1166. const mixin = {
  1167. provide: {
  1168. a: 1
  1169. }
  1170. }
  1171. createApp({
  1172. mixins: [mixin],
  1173. provide() {
  1174. return {
  1175. b: 2
  1176. }
  1177. },
  1178. created() {
  1179. expect(this.$options.provide).toBeInstanceOf(Function)
  1180. expect(this.$options.provide()).toEqual({ a: 1, b: 2 })
  1181. },
  1182. render: () => null
  1183. }).mount(nodeOps.createElement('div'))
  1184. })
  1185. test('this.$options[lifecycle-name]', () => {
  1186. const mixin = {
  1187. mounted() {},
  1188. beforeUnmount() {},
  1189. unmounted() {}
  1190. }
  1191. createApp({
  1192. mixins: [mixin],
  1193. mounted() {},
  1194. beforeUnmount() {},
  1195. unmounted() {},
  1196. created() {
  1197. expect(this.$options.mounted).toBeInstanceOf(Array)
  1198. expect(this.$options.mounted.length).toBe(2)
  1199. expect(this.$options.beforeUnmount).toBeInstanceOf(Array)
  1200. expect(this.$options.beforeUnmount.length).toBe(2)
  1201. expect(this.$options.unmounted).toBeInstanceOf(Array)
  1202. expect(this.$options.unmounted.length).toBe(2)
  1203. },
  1204. render: () => null
  1205. }).mount(nodeOps.createElement('div'))
  1206. })
  1207. test('this.$options[asset-name]', () => {
  1208. const mixin = {
  1209. components: {
  1210. a: {}
  1211. },
  1212. directives: {
  1213. d1: {}
  1214. }
  1215. }
  1216. createApp({
  1217. mixins: [mixin],
  1218. components: {
  1219. b: {}
  1220. },
  1221. directives: {
  1222. d2: {}
  1223. },
  1224. created() {
  1225. expect('a' in this.$options.components).toBe(true)
  1226. expect('b' in this.$options.components).toBe(true)
  1227. expect('d1' in this.$options.directives).toBe(true)
  1228. expect('d2' in this.$options.directives).toBe(true)
  1229. },
  1230. render: () => null
  1231. }).mount(nodeOps.createElement('div'))
  1232. })
  1233. test('this.$options.methods', () => {
  1234. const mixin = {
  1235. methods: {
  1236. fn1() {}
  1237. }
  1238. }
  1239. createApp({
  1240. mixins: [mixin],
  1241. methods: {
  1242. fn2() {}
  1243. },
  1244. created() {
  1245. expect(this.$options.methods.fn1).toBeInstanceOf(Function)
  1246. expect(this.$options.methods.fn2).toBeInstanceOf(Function)
  1247. },
  1248. render: () => null
  1249. }).mount(nodeOps.createElement('div'))
  1250. })
  1251. test('this.$options.computed', () => {
  1252. const mixin = {
  1253. computed: {
  1254. c1() {}
  1255. }
  1256. }
  1257. createApp({
  1258. mixins: [mixin],
  1259. computed: {
  1260. c2() {}
  1261. },
  1262. created() {
  1263. expect(this.$options.computed.c1).toBeInstanceOf(Function)
  1264. expect(this.$options.computed.c2).toBeInstanceOf(Function)
  1265. },
  1266. render: () => null
  1267. }).mount(nodeOps.createElement('div'))
  1268. })
  1269. // #2791
  1270. test('modify $options in the beforeCreate hook', async () => {
  1271. const count = ref(0)
  1272. const mixin = {
  1273. data() {
  1274. return { foo: 1 }
  1275. },
  1276. beforeCreate(this: any) {
  1277. if (!this.$options.computed) {
  1278. this.$options.computed = {}
  1279. }
  1280. this.$options.computed.value = () => count.value
  1281. }
  1282. }
  1283. const root = nodeOps.createElement('div')
  1284. createApp({
  1285. mixins: [mixin],
  1286. render(this: any) {
  1287. return this.value
  1288. }
  1289. }).mount(root)
  1290. expect(serializeInner(root)).toBe('0')
  1291. count.value++
  1292. await nextTick()
  1293. expect(serializeInner(root)).toBe('1')
  1294. })
  1295. })
  1296. describe('warnings', () => {
  1297. test('Expected a function as watch handler', () => {
  1298. const Comp = {
  1299. watch: {
  1300. foo: 'notExistingMethod',
  1301. foo2: {
  1302. handler: 'notExistingMethod2'
  1303. }
  1304. },
  1305. render() {}
  1306. }
  1307. const root = nodeOps.createElement('div')
  1308. render(h(Comp), root)
  1309. expect(
  1310. 'Invalid watch handler specified by key "notExistingMethod"'
  1311. ).toHaveBeenWarned()
  1312. expect(
  1313. 'Invalid watch handler specified by key "notExistingMethod2"'
  1314. ).toHaveBeenWarned()
  1315. })
  1316. test('Invalid watch option', () => {
  1317. const Comp = {
  1318. watch: { foo: true },
  1319. render() {}
  1320. }
  1321. const root = nodeOps.createElement('div')
  1322. // @ts-expect-error
  1323. render(h(Comp), root)
  1324. expect('Invalid watch option: "foo"').toHaveBeenWarned()
  1325. })
  1326. test('computed with setter and no getter', () => {
  1327. const Comp = {
  1328. computed: {
  1329. foo: {
  1330. set() {}
  1331. }
  1332. },
  1333. render() {}
  1334. }
  1335. const root = nodeOps.createElement('div')
  1336. render(h(Comp), root)
  1337. expect('Computed property "foo" has no getter.').toHaveBeenWarned()
  1338. })
  1339. test('assigning to computed with no setter', () => {
  1340. let instance: any
  1341. const Comp = {
  1342. computed: {
  1343. foo: {
  1344. get() {}
  1345. }
  1346. },
  1347. mounted() {
  1348. instance = this
  1349. },
  1350. render() {}
  1351. }
  1352. const root = nodeOps.createElement('div')
  1353. render(h(Comp), root)
  1354. instance.foo = 1
  1355. expect(
  1356. 'Write operation failed: computed property "foo" is readonly'
  1357. ).toHaveBeenWarned()
  1358. })
  1359. test('inject property is already declared in props', () => {
  1360. const Comp = {
  1361. data() {
  1362. return {
  1363. a: 1
  1364. }
  1365. },
  1366. provide() {
  1367. return {
  1368. a: this.a
  1369. }
  1370. },
  1371. render() {
  1372. return [h(ChildA)]
  1373. }
  1374. } as any
  1375. const ChildA = {
  1376. props: { a: Number },
  1377. inject: ['a'],
  1378. render() {
  1379. return this.a
  1380. }
  1381. } as any
  1382. const root = nodeOps.createElement('div')
  1383. render(h(Comp), root)
  1384. expect(
  1385. `Inject property "a" is already defined in Props.`
  1386. ).toHaveBeenWarned()
  1387. })
  1388. test('methods property is not a function', () => {
  1389. const Comp = {
  1390. methods: {
  1391. foo: 1
  1392. },
  1393. render() {}
  1394. }
  1395. const root = nodeOps.createElement('div')
  1396. render(h(Comp), root)
  1397. expect(
  1398. `Method "foo" has type "number" in the component definition. ` +
  1399. `Did you reference the function correctly?`
  1400. ).toHaveBeenWarned()
  1401. })
  1402. test('methods property is already declared in props', () => {
  1403. const Comp = {
  1404. props: {
  1405. foo: Number
  1406. },
  1407. methods: {
  1408. foo() {}
  1409. },
  1410. render() {}
  1411. }
  1412. const root = nodeOps.createElement('div')
  1413. render(h(Comp), root)
  1414. expect(
  1415. `Methods property "foo" is already defined in Props.`
  1416. ).toHaveBeenWarned()
  1417. })
  1418. test('methods property is already declared in inject', () => {
  1419. const Comp = {
  1420. data() {
  1421. return {
  1422. a: 1
  1423. }
  1424. },
  1425. provide() {
  1426. return {
  1427. a: this.a
  1428. }
  1429. },
  1430. render() {
  1431. return [h(ChildA)]
  1432. }
  1433. } as any
  1434. const ChildA = {
  1435. methods: {
  1436. a: () => null
  1437. },
  1438. inject: ['a'],
  1439. render() {
  1440. return this.a
  1441. }
  1442. } as any
  1443. const root = nodeOps.createElement('div')
  1444. render(h(Comp), root)
  1445. expect(
  1446. `Methods property "a" is already defined in Inject.`
  1447. ).toHaveBeenWarned()
  1448. })
  1449. test('data property is already declared in props', () => {
  1450. const Comp = {
  1451. props: { foo: Number },
  1452. data: () => ({
  1453. foo: 1
  1454. }),
  1455. render() {}
  1456. }
  1457. const root = nodeOps.createElement('div')
  1458. render(h(Comp), root)
  1459. expect(
  1460. `Data property "foo" is already defined in Props.`
  1461. ).toHaveBeenWarned()
  1462. })
  1463. test('data property is already declared in inject', () => {
  1464. const Comp = {
  1465. data() {
  1466. return {
  1467. a: 1
  1468. }
  1469. },
  1470. provide() {
  1471. return {
  1472. a: this.a
  1473. }
  1474. },
  1475. render() {
  1476. return [h(ChildA)]
  1477. }
  1478. } as any
  1479. const ChildA = {
  1480. data() {
  1481. return {
  1482. a: 1
  1483. }
  1484. },
  1485. inject: ['a'],
  1486. render() {
  1487. return this.a
  1488. }
  1489. } as any
  1490. const root = nodeOps.createElement('div')
  1491. render(h(Comp), root)
  1492. expect(
  1493. `Data property "a" is already defined in Inject.`
  1494. ).toHaveBeenWarned()
  1495. })
  1496. test('data property is already declared in methods', () => {
  1497. const Comp = {
  1498. data: () => ({
  1499. foo: 1
  1500. }),
  1501. methods: {
  1502. foo() {}
  1503. },
  1504. render() {}
  1505. }
  1506. const root = nodeOps.createElement('div')
  1507. render(h(Comp), root)
  1508. expect(
  1509. `Data property "foo" is already defined in Methods.`
  1510. ).toHaveBeenWarned()
  1511. })
  1512. test('computed property is already declared in props', () => {
  1513. const Comp = {
  1514. props: { foo: Number },
  1515. computed: {
  1516. foo() {}
  1517. },
  1518. render() {}
  1519. }
  1520. const root = nodeOps.createElement('div')
  1521. render(h(Comp), root)
  1522. expect(
  1523. `Computed property "foo" is already defined in Props.`
  1524. ).toHaveBeenWarned()
  1525. })
  1526. test('computed property is already declared in inject', () => {
  1527. const Comp = {
  1528. data() {
  1529. return {
  1530. a: 1
  1531. }
  1532. },
  1533. provide() {
  1534. return {
  1535. a: this.a
  1536. }
  1537. },
  1538. render() {
  1539. return [h(ChildA)]
  1540. }
  1541. } as any
  1542. const ChildA = {
  1543. computed: {
  1544. a: {
  1545. get() {},
  1546. set() {}
  1547. }
  1548. },
  1549. inject: ['a'],
  1550. render() {
  1551. return this.a
  1552. }
  1553. } as any
  1554. const root = nodeOps.createElement('div')
  1555. render(h(Comp), root)
  1556. expect(
  1557. `Computed property "a" is already defined in Inject.`
  1558. ).toHaveBeenWarned()
  1559. })
  1560. test('computed property is already declared in methods', () => {
  1561. const Comp = {
  1562. computed: {
  1563. foo() {}
  1564. },
  1565. methods: {
  1566. foo() {}
  1567. },
  1568. render() {}
  1569. }
  1570. const root = nodeOps.createElement('div')
  1571. render(h(Comp), root)
  1572. expect(
  1573. `Computed property "foo" is already defined in Methods.`
  1574. ).toHaveBeenWarned()
  1575. })
  1576. test('computed property is already declared in data', () => {
  1577. const Comp = {
  1578. data: () => ({
  1579. foo: 1
  1580. }),
  1581. computed: {
  1582. foo() {}
  1583. },
  1584. render() {}
  1585. }
  1586. const root = nodeOps.createElement('div')
  1587. render(h(Comp), root)
  1588. expect(
  1589. `Computed property "foo" is already defined in Data.`
  1590. ).toHaveBeenWarned()
  1591. })
  1592. })
  1593. })