|
1 | 1 | import Vue from 'vue'
|
2 | 2 | import { isNative } from 'core/util/env'
|
| 3 | +import { isObserver } from 'core/observer' |
3 | 4 |
|
4 | 5 | describe('Options provide/inject', () => {
|
5 | 6 | let injected
|
@@ -186,40 +187,78 @@ describe('Options provide/inject', () => {
|
186 | 187 | })
|
187 | 188 | }
|
188 | 189 |
|
189 |
| - // Github issue #5223 |
190 |
| - it('should work with reactive array', done => { |
| 190 | + it('should work when the provide change', done => { |
191 | 191 | const vm = new Vue({
|
192 | 192 | template: `<div><child></child></div>`,
|
193 | 193 | data () {
|
194 | 194 | return {
|
195 |
| - foo: [] |
| 195 | + foo: 0, |
| 196 | + bar: { |
| 197 | + val: 0 |
| 198 | + }, |
| 199 | + baz: [] |
196 | 200 | }
|
197 | 201 | },
|
198 | 202 | provide () {
|
199 | 203 | return {
|
200 |
| - foo: this.foo |
| 204 | + foo: this.foo, |
| 205 | + bar: this.bar, |
| 206 | + baz: this.baz |
201 | 207 | }
|
202 | 208 | },
|
203 | 209 | components: {
|
204 | 210 | child: {
|
205 |
| - inject: ['foo'], |
206 |
| - template: `<span>{{foo.length}}</span>` |
| 211 | + inject: ['foo', 'bar', 'baz'], |
| 212 | + template: `<span>{{foo}},{{bar.val}},{{baz.length}}</span>` |
207 | 213 | }
|
208 | 214 | }
|
209 | 215 | }).$mount()
|
210 | 216 |
|
211 |
| - expect(vm.$el.innerHTML).toEqual(`<span>0</span>`) |
212 |
| - vm.foo.push(vm.foo.length) |
| 217 | + expect(vm.$el.innerHTML).toEqual(`<span>0,0,0</span>`) |
| 218 | + vm.foo = 1 // primitive should no modified |
| 219 | + vm.bar.val = 1 // reactive should modified |
| 220 | + vm.baz.push(0) // reactive array should modified |
213 | 221 | vm.$nextTick(() => {
|
214 |
| - expect(vm.$el.innerHTML).toEqual(`<span>1</span>`) |
215 |
| - vm.foo.pop() |
216 |
| - vm.$nextTick(() => { |
217 |
| - expect(vm.$el.innerHTML).toEqual(`<span>0</span>`) |
218 |
| - done() |
219 |
| - }) |
| 222 | + expect(vm.$el.innerHTML).toEqual(`<span>0,1,1</span>`) |
| 223 | + done() |
220 | 224 | })
|
221 | 225 | })
|
222 | 226 |
|
| 227 | + // Github issue #5913 |
| 228 | + it('should keep the reactive with provide', () => { |
| 229 | + const vm = new Vue({ |
| 230 | + template: `<div><child ref='child'></child></div>`, |
| 231 | + data () { |
| 232 | + return { |
| 233 | + foo: {}, |
| 234 | + $foo: {}, |
| 235 | + foo1: [] |
| 236 | + } |
| 237 | + }, |
| 238 | + provide () { |
| 239 | + return { |
| 240 | + foo: this.foo, |
| 241 | + $foo: this.$foo, |
| 242 | + foo1: this.foo1, |
| 243 | + bar: {}, |
| 244 | + baz: [] |
| 245 | + } |
| 246 | + }, |
| 247 | + components: { |
| 248 | + child: { |
| 249 | + inject: ['foo', '$foo', 'foo1', 'bar', 'baz'], |
| 250 | + template: `<span/>` |
| 251 | + } |
| 252 | + } |
| 253 | + }).$mount() |
| 254 | + const child = vm.$refs.child |
| 255 | + expect(isObserver(child.foo)).toBe(true) |
| 256 | + expect(isObserver(child.$foo)).toBe(false) |
| 257 | + expect(isObserver(child.foo1)).toBe(true) |
| 258 | + expect(isObserver(child.bar)).toBe(false) |
| 259 | + expect(isObserver(child.baz)).toBe(false) |
| 260 | + }) |
| 261 | + |
223 | 262 | it('should extend properly', () => {
|
224 | 263 | const parent = Vue.extend({
|
225 | 264 | template: `<span/>`,
|
@@ -250,24 +289,31 @@ describe('Options provide/inject', () => {
|
250 | 289 | })
|
251 | 290 |
|
252 | 291 | it('should warn when injections has been modified', () => {
|
253 |
| - const key = 'foo' |
| 292 | + const makeWarnText = key => |
| 293 | + `Avoid mutating an injected value directly since the changes will be ` + |
| 294 | + `overwritten whenever the provided component re-renders. ` + |
| 295 | + `injection being mutated: "${key}"` |
| 296 | + |
254 | 297 | const vm = new Vue({
|
255 | 298 | provide: {
|
256 |
| - foo: 1 |
| 299 | + foo: 1, |
| 300 | + bar: { |
| 301 | + val: 1 |
| 302 | + } |
257 | 303 | }
|
258 | 304 | })
|
259 | 305 |
|
260 | 306 | const child = new Vue({
|
261 | 307 | parent: vm,
|
262 |
| - inject: ['foo'] |
| 308 | + inject: ['foo', 'bar'] |
263 | 309 | })
|
264 | 310 |
|
265 | 311 | expect(child.foo).toBe(1)
|
| 312 | + expect(child.bar.val).toBe(1) |
266 | 313 | child.foo = 2
|
267 |
| - expect( |
268 |
| - `Avoid mutating an injected value directly since the changes will be ` + |
269 |
| - `overwritten whenever the provided component re-renders. ` + |
270 |
| - `injection being mutated: "${key}"`).toHaveBeenWarned() |
| 314 | + expect(makeWarnText('foo')).toHaveBeenWarned() |
| 315 | + child.bar = { val: 2 } |
| 316 | + expect(makeWarnText('bar')).toHaveBeenWarned() |
271 | 317 | })
|
272 | 318 |
|
273 | 319 | it('should warn when injections cannot be found', () => {
|
|
0 commit comments