mirror of https://github.com/sveltejs/svelte
[fix] Improve error message if `this` attribute of `<svelte:component>` is not valid (#7551)
* add test * improve error message if this attribute of <svelte:component> is not SvelteComponent * add more tests * improve validation * simplify test Co-authored-by: Tan Li Hau <tanhauhau@users.noreply.github.com>pull/7833/head
parent
be70a898f3
commit
01a91163a9
@ -0,0 +1,8 @@
|
||||
export default {
|
||||
skip_if_ssr: true,
|
||||
skip_if_hydrate_from_ssr: true,
|
||||
compileOptions: {
|
||||
dev: true
|
||||
},
|
||||
error: 'this={...} of <svelte:component> should specify a Svelte component.'
|
||||
};
|
@ -0,0 +1,5 @@
|
||||
<script>
|
||||
let banana = {};
|
||||
</script>
|
||||
|
||||
<svelte:component this={banana} />
|
@ -0,0 +1 @@
|
||||
<div>Sub</div>
|
@ -0,0 +1,8 @@
|
||||
export default {
|
||||
skip_if_ssr: true,
|
||||
skip_if_hydrate_from_ssr: true,
|
||||
props: {
|
||||
selected: false
|
||||
},
|
||||
error: 'component is not a constructor'
|
||||
};
|
@ -0,0 +1,9 @@
|
||||
<script>
|
||||
import Sub from './Sub.svelte';
|
||||
export let selected;
|
||||
let banana = {};
|
||||
let component = banana;
|
||||
$: selected ? component = Sub : component = banana;
|
||||
</script>
|
||||
|
||||
<svelte:component this={component} />
|
@ -0,0 +1 @@
|
||||
<div>Sub</div>
|
@ -0,0 +1,19 @@
|
||||
export default {
|
||||
compileOptions: {
|
||||
dev: true
|
||||
},
|
||||
props: {
|
||||
componentName: 'Sub'
|
||||
},
|
||||
html: '<div>Sub</div>',
|
||||
test({ assert, component, target }) {
|
||||
component.componentName = 'Proxy';
|
||||
assert.htmlEqual(target.innerHTML, '<div>Sub</div>');
|
||||
try {
|
||||
component.componentName = 'banana';
|
||||
throw new Error('Expected an error');
|
||||
} catch (err) {
|
||||
assert.equal(err.message, 'this={...} of <svelte:component> should specify a Svelte component.');
|
||||
}
|
||||
}
|
||||
};
|
@ -0,0 +1,14 @@
|
||||
<script>
|
||||
import Sub from './Sub.svelte';
|
||||
export let componentName = 'Sub';
|
||||
let proxy = new Proxy(Sub, {});
|
||||
let banana = {};
|
||||
let component;
|
||||
$: {
|
||||
if (componentName === 'Sub') component = Sub;
|
||||
else if (componentName === 'Proxy') component = proxy;
|
||||
else component = banana;
|
||||
};
|
||||
</script>
|
||||
|
||||
<svelte:component this={component} />
|
@ -0,0 +1 @@
|
||||
<div>Sub</div>
|
@ -0,0 +1,16 @@
|
||||
export default {
|
||||
props: {
|
||||
componentName: 'Sub'
|
||||
},
|
||||
html: '<div>Sub</div>',
|
||||
test({ assert, component, target }) {
|
||||
component.componentName = 'Proxy';
|
||||
assert.htmlEqual(target.innerHTML, '<div>Sub</div>');
|
||||
try {
|
||||
component.componentName = 'banana';
|
||||
throw new Error('Expected an error');
|
||||
} catch (err) {
|
||||
assert.equal(err.message, 'component is not a constructor');
|
||||
}
|
||||
}
|
||||
};
|
@ -0,0 +1,14 @@
|
||||
<script>
|
||||
import Sub from './Sub.svelte';
|
||||
export let componentName = 'Sub';
|
||||
let proxy = new Proxy(Sub, {});
|
||||
let banana = {};
|
||||
let component;
|
||||
$: {
|
||||
if (componentName === 'Sub') component = Sub;
|
||||
else if (componentName === 'Proxy') component = proxy;
|
||||
else component = banana;
|
||||
};
|
||||
</script>
|
||||
|
||||
<svelte:component this={component} />
|
Loading…
Reference in new issue