From 01b61ab32c59db648586f133ccfa8cf46cdf0b53 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sat, 28 Mar 2020 12:15:46 +0100 Subject: [PATCH] fix(inject): allow default value to be `undefined` Close #892 --- .../runtime-core/__tests__/apiInject.spec.ts | 23 +++++++++++++++++++ packages/runtime-core/src/apiInject.ts | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/apiInject.spec.ts b/packages/runtime-core/__tests__/apiInject.spec.ts index d979861a82b..4d68901f0a5 100644 --- a/packages/runtime-core/__tests__/apiInject.spec.ts +++ b/packages/runtime-core/__tests__/apiInject.spec.ts @@ -284,4 +284,27 @@ describe('api: provide/inject', () => { expect(serialize(root)).toBe(`
`) 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() + }) }) diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index de136d4a474..b52122cd252 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -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.`)