Skip to content

Commit

Permalink
fix(runtime-core/inject): handle optional from option in inject obj…
Browse files Browse the repository at this point in the history
…ect config (#2073)
  • Loading branch information
CyberAP authored Sep 14, 2020
1 parent a096a58 commit 313dd06
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
35 changes: 31 additions & 4 deletions packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
}
})

Expand All @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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 */
)
Expand Down

0 comments on commit 313dd06

Please sign in to comment.