inject.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. }
  174. // GitHub issue #5223
  175. it('should work with reactive array', done => {
  176. const vm = new Vue({
  177. template: `<div><child></child></div>`,
  178. data () {
  179. return {
  180. foo: []
  181. }
  182. },
  183. provide () {
  184. return {
  185. foo: this.foo
  186. }
  187. },
  188. components: {
  189. child: {
  190. inject: ['foo'],
  191. template: `<span>{{foo.length}}</span>`
  192. }
  193. }
  194. }).$mount()
  195. expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
  196. vm.foo.push(vm.foo.length)
  197. vm.$nextTick(() => {
  198. expect(vm.$el.innerHTML).toEqual(`<span>1</span>`)
  199. vm.foo.pop()
  200. vm.$nextTick(() => {
  201. expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
  202. done()
  203. })
  204. })
  205. })
  206. it('should extend properly', () => {
  207. const parent = Vue.extend({
  208. template: `<span/>`,
  209. inject: ['foo']
  210. })
  211. const child = parent.extend({
  212. template: `<span/>`,
  213. inject: ['bar'],
  214. created () {
  215. injected = [this.foo, this.bar]
  216. }
  217. })
  218. new Vue({
  219. template: `<div><parent/><child/></div>`,
  220. provide: {
  221. foo: 1,
  222. bar: false
  223. },
  224. components: {
  225. parent,
  226. child
  227. }
  228. }).$mount()
  229. expect(injected).toEqual([1, false])
  230. })
  231. it('should merge from mixins properly (objects)', () => {
  232. const mixinA = { inject: { foo: 'foo' }}
  233. const mixinB = { inject: { bar: 'bar' }}
  234. const child = {
  235. mixins: [mixinA, mixinB],
  236. template: `<span/>`,
  237. created () {
  238. injected = [this.foo, this.bar]
  239. }
  240. }
  241. new Vue({
  242. provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
  243. render (h) {
  244. return h(child)
  245. }
  246. }).$mount()
  247. expect(injected).toEqual(['foo', 'bar'])
  248. })
  249. it('should merge from mixins properly (arrays)', () => {
  250. const mixinA = { inject: ['foo'] }
  251. const mixinB = { inject: ['bar'] }
  252. const child = {
  253. mixins: [mixinA, mixinB],
  254. inject: ['baz'],
  255. template: `<span/>`,
  256. created () {
  257. injected = [this.foo, this.bar, this.baz]
  258. }
  259. }
  260. new Vue({
  261. provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
  262. render (h) {
  263. return h(child)
  264. }
  265. }).$mount()
  266. expect(injected).toEqual(['foo', 'bar', 'baz'])
  267. })
  268. it('should merge from mixins properly (mix of objects and arrays)', () => {
  269. const mixinA = { inject: { foo: 'foo' }}
  270. const mixinB = { inject: ['bar'] }
  271. const child = {
  272. mixins: [mixinA, mixinB],
  273. inject: { qux: 'baz' },
  274. template: `<span/>`,
  275. created () {
  276. injected = [this.foo, this.bar, this.qux]
  277. }
  278. }
  279. new Vue({
  280. provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
  281. render (h) {
  282. return h(child)
  283. }
  284. }).$mount()
  285. expect(injected).toEqual(['foo', 'bar', 'baz'])
  286. })
  287. it('should warn when injections has been modified', () => {
  288. const key = 'foo'
  289. const vm = new Vue({
  290. provide: {
  291. foo: 1
  292. }
  293. })
  294. const child = new Vue({
  295. parent: vm,
  296. inject: ['foo']
  297. })
  298. expect(child.foo).toBe(1)
  299. child.foo = 2
  300. expect(
  301. `Avoid mutating an injected value directly since the changes will be ` +
  302. `overwritten whenever the provided component re-renders. ` +
  303. `injection being mutated: "${key}"`).toHaveBeenWarned()
  304. })
  305. it('should warn when injections cannot be found', () => {
  306. const vm = new Vue({})
  307. new Vue({
  308. parent: vm,
  309. inject: ['foo', 'bar'],
  310. created () {}
  311. })
  312. expect(`Injection "foo" not found`).toHaveBeenWarned()
  313. expect(`Injection "bar" not found`).toHaveBeenWarned()
  314. })
  315. it('should not warn when injections can be found', () => {
  316. const vm = new Vue({
  317. provide: {
  318. foo: 1,
  319. bar: false,
  320. baz: undefined
  321. }
  322. })
  323. new Vue({
  324. parent: vm,
  325. inject: ['foo', 'bar', 'baz'],
  326. created () {}
  327. })
  328. expect(`Injection "foo" not found`).not.toHaveBeenWarned()
  329. expect(`Injection "bar" not found`).not.toHaveBeenWarned()
  330. expect(`Injection "baz" not found`).not.toHaveBeenWarned()
  331. })
  332. it('should not warn when injection key which is not provided is not enumerable', () => {
  333. const parent = new Vue({ provide: { foo: 1 }})
  334. const inject = { foo: 'foo' }
  335. Object.defineProperty(inject, '__ob__', { enumerable: false, value: '__ob__' })
  336. new Vue({ parent, inject })
  337. expect(`Injection "__ob__" not found`).not.toHaveBeenWarned()
  338. })
  339. // Github issue #6097
  340. it('should not warn when injections cannot be found but have default value', () => {
  341. const vm = new Vue({})
  342. new Vue({
  343. parent: vm,
  344. inject: {
  345. foo: { default: 1 },
  346. bar: { default: false },
  347. baz: { default: undefined }
  348. },
  349. created () {
  350. injected = [this.foo, this.bar, this.baz]
  351. }
  352. })
  353. expect(injected).toEqual([1, false, undefined])
  354. })
  355. it('should support name alias and default together', () => {
  356. const vm = new Vue({
  357. provide: {
  358. FOO: 2
  359. }
  360. })
  361. new Vue({
  362. parent: vm,
  363. inject: {
  364. foo: { from: 'FOO', default: 1 },
  365. bar: { default: false },
  366. baz: { default: undefined }
  367. },
  368. created () {
  369. injected = [this.foo, this.bar, this.baz]
  370. }
  371. })
  372. expect(injected).toEqual([2, false, undefined])
  373. })
  374. it('should use provided value even if inject has default', () => {
  375. const vm = new Vue({
  376. provide: {
  377. foo: 1,
  378. bar: false,
  379. baz: undefined
  380. }
  381. })
  382. new Vue({
  383. parent: vm,
  384. inject: {
  385. foo: { default: 2 },
  386. bar: { default: 2 },
  387. baz: { default: 2 }
  388. },
  389. created () {
  390. injected = [this.foo, this.bar, this.baz]
  391. }
  392. })
  393. expect(injected).toEqual([1, false, undefined])
  394. })
  395. // Github issue #6008
  396. it('should merge provide from mixins (objects)', () => {
  397. const mixinA = { provide: { foo: 'foo' }}
  398. const mixinB = { provide: { bar: 'bar' }}
  399. const child = {
  400. inject: ['foo', 'bar'],
  401. template: `<span/>`,
  402. created () {
  403. injected = [this.foo, this.bar]
  404. }
  405. }
  406. new Vue({
  407. mixins: [mixinA, mixinB],
  408. render (h) {
  409. return h(child)
  410. }
  411. }).$mount()
  412. expect(injected).toEqual(['foo', 'bar'])
  413. })
  414. it('should merge provide from mixins (functions)', () => {
  415. const mixinA = { provide: () => ({ foo: 'foo' }) }
  416. const mixinB = { provide: () => ({ bar: 'bar' }) }
  417. const child = {
  418. inject: ['foo', 'bar'],
  419. template: `<span/>`,
  420. created () {
  421. injected = [this.foo, this.bar]
  422. }
  423. }
  424. new Vue({
  425. mixins: [mixinA, mixinB],
  426. render (h) {
  427. return h(child)
  428. }
  429. }).$mount()
  430. expect(injected).toEqual(['foo', 'bar'])
  431. })
  432. it('should merge provide from mixins (mix of objects and functions)', () => {
  433. const mixinA = { provide: { foo: 'foo' }}
  434. const mixinB = { provide: () => ({ bar: 'bar' }) }
  435. const mixinC = { provide: { baz: 'baz' }}
  436. const mixinD = { provide: () => ({ bam: 'bam' }) }
  437. const child = {
  438. inject: ['foo', 'bar', 'baz', 'bam'],
  439. template: `<span/>`,
  440. created () {
  441. injected = [this.foo, this.bar, this.baz, this.bam]
  442. }
  443. }
  444. new Vue({
  445. mixins: [mixinA, mixinB, mixinC, mixinD],
  446. render (h) {
  447. return h(child)
  448. }
  449. }).$mount()
  450. expect(injected).toEqual(['foo', 'bar', 'baz', 'bam'])
  451. })
  452. it('should merge provide from mixins and override existing keys', () => {
  453. const mixinA = { provide: { foo: 'foo' }}
  454. const mixinB = { provide: { foo: 'bar' }}
  455. const child = {
  456. inject: ['foo'],
  457. template: `<span/>`,
  458. created () {
  459. injected = [this.foo]
  460. }
  461. }
  462. new Vue({
  463. mixins: [mixinA, mixinB],
  464. render (h) {
  465. return h(child)
  466. }
  467. }).$mount()
  468. expect(injected).toEqual(['bar'])
  469. })
  470. it('should merge provide when Vue.extend', () => {
  471. const mixinA = { provide: () => ({ foo: 'foo' }) }
  472. const child = {
  473. inject: ['foo', 'bar'],
  474. template: `<span/>`,
  475. created () {
  476. injected = [this.foo, this.bar]
  477. }
  478. }
  479. const Ctor = Vue.extend({
  480. mixins: [mixinA],
  481. provide: { bar: 'bar' },
  482. render (h) {
  483. return h(child)
  484. }
  485. })
  486. new Ctor().$mount()
  487. expect(injected).toEqual(['foo', 'bar'])
  488. })
  489. // #5913
  490. it('should keep the reactive with provide', () => {
  491. function isObserver (obj) {
  492. if (isObject(obj)) {
  493. return hasOwn(obj, '__ob__') && obj.__ob__ instanceof Observer
  494. }
  495. return false
  496. }
  497. const vm = new Vue({
  498. template: `<div><child ref='child'></child></div>`,
  499. data () {
  500. return {
  501. foo: {},
  502. $foo: {},
  503. foo1: []
  504. }
  505. },
  506. provide () {
  507. return {
  508. foo: this.foo,
  509. $foo: this.$foo,
  510. foo1: this.foo1,
  511. bar: {},
  512. baz: []
  513. }
  514. },
  515. components: {
  516. child: {
  517. inject: ['foo', '$foo', 'foo1', 'bar', 'baz'],
  518. template: `<span/>`
  519. }
  520. }
  521. }).$mount()
  522. const child = vm.$refs.child
  523. expect(isObserver(child.foo)).toBe(true)
  524. expect(isObserver(child.$foo)).toBe(false)
  525. expect(isObserver(child.foo1)).toBe(true)
  526. expect(isObserver(child.bar)).toBe(false)
  527. expect(isObserver(child.baz)).toBe(false)
  528. })
  529. // #6175
  530. it('merge provide properly from mixins', () => {
  531. const ProvideFooMixin = {
  532. provide: {
  533. foo: 'foo injected'
  534. }
  535. }
  536. const ProvideBarMixin = {
  537. provide: {
  538. bar: 'bar injected'
  539. }
  540. }
  541. const Child = {
  542. inject: ['foo', 'bar'],
  543. render (h) {
  544. return h('div', [`foo: ${this.foo}, `, `bar: ${this.bar}`])
  545. }
  546. }
  547. const Parent = {
  548. mixins: [ProvideFooMixin, ProvideBarMixin],
  549. render (h) {
  550. return h(Child)
  551. }
  552. }
  553. const vm = new Vue({
  554. render (h) {
  555. return h(Parent)
  556. }
  557. }).$mount()
  558. expect(vm.$el.textContent).toBe(`foo: foo injected, bar: bar injected`)
  559. })
  560. it('merge provide with object syntax when using Vue.extend', () => {
  561. const child = {
  562. inject: ['foo'],
  563. template: `<span/>`,
  564. created () {
  565. injected = this.foo
  566. }
  567. }
  568. const Ctor = Vue.extend({
  569. provide: { foo: 'foo' },
  570. render (h) {
  571. return h(child)
  572. }
  573. })
  574. new Ctor().$mount()
  575. expect(injected).toEqual('foo')
  576. })
  577. })