Skip to content

Commit 2a8a777

Browse files
znckyyx990803
authored andcommitted
Check property exists instead of truthy value (#5044)
* Check property exists instead of truthy value * Provide some falsy values for inject tests
1 parent 367f781 commit 2a8a777

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/core/instance/inject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function initInjections (vm: Component) {
2525
const provideKey = isArray ? key : inject[key]
2626
let source = vm
2727
while (source) {
28-
if (source._provided && source._provided[provideKey]) {
28+
if (source._provided && provideKey in source._provided) {
2929
vm[key] = source._provided[provideKey]
3030
break
3131
}

test/unit/features/options/inject.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('Options provide/inject', () => {
2020
template: `<child/>`,
2121
provide: {
2222
foo: 1,
23-
bar: 2
23+
bar: false
2424
},
2525
components: {
2626
child: {
@@ -32,15 +32,15 @@ describe('Options provide/inject', () => {
3232
}
3333
}).$mount()
3434

35-
expect(injected).toEqual([1, 2])
35+
expect(injected).toEqual([1, false])
3636
})
3737

3838
it('should use closest parent', () => {
3939
new Vue({
4040
template: `<child/>`,
4141
provide: {
4242
foo: 1,
43-
bar: 2
43+
bar: null
4444
},
4545
components: {
4646
child: {
@@ -55,15 +55,15 @@ describe('Options provide/inject', () => {
5555
}
5656
}).$mount()
5757

58-
expect(injected).toEqual([3, 2])
58+
expect(injected).toEqual([3, null])
5959
})
6060

6161
it('provide function', () => {
6262
new Vue({
6363
template: `<child/>`,
6464
data: {
6565
a: 1,
66-
b: 2
66+
b: false
6767
},
6868
provide () {
6969
return {
@@ -81,7 +81,7 @@ describe('Options provide/inject', () => {
8181
}
8282
}).$mount()
8383

84-
expect(injected).toEqual([1, 2])
84+
expect(injected).toEqual([1, false])
8585
})
8686

8787
it('inject with alias', () => {
@@ -99,7 +99,7 @@ describe('Options provide/inject', () => {
9999
new Vue({
100100
template: `<child/>`,
101101
provide: {
102-
foo: 1,
102+
foo: false,
103103
bar: 2
104104
},
105105
components: {
@@ -112,7 +112,7 @@ describe('Options provide/inject', () => {
112112
}
113113
}).$mount()
114114

115-
expect(injected).toEqual([1, 2])
115+
expect(injected).toEqual([false, 2])
116116
})
117117

118118
it('self-inject', () => {

0 commit comments

Comments
 (0)