]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(inject): allow default value to be `undefined` (#894)
authorEduardo San Martin Morote <posva@users.noreply.github.com>
Mon, 30 Mar 2020 19:24:55 +0000 (21:24 +0200)
committerGitHub <noreply@github.com>
Mon, 30 Mar 2020 19:24:55 +0000 (15:24 -0400)
Close #892

packages/runtime-core/__tests__/apiInject.spec.ts
packages/runtime-core/src/apiInject.ts

index d979861a82b699781b8ea8ca7b076d512dd450e8..4d68901f0a52f72397afd923d4d8a4371c095286 100644 (file)
@@ -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()
+  })
 })
index de136d4a4748544bfe0805744f104efbf5c634c4..b52122cd2525c92af165d8207b6e974f3ae41592 100644 (file)
@@ -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.`)