inject.spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. import Vue from 'vue'
  2. import { Observer } from 'core/observer/index'
  3. import { isNative, isObject, hasOwn } 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}"`).toHaveBeenWarned()
  327. })
  328. it('should warn when injections cannot be found', () => {
  329. const vm = new Vue({})
  330. new Vue({
  331. parent: vm,
  332. inject: ['foo', 'bar'],
  333. created () {}
  334. })
  335. expect(`Injection "foo" not found`).toHaveBeenWarned()
  336. expect(`Injection "bar" not found`).toHaveBeenWarned()
  337. })
  338. it('should not warn when injections can be found', () => {
  339. const vm = new Vue({
  340. provide: {
  341. foo: 1,
  342. bar: false,
  343. baz: undefined
  344. }
  345. })
  346. new Vue({
  347. parent: vm,
  348. inject: ['foo', 'bar', 'baz'],
  349. created () {}
  350. })
  351. expect(`Injection "foo" not found`).not.toHaveBeenWarned()
  352. expect(`Injection "bar" not found`).not.toHaveBeenWarned()
  353. expect(`Injection "baz" not found`).not.toHaveBeenWarned()
  354. })
  355. it('should not warn when injection key which is not provided is not enumerable', () => {
  356. const parent = new Vue({ provide: { foo: 1 }})
  357. const inject = { foo: 'foo' }
  358. Object.defineProperty(inject, '__ob__', { enumerable: false, value: '__ob__' })
  359. new Vue({ parent, inject })
  360. expect(`Injection "__ob__" not found`).not.toHaveBeenWarned()
  361. })
  362. // Github issue #6097
  363. it('should not warn when injections cannot be found but have default value', () => {
  364. const vm = new Vue({})
  365. new Vue({
  366. parent: vm,
  367. inject: {
  368. foo: { default: 1 },
  369. bar: { default: false },
  370. baz: { default: undefined }
  371. },
  372. created () {
  373. injected = [this.foo, this.bar, this.baz]
  374. }
  375. })
  376. expect(injected).toEqual([1, false, undefined])
  377. })
  378. it('should support name alias and default together', () => {
  379. const vm = new Vue({
  380. provide: {
  381. FOO: 2
  382. }
  383. })
  384. new Vue({
  385. parent: vm,
  386. inject: {
  387. foo: { from: 'FOO', default: 1 },
  388. bar: { default: false },
  389. baz: { default: undefined }
  390. },
  391. created () {
  392. injected = [this.foo, this.bar, this.baz]
  393. }
  394. })
  395. expect(injected).toEqual([2, false, undefined])
  396. })
  397. it('should use provided value even if inject has default', () => {
  398. const vm = new Vue({
  399. provide: {
  400. foo: 1,
  401. bar: false,
  402. baz: undefined
  403. }
  404. })
  405. new Vue({
  406. parent: vm,
  407. inject: {
  408. foo: { default: 2 },
  409. bar: { default: 2 },
  410. baz: { default: 2 }
  411. },
  412. created () {
  413. injected = [this.foo, this.bar, this.baz]
  414. }
  415. })
  416. expect(injected).toEqual([1, false, undefined])
  417. })
  418. // Github issue #6008
  419. it('should merge provide from mixins (objects)', () => {
  420. const mixinA = { provide: { foo: 'foo' }}
  421. const mixinB = { provide: { bar: 'bar' }}
  422. const child = {
  423. inject: ['foo', 'bar'],
  424. template: `<span/>`,
  425. created () {
  426. injected = [this.foo, this.bar]
  427. }
  428. }
  429. new Vue({
  430. mixins: [mixinA, mixinB],
  431. render (h) {
  432. return h(child)
  433. }
  434. }).$mount()
  435. expect(injected).toEqual(['foo', 'bar'])
  436. })
  437. it('should merge provide from mixins (functions)', () => {
  438. const mixinA = { provide: () => ({ foo: 'foo' }) }
  439. const mixinB = { provide: () => ({ bar: 'bar' }) }
  440. const child = {
  441. inject: ['foo', 'bar'],
  442. template: `<span/>`,
  443. created () {
  444. injected = [this.foo, this.bar]
  445. }
  446. }
  447. new Vue({
  448. mixins: [mixinA, mixinB],
  449. render (h) {
  450. return h(child)
  451. }
  452. }).$mount()
  453. expect(injected).toEqual(['foo', 'bar'])
  454. })
  455. it('should merge provide from mixins (mix of objects and functions)', () => {
  456. const mixinA = { provide: { foo: 'foo' }}
  457. const mixinB = { provide: () => ({ bar: 'bar' }) }
  458. const mixinC = { provide: { baz: 'baz' }}
  459. const mixinD = { provide: () => ({ bam: 'bam' }) }
  460. const child = {
  461. inject: ['foo', 'bar', 'baz', 'bam'],
  462. template: `<span/>`,
  463. created () {
  464. injected = [this.foo, this.bar, this.baz, this.bam]
  465. }
  466. }
  467. new Vue({
  468. mixins: [mixinA, mixinB, mixinC, mixinD],
  469. render (h) {
  470. return h(child)
  471. }
  472. }).$mount()
  473. expect(injected).toEqual(['foo', 'bar', 'baz', 'bam'])
  474. })
  475. it('should merge provide from mixins and override existing keys', () => {
  476. const mixinA = { provide: { foo: 'foo' }}
  477. const mixinB = { provide: { foo: 'bar' }}
  478. const child = {
  479. inject: ['foo'],
  480. template: `<span/>`,
  481. created () {
  482. injected = [this.foo]
  483. }
  484. }
  485. new Vue({
  486. mixins: [mixinA, mixinB],
  487. render (h) {
  488. return h(child)
  489. }
  490. }).$mount()
  491. expect(injected).toEqual(['bar'])
  492. })
  493. it('should merge provide when Vue.extend', () => {
  494. const mixinA = { provide: () => ({ foo: 'foo' }) }
  495. const child = {
  496. inject: ['foo', 'bar'],
  497. template: `<span/>`,
  498. created () {
  499. injected = [this.foo, this.bar]
  500. }
  501. }
  502. const Ctor = Vue.extend({
  503. mixins: [mixinA],
  504. provide: { bar: 'bar' },
  505. render (h) {
  506. return h(child)
  507. }
  508. })
  509. new Ctor().$mount()
  510. expect(injected).toEqual(['foo', 'bar'])
  511. })
  512. // #5913
  513. it('should keep the reactive with provide', () => {
  514. function isObserver (obj) {
  515. if (isObject(obj)) {
  516. return hasOwn(obj, '__ob__') && obj.__ob__ instanceof Observer
  517. }
  518. return false
  519. }
  520. const vm = new Vue({
  521. template: `<div><child ref='child'></child></div>`,
  522. data () {
  523. return {
  524. foo: {},
  525. $foo: {},
  526. foo1: []
  527. }
  528. },
  529. provide () {
  530. return {
  531. foo: this.foo,
  532. $foo: this.$foo,
  533. foo1: this.foo1,
  534. bar: {},
  535. baz: []
  536. }
  537. },
  538. components: {
  539. child: {
  540. inject: ['foo', '$foo', 'foo1', 'bar', 'baz'],
  541. template: `<span/>`
  542. }
  543. }
  544. }).$mount()
  545. const child = vm.$refs.child
  546. expect(isObserver(child.foo)).toBe(true)
  547. expect(isObserver(child.$foo)).toBe(false)
  548. expect(isObserver(child.foo1)).toBe(true)
  549. expect(isObserver(child.bar)).toBe(false)
  550. expect(isObserver(child.baz)).toBe(false)
  551. })
  552. // #6175
  553. it('merge provide properly from mixins', () => {
  554. const ProvideFooMixin = {
  555. provide: {
  556. foo: 'foo injected'
  557. }
  558. }
  559. const ProvideBarMixin = {
  560. provide: {
  561. bar: 'bar injected'
  562. }
  563. }
  564. const Child = {
  565. inject: ['foo', 'bar'],
  566. render (h) {
  567. return h('div', [`foo: ${this.foo}, `, `bar: ${this.bar}`])
  568. }
  569. }
  570. const Parent = {
  571. mixins: [ProvideFooMixin, ProvideBarMixin],
  572. render (h) {
  573. return h(Child)
  574. }
  575. }
  576. const vm = new Vue({
  577. render (h) {
  578. return h(Parent)
  579. }
  580. }).$mount()
  581. expect(vm.$el.textContent).toBe(`foo: foo injected, bar: bar injected`)
  582. })
  583. it('merge provide with object syntax when using Vue.extend', () => {
  584. const child = {
  585. inject: ['foo'],
  586. template: `<span/>`,
  587. created () {
  588. injected = this.foo
  589. }
  590. }
  591. const Ctor = Vue.extend({
  592. provide: { foo: 'foo' },
  593. render (h) {
  594. return h(child)
  595. }
  596. })
  597. new Ctor().$mount()
  598. expect(injected).toEqual('foo')
  599. })
  600. // #7284
  601. it('should not inject prototype properties', () => {
  602. const vm = new Vue({
  603. provide: {}
  604. })
  605. new Vue({
  606. parent: vm,
  607. inject: ['constructor']
  608. })
  609. expect(`Injection "constructor" not found`).toHaveBeenWarned()
  610. })
  611. })