From f99c4a09eaab2b735c6311bb44bc170d2519b3e9 Mon Sep 17 00:00:00 2001
From: Sean O'Grady <1761115+miralize@users.noreply.github.com>
Date: Thu, 5 May 2022 17:32:53 +0100
Subject: [PATCH 1/2] fix(inject): allow default value to be undefined
---
src/apis/inject.ts | 2 +-
test/apis/inject.spec.js | 20 ++++++++++++++++++++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/src/apis/inject.ts b/src/apis/inject.ts
index e94c7a2b..19c1cbed 100644
--- a/src/apis/inject.ts
+++ b/src/apis/inject.ts
@@ -76,7 +76,7 @@ export function inject(
return val
}
- if (defaultValue === undefined && __DEV__) {
+ if (arguments.length > 1 && __DEV__) {
warn(`Injection "${String(key)}" not found`, vm)
}
diff --git a/test/apis/inject.spec.js b/test/apis/inject.spec.js
index b2a817f8..f610b612 100644
--- a/test/apis/inject.spec.js
+++ b/test/apis/inject.spec.js
@@ -166,4 +166,24 @@ describe('Hooks provide/inject', () => {
}).$mount()
expect(fn).toHaveBeenCalled()
})
+
+ it('should not warn when default value is undefined', () => {
+ let injected
+ new Vue({
+ template: ``,
+ components: {
+ child: {
+ template: `
{{ msg }}
`,
+ setup() {
+ injected = inject('foo', undefined)
+ return {
+ injected,
+ }
+ },
+ },
+ },
+ }).$mount()
+
+ expect(`injection "foo" not found`).not.toHaveBeenWarned()
+ })
})
From 1603d867cb0c568cc69a1608c27c09d3528f992f Mon Sep 17 00:00:00 2001
From: Sean O'Grady <1761115+miralize@users.noreply.github.com>
Date: Thu, 5 May 2022 18:00:16 +0100
Subject: [PATCH 2/2] update test
---
src/apis/inject.ts | 14 ++++++-------
test/apis/inject.spec.js | 20 ------------------
test/v3/runtime-core/apiInject.spec.ts | 29 +++++++++++++++++++++++++-
3 files changed, 34 insertions(+), 29 deletions(-)
diff --git a/src/apis/inject.ts b/src/apis/inject.ts
index 19c1cbed..37ed881d 100644
--- a/src/apis/inject.ts
+++ b/src/apis/inject.ts
@@ -74,13 +74,11 @@ export function inject(
const val = resolveInject(key, vm)
if (val !== NOT_FOUND) {
return val
+ } else if (arguments.length > 1) {
+ return treatDefaultAsFactory && isFunction(defaultValue)
+ ? defaultValue()
+ : defaultValue
+ } else if (__DEV__) {
+ warn(`Injection "${String(key)}" not found.`, vm)
}
-
- if (arguments.length > 1 && __DEV__) {
- warn(`Injection "${String(key)}" not found`, vm)
- }
-
- return treatDefaultAsFactory && isFunction(defaultValue)
- ? defaultValue()
- : defaultValue
}
diff --git a/test/apis/inject.spec.js b/test/apis/inject.spec.js
index f610b612..b2a817f8 100644
--- a/test/apis/inject.spec.js
+++ b/test/apis/inject.spec.js
@@ -166,24 +166,4 @@ describe('Hooks provide/inject', () => {
}).$mount()
expect(fn).toHaveBeenCalled()
})
-
- it('should not warn when default value is undefined', () => {
- let injected
- new Vue({
- template: ``,
- components: {
- child: {
- template: `{{ msg }}
`,
- setup() {
- injected = inject('foo', undefined)
- return {
- injected,
- }
- },
- },
- },
- }).$mount()
-
- expect(`injection "foo" not found`).not.toHaveBeenWarned()
- })
})
diff --git a/test/v3/runtime-core/apiInject.spec.ts b/test/v3/runtime-core/apiInject.spec.ts
index 1ed7d5cb..99b7cc85 100644
--- a/test/v3/runtime-core/apiInject.spec.ts
+++ b/test/v3/runtime-core/apiInject.spec.ts
@@ -239,7 +239,7 @@ describe('api: provide/inject', () => {
const root = document.createElement('div')
const vm = createApp(Provider).mount(root)
expect(vm.$el.outerHTML).toBe(``)
- expect(`[Vue warn]: Injection "foo" not found`).toHaveBeenWarned()
+ expect(`[Vue warn]: Injection "foo" not found.`).toHaveBeenWarned()
})
it('should warn unfound w/ injectionKey is undefined', () => {
@@ -277,4 +277,31 @@ describe('api: provide/inject', () => {
const vm = createApp(Comp).mount(root)
expect(vm.$el.outerHTML).toBe(`foo
`)
})
+
+ it('should not warn when default value is undefined', () => {
+ const Provider = {
+ setup() {
+ provide('foo', undefined)
+ return () => h(Middle)
+ },
+ }
+
+ const Middle = {
+ setup() {
+ return () => h(Consumer)
+ },
+ }
+
+ const Consumer = {
+ setup() {
+ const foo = inject('foo')
+ return () => h('div', foo as unknown as string)
+ },
+ }
+
+ const root = document.createElement('div')
+ const vm = createApp(Provider).mount(root)
+ expect(vm.$el.outerHTML).toBe(``)
+ expect(`injection "foo" not found.`).not.toHaveBeenWarned()
+ })
})