From 313dd06065b1782d67f6881fbd42ae92a7f9cade Mon Sep 17 00:00:00 2001 From: Stanislav Lashmanov Date: Mon, 14 Sep 2020 19:52:19 +0300 Subject: [PATCH] fix(runtime-core/inject): handle optional `from` option in inject object config (#2073) --- .../runtime-core/__tests__/apiOptions.spec.ts | 35 ++++++++++++++++--- packages/runtime-core/src/componentOptions.ts | 4 +-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/runtime-core/__tests__/apiOptions.spec.ts b/packages/runtime-core/__tests__/apiOptions.spec.ts index 4dd1910b..00ecb6e4 100644 --- a/packages/runtime-core/__tests__/apiOptions.spec.ts +++ b/packages/runtime-core/__tests__/apiOptions.spec.ts @@ -253,7 +253,16 @@ describe('api: options', () => { } }, render() { - return [h(ChildA), h(ChildB), h(ChildC), h(ChildD), h(ChildE)] + return [ + h(ChildA), + h(ChildB), + h(ChildC), + h(ChildD), + h(ChildE), + h(ChildF), + h(ChildG), + h(ChildH) + ] } }) @@ -272,19 +281,37 @@ describe('api: options', () => { from: 'a' } }) - const ChildD = defineChild({ + const ChildD = defineChild( + { + a: { + default: () => 0 + } + }, + 'a' + ) + const ChildE = defineChild({ b: { from: 'c', default: 2 } }) - const ChildE = defineChild({ + const ChildF = defineChild({ b: { from: 'c', default: () => 3 } }) - expect(renderToString(h(Root))).toBe(`11123`) + const ChildG = defineChild({ + b: { + default: 4 + } + }) + const ChildH = defineChild({ + b: { + default: () => 5 + } + }) + expect(renderToString(h(Root))).toBe(`11112345`) }) test('lifecycle', async () => { diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index d02a1bb0..672cb8af 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -275,7 +275,7 @@ type ComponentInjectOptions = | string[] | Record< string | symbol, - string | symbol | { from: string | symbol; default?: unknown } + string | symbol | { from?: string | symbol; default?: unknown } > interface LegacyOptions< @@ -460,7 +460,7 @@ export function applyOptions( const opt = injectOptions[key] if (isObject(opt)) { ctx[key] = inject( - opt.from, + opt.from || key, opt.default, true /* treat default function as factory */ )