inject.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. import Vue from 'vue'
  2. import { Observer } from 'core/observer/index'
  3. import { isNative, isObject, hasOwn, nextTick } from 'core/util/index'
  4. import testObjectOption from '../../../helpers/test-object-option'
  5. describe('Options provide/inject', () => {
  6. testObjectOption('inject')
  7. let injected
  8. const injectedComp = {
  9. inject: ['foo', 'bar'],
  10. render() {},
  11. created() {
  12. injected = [this.foo, this.bar]
  13. }
  14. }
  15. beforeEach(() => {
  16. injected = null
  17. })
  18. it('should work', () => {
  19. new Vue({
  20. template: `<child/>`,
  21. provide: {
  22. foo: 1,
  23. bar: false
  24. },
  25. components: {
  26. child: {
  27. template: `<injected-comp/>`,
  28. components: {
  29. injectedComp
  30. }
  31. }
  32. }
  33. }).$mount()
  34. expect(injected).toEqual([1, false])
  35. })
  36. it('should use closest parent', () => {
  37. new Vue({
  38. template: `<child/>`,
  39. provide: {
  40. foo: 1,
  41. bar: null
  42. },
  43. components: {
  44. child: {
  45. provide: {
  46. foo: 3
  47. },
  48. template: `<injected-comp/>`,
  49. components: {
  50. injectedComp
  51. }
  52. }
  53. }
  54. }).$mount()
  55. expect(injected).toEqual([3, null])
  56. })
  57. it('provide function', () => {
  58. new Vue({
  59. template: `<child/>`,
  60. data: {
  61. a: 1,
  62. b: false
  63. },
  64. provide() {
  65. return {
  66. foo: this.a,
  67. bar: this.b
  68. }
  69. },
  70. components: {
  71. child: {
  72. template: `<injected-comp/>`,
  73. components: {
  74. injectedComp
  75. }
  76. }
  77. }
  78. }).$mount()
  79. expect(injected).toEqual([1, false])
  80. })
  81. it('inject with alias', () => {
  82. const injectAlias = {
  83. inject: {
  84. baz: 'foo',
  85. qux: 'bar'
  86. },
  87. render() {},
  88. created() {
  89. injected = [this.baz, this.qux]
  90. }
  91. }
  92. new Vue({
  93. template: `<child/>`,
  94. provide: {
  95. foo: false,
  96. bar: 2
  97. },
  98. components: {
  99. child: {
  100. template: `<inject-alias/>`,
  101. components: {
  102. injectAlias
  103. }
  104. }
  105. }
  106. }).$mount()
  107. expect(injected).toEqual([false, 2])
  108. })
  109. it('inject before resolving data/props', () => {
  110. const vm = new Vue({
  111. provide: {
  112. foo: 1
  113. }
  114. })
  115. const child = new Vue({
  116. parent: vm,
  117. inject: ['foo'],
  118. data() {
  119. return {
  120. bar: this.foo + 1
  121. }
  122. },
  123. props: {
  124. baz: {
  125. default() {
  126. return this.foo + 2
  127. }
  128. }
  129. }
  130. })
  131. expect(child.foo).toBe(1)
  132. expect(child.bar).toBe(2)
  133. expect(child.baz).toBe(3)
  134. })
  135. // GitHub issue #5194
  136. it('should work with functional', () => {
  137. new Vue({
  138. template: `<child/>`,
  139. provide: {
  140. foo: 1,
  141. bar: false
  142. },
  143. components: {
  144. child: {
  145. functional: true,
  146. inject: ['foo', 'bar'],
  147. render(h, context) {
  148. const { injections } = context
  149. injected = [injections.foo, injections.bar]
  150. }
  151. }
  152. }
  153. }).$mount()
  154. expect(injected).toEqual([1, false])
  155. })
  156. if (typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)) {
  157. it('with Symbol keys', () => {
  158. const s = Symbol()
  159. const vm = new Vue({
  160. template: `<child/>`,
  161. provide: {
  162. [s]: 123
  163. },
  164. components: {
  165. child: {
  166. inject: { s },
  167. template: `<div>{{ s }}</div>`
  168. }
  169. }
  170. }).$mount()
  171. expect(vm.$el.textContent).toBe('123')
  172. })
  173. it('should merge symbol provide from mixins (functions)', () => {
  174. const keyA = Symbol('foo')
  175. const keyB = Symbol('bar')
  176. const mixinA = { provide: () => ({ [keyA]: 'foo' }) }
  177. const mixinB = { provide: () => ({ [keyB]: 'bar' }) }
  178. const child = {
  179. inject: {
  180. foo: keyA,
  181. bar: keyB
  182. },
  183. template: `<span/>`,
  184. created() {
  185. injected = [this.foo, this.bar]
  186. }
  187. }
  188. new Vue({
  189. mixins: [mixinA, mixinB],
  190. render(h) {
  191. return h(child)
  192. }
  193. }).$mount()
  194. expect(injected).toEqual(['foo', 'bar'])
  195. })
  196. }
  197. // GitHub issue #5223
  198. it('should work with reactive array', done => {
  199. const vm = new Vue({
  200. template: `<div><child></child></div>`,
  201. data() {
  202. return {
  203. foo: []
  204. }
  205. },
  206. provide() {
  207. return {
  208. foo: this.foo
  209. }
  210. },
  211. components: {
  212. child: {
  213. inject: ['foo'],
  214. template: `<span>{{foo.length}}</span>`
  215. }
  216. }
  217. }).$mount()
  218. expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
  219. vm.foo.push(vm.foo.length)
  220. vm.$nextTick(() => {
  221. expect(vm.$el.innerHTML).toEqual(`<span>1</span>`)
  222. vm.foo.pop()
  223. vm.$nextTick(() => {
  224. expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
  225. done()
  226. })
  227. })
  228. })
  229. it('should extend properly', () => {
  230. const parent = Vue.extend({
  231. template: `<span/>`,
  232. inject: ['foo']
  233. })
  234. const child = parent.extend({
  235. template: `<span/>`,
  236. inject: ['bar'],
  237. created() {
  238. injected = [this.foo, this.bar]
  239. }
  240. })
  241. new Vue({
  242. template: `<div><parent/><child/></div>`,
  243. provide: {
  244. foo: 1,
  245. bar: false
  246. },
  247. components: {
  248. parent,
  249. child
  250. }
  251. }).$mount()
  252. expect(injected).toEqual([1, false])
  253. })
  254. it('should merge from mixins properly (objects)', () => {
  255. const mixinA = { inject: { foo: 'foo' } }
  256. const mixinB = { inject: { bar: 'bar' } }
  257. const child = {
  258. mixins: [mixinA, mixinB],
  259. template: `<span/>`,
  260. created() {
  261. injected = [this.foo, this.bar]
  262. }
  263. }
  264. new Vue({
  265. provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
  266. render(h) {
  267. return h(child)
  268. }
  269. }).$mount()
  270. expect(injected).toEqual(['foo', 'bar'])
  271. })
  272. it('should merge from mixins properly (arrays)', () => {
  273. const mixinA = { inject: ['foo'] }
  274. const mixinB = { inject: ['bar'] }
  275. const child = {
  276. mixins: [mixinA, mixinB],
  277. inject: ['baz'],
  278. template: `<span/>`,
  279. created() {
  280. injected = [this.foo, this.bar, this.baz]
  281. }
  282. }
  283. new Vue({
  284. provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
  285. render(h) {
  286. return h(child)
  287. }
  288. }).$mount()
  289. expect(injected).toEqual(['foo', 'bar', 'baz'])
  290. })
  291. it('should merge from mixins properly (mix of objects and arrays)', () => {
  292. const mixinA = { inject: { foo: 'foo' } }
  293. const mixinB = { inject: ['bar'] }
  294. const child = {
  295. mixins: [mixinA, mixinB],
  296. inject: { qux: 'baz' },
  297. template: `<span/>`,
  298. created() {
  299. injected = [this.foo, this.bar, this.qux]
  300. }
  301. }
  302. new Vue({
  303. provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
  304. render(h) {
  305. return h(child)
  306. }
  307. }).$mount()
  308. expect(injected).toEqual(['foo', 'bar', 'baz'])
  309. })
  310. it('should warn when injections has been modified', () => {
  311. const key = 'foo'
  312. const vm = new Vue({
  313. provide: {
  314. foo: 1
  315. }
  316. })
  317. const child = new Vue({
  318. parent: vm,
  319. inject: ['foo']
  320. })
  321. expect(child.foo).toBe(1)
  322. child.foo = 2
  323. expect(
  324. `Avoid mutating an injected value directly since the changes will be ` +
  325. `overwritten whenever the provided component re-renders. ` +
  326. `injection being mutated: "${key}"`
  327. ).toHaveBeenWarned()
  328. })
  329. it('should warn when injections cannot be found', () => {
  330. const vm = new Vue({})
  331. new Vue({
  332. parent: vm,
  333. inject: ['foo', 'bar'],
  334. created() {}
  335. })
  336. expect(`Injection "foo" not found`).toHaveBeenWarned()
  337. expect(`Injection "bar" not found`).toHaveBeenWarned()
  338. })
  339. it('should not warn when injections can be found', () => {
  340. const vm = new Vue({
  341. provide: {
  342. foo: 1,
  343. bar: false,
  344. baz: undefined
  345. }
  346. })
  347. new Vue({
  348. parent: vm,
  349. inject: ['foo', 'bar', 'baz'],
  350. created() {}
  351. })
  352. expect(`Injection "foo" not found`).not.toHaveBeenWarned()
  353. expect(`Injection "bar" not found`).not.toHaveBeenWarned()
  354. expect(`Injection "baz" not found`).not.toHaveBeenWarned()
  355. })
  356. it('should not warn when injection key which is not provided is not enumerable', () => {
  357. const parent = new Vue({ provide: { foo: 1 } })
  358. const inject = { foo: 'foo' }
  359. Object.defineProperty(inject, '__ob__', {
  360. enumerable: false,
  361. value: '__ob__'
  362. })
  363. new Vue({ parent, inject })
  364. expect(`Injection "__ob__" not found`).not.toHaveBeenWarned()
  365. })
  366. // Github issue #6097
  367. it('should not warn when injections cannot be found but have default value', () => {
  368. const vm = new Vue({})
  369. new Vue({
  370. parent: vm,
  371. inject: {
  372. foo: { default: 1 },
  373. bar: { default: false },
  374. baz: { default: undefined }
  375. },
  376. created() {
  377. injected = [this.foo, this.bar, this.baz]
  378. }
  379. })
  380. expect(injected).toEqual([1, false, undefined])
  381. })
  382. it('should support name alias and default together', () => {
  383. const vm = new Vue({
  384. provide: {
  385. FOO: 2
  386. }
  387. })
  388. new Vue({
  389. parent: vm,
  390. inject: {
  391. foo: { from: 'FOO', default: 1 },
  392. bar: { default: false },
  393. baz: { default: undefined }
  394. },
  395. created() {
  396. injected = [this.foo, this.bar, this.baz]
  397. }
  398. })
  399. expect(injected).toEqual([2, false, undefined])
  400. })
  401. it('should use provided value even if inject has default', () => {
  402. const vm = new Vue({
  403. provide: {
  404. foo: 1,
  405. bar: false,
  406. baz: undefined
  407. }
  408. })
  409. new Vue({
  410. parent: vm,
  411. inject: {
  412. foo: { default: 2 },
  413. bar: { default: 2 },
  414. baz: { default: 2 }
  415. },
  416. created() {
  417. injected = [this.foo, this.bar, this.baz]
  418. }
  419. })
  420. expect(injected).toEqual([1, false, undefined])
  421. })
  422. // Github issue #6008
  423. it('should merge provide from mixins (objects)', () => {
  424. const mixinA = { provide: { foo: 'foo' } }
  425. const mixinB = { provide: { bar: 'bar' } }
  426. const child = {
  427. inject: ['foo', 'bar'],
  428. template: `<span/>`,
  429. created() {
  430. injected = [this.foo, this.bar]
  431. }
  432. }
  433. new Vue({
  434. mixins: [mixinA, mixinB],
  435. render(h) {
  436. return h(child)
  437. }
  438. }).$mount()
  439. expect(injected).toEqual(['foo', 'bar'])
  440. })
  441. it('should merge provide from mixins (functions)', () => {
  442. const mixinA = { provide: () => ({ foo: 'foo' }) }
  443. const mixinB = { provide: () => ({ bar: 'bar' }) }
  444. const child = {
  445. inject: ['foo', 'bar'],
  446. template: `<span/>`,
  447. created() {
  448. injected = [this.foo, this.bar]
  449. }
  450. }
  451. new Vue({
  452. mixins: [mixinA, mixinB],
  453. render(h) {
  454. return h(child)
  455. }
  456. }).$mount()
  457. expect(injected).toEqual(['foo', 'bar'])
  458. })
  459. it('should merge provide from mixins (mix of objects and functions)', () => {
  460. const mixinA = { provide: { foo: 'foo' } }
  461. const mixinB = { provide: () => ({ bar: 'bar' }) }
  462. const mixinC = { provide: { baz: 'baz' } }
  463. const mixinD = { provide: () => ({ bam: 'bam' }) }
  464. const child = {
  465. inject: ['foo', 'bar', 'baz', 'bam'],
  466. template: `<span/>`,
  467. created() {
  468. injected = [this.foo, this.bar, this.baz, this.bam]
  469. }
  470. }
  471. new Vue({
  472. mixins: [mixinA, mixinB, mixinC, mixinD],
  473. render(h) {
  474. return h(child)
  475. }
  476. }).$mount()
  477. expect(injected).toEqual(['foo', 'bar', 'baz', 'bam'])
  478. })
  479. it('should merge provide from mixins and override existing keys', () => {
  480. const mixinA = { provide: { foo: 'foo' } }
  481. const mixinB = { provide: { foo: 'bar' } }
  482. const child = {
  483. inject: ['foo'],
  484. template: `<span/>`,
  485. created() {
  486. injected = [this.foo]
  487. }
  488. }
  489. new Vue({
  490. mixins: [mixinA, mixinB],
  491. render(h) {
  492. return h(child)
  493. }
  494. }).$mount()
  495. expect(injected).toEqual(['bar'])
  496. })
  497. it('should merge provide when Vue.extend', () => {
  498. const mixinA = { provide: () => ({ foo: 'foo' }) }
  499. const child = {
  500. inject: ['foo', 'bar'],
  501. template: `<span/>`,
  502. created() {
  503. injected = [this.foo, this.bar]
  504. }
  505. }
  506. const Ctor = Vue.extend({
  507. mixins: [mixinA],
  508. provide: { bar: 'bar' },
  509. render(h) {
  510. return h(child)
  511. }
  512. })
  513. new Ctor().$mount()
  514. expect(injected).toEqual(['foo', 'bar'])
  515. })
  516. // #5913
  517. it('should keep the reactive with provide', () => {
  518. function isObserver(obj) {
  519. if (isObject(obj)) {
  520. return hasOwn(obj, '__ob__') && obj.__ob__ instanceof Observer
  521. }
  522. return false
  523. }
  524. const vm = new Vue({
  525. template: `<div><child ref='child'></child></div>`,
  526. data() {
  527. return {
  528. foo: {},
  529. $foo: {},
  530. foo1: []
  531. }
  532. },
  533. provide() {
  534. return {
  535. foo: this.foo,
  536. $foo: this.$foo,
  537. foo1: this.foo1,
  538. bar: {},
  539. baz: []
  540. }
  541. },
  542. components: {
  543. child: {
  544. inject: ['foo', '$foo', 'foo1', 'bar', 'baz'],
  545. template: `<span/>`
  546. }
  547. }
  548. }).$mount()
  549. const child = vm.$refs.child
  550. expect(isObserver(child.foo)).toBe(true)
  551. expect(isObserver(child.$foo)).toBe(false)
  552. expect(isObserver(child.foo1)).toBe(true)
  553. expect(isObserver(child.bar)).toBe(false)
  554. expect(isObserver(child.baz)).toBe(false)
  555. })
  556. // #6175
  557. it('merge provide properly from mixins', () => {
  558. const ProvideFooMixin = {
  559. provide: {
  560. foo: 'foo injected'
  561. }
  562. }
  563. const ProvideBarMixin = {
  564. provide: {
  565. bar: 'bar injected'
  566. }
  567. }
  568. const Child = {
  569. inject: ['foo', 'bar'],
  570. render(h) {
  571. return h('div', [`foo: ${this.foo}, `, `bar: ${this.bar}`])
  572. }
  573. }
  574. const Parent = {
  575. mixins: [ProvideFooMixin, ProvideBarMixin],
  576. render(h) {
  577. return h(Child)
  578. }
  579. }
  580. const vm = new Vue({
  581. render(h) {
  582. return h(Parent)
  583. }
  584. }).$mount()
  585. expect(vm.$el.textContent).toBe(`foo: foo injected, bar: bar injected`)
  586. })
  587. it('merge provide with object syntax when using Vue.extend', () => {
  588. const child = {
  589. inject: ['foo'],
  590. template: `<span/>`,
  591. created() {
  592. injected = this.foo
  593. }
  594. }
  595. const Ctor = Vue.extend({
  596. provide: { foo: 'foo' },
  597. render(h) {
  598. return h(child)
  599. }
  600. })
  601. new Ctor().$mount()
  602. expect(injected).toEqual('foo')
  603. })
  604. // #7284
  605. it('should not inject prototype properties', () => {
  606. const vm = new Vue({
  607. provide: {}
  608. })
  609. new Vue({
  610. parent: vm,
  611. inject: ['constructor']
  612. })
  613. expect(`Injection "constructor" not found`).toHaveBeenWarned()
  614. })
  615. // #12667
  616. test('provide with getters', async () => {
  617. const spy = vi.fn()
  618. const Child = {
  619. render() {},
  620. inject: ['foo'],
  621. mounted() {
  622. spy(this.foo)
  623. }
  624. }
  625. let val = 1
  626. const vm = new Vue({
  627. components: { Child },
  628. template: `<Child v-if="ok" />`,
  629. data() {
  630. return {
  631. ok: false
  632. }
  633. },
  634. provide() {
  635. return {
  636. get foo() {
  637. return val
  638. }
  639. }
  640. }
  641. }).$mount()
  642. val = 2
  643. vm.ok = true
  644. await nextTick()
  645. expect(spy).toHaveBeenCalledWith(2)
  646. })
  647. // #12854
  648. test('should not mutate original provide options', () => {
  649. const hairMixin = { provide: { hair: 'red' } }
  650. const eyesMixin = { provide: { eyes: 'brown' } }
  651. new Vue({ mixins: [hairMixin, eyesMixin], render() {} }).$mount()
  652. expect(eyesMixin.provide).toStrictEqual({ eyes: 'brown' })
  653. })
  654. })