Skip to content

Commit

Permalink
fix: error using useProvidedComponentClass with componentLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Dec 15, 2021
1 parent d66d8a9 commit 7705623
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/runtime/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export function createResolver (exports: {[key: string]: any}, require: nodeRequ
if (typeof ChildComponentClassOrInstance === 'string' && ChildComponentClassOrInstance === 'self') {
return instance
}
// component loader
if (
Object.prototype.hasOwnProperty.call(ChildComponentClassOrInstance, 'load') &&
Object.prototype.hasOwnProperty.call(ChildComponentClassOrInstance, 'placeholder')
) {
return ChildComponentClassOrInstance.placeholder
}
if (typeof ChildComponentClassOrInstance !== 'function' && typeof ChildComponentClassOrInstance !== 'object') {
throw Error(`external component is not provided: ${tagName}${instance.prototype?.id || ''}`)
}
Expand Down
18 changes: 17 additions & 1 deletion test/cases/provide-class/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ const List = san.defineComponent({
template: '<div>{{ text }}</div>'
})

const CompA = san.defineComponent({
initData () {
return {
text: 'component a'
}
},
template: '<div>{{ text }}</div>'
})

const MyComponent = san.defineComponent({
components: {
'x-l': List
'x-l': List,
'x-g': san.createComponentLoader({
load: () => {
return List
},
placeholder: CompA
})
},
initData () {
return {
Expand All @@ -21,6 +36,7 @@ const MyComponent = san.defineComponent({
template: `<div>
<div s-is="c"></div>
<x-l/>
<x-g/>
</div>`
})

Expand Down
2 changes: 1 addition & 1 deletion test/cases/provide-class/expected.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div><!--s-data:{"c": "x-l"}--><div>child</div><div>child</div></div>
<div><!--s-data:{"c": "x-l"}--><div>child</div><div>child</div><div>component a</div></div>
17 changes: 16 additions & 1 deletion test/unit/runtime/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ describe('runtime/resolver', () => {
'child-a': ChildA,
'child-b': ChildA,
'child-c': 'self',
'child-d': 123
'child-d': 123,
'child-e': san.createComponentLoader(() => Promise.resolve(ChildA)),
'child-f': san.createComponentLoader({
load () {
return Promise.resolve(ChildA)
},
placeholder: ChildB
})
}
})
const ref = { id: 'id', specifier: './som/path' }
Expand Down Expand Up @@ -101,5 +108,13 @@ describe('runtime/resolver', () => {
}
expect(fn.mock.calls.length).toBe(1)
})

it('should get placeholder of componentLoader', () => {
const ChildE = resolver.getChildComponentClass(ref, MyComponent, 'child-e');
const ChildF = resolver.getChildComponentClass(ref, MyComponent, 'child-f');

expect(ChildE).toBe(undefined)
expect(ChildF).toBe(ChildB)
})
})
})

0 comments on commit 7705623

Please sign in to comment.