Skip to content

Commit

Permalink
fix(inject): allow default value to be undefined (#894)
Browse files Browse the repository at this point in the history
Close #892
  • Loading branch information
posva authored Mar 30, 2020
1 parent 573bcb2 commit 94562da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/runtime-core/__tests__/apiInject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,27 @@ describe('api: provide/inject', () => {
expect(serialize(root)).toBe(`<div><!----></div>`)
expect(`injection "foo" not found.`).toHaveBeenWarned()
})

it('should not warn when default value is undefined', () => {
const Provider = {
setup() {
return () => h(Middle)
}
}

const Middle = {
render: () => h(Consumer)
}

const Consumer = {
setup() {
const foo = inject('foo', undefined)
return () => foo
}
}

const root = nodeOps.createElement('div')
render(h(Provider), root)
expect(`injection "foo" not found.`).not.toHaveBeenWarned()
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function inject(
if (key in provides) {
// TS doesn't allow symbol as index type
return provides[key as string]
} else if (defaultValue !== undefined) {
} else if (arguments.length > 1) {
return defaultValue
} else if (__DEV__) {
warn(`injection "${String(key)}" not found.`)
Expand Down

0 comments on commit 94562da

Please sign in to comment.