mirror of https://github.com/sveltejs/svelte
allow passing in context in constructor (#6032)
Co-authored-by: Conduitry <git@chor.date>pull/6157/head
parent
bcf2313a34
commit
6c5257beb2
@ -0,0 +1,9 @@
|
||||
<script>
|
||||
import { getContext } from 'svelte';
|
||||
|
||||
const value = getContext('key');
|
||||
const fn = getContext('fn');
|
||||
</script>
|
||||
|
||||
<div>{value}</div>
|
||||
<button on:click={() => fn('hello world')} />
|
@ -0,0 +1,32 @@
|
||||
export default {
|
||||
async test({ assert, target, window }) {
|
||||
const Component = require('./Component.svelte').default;
|
||||
|
||||
const called = [];
|
||||
const component = new Component({
|
||||
target,
|
||||
context: new Map([
|
||||
['key', 'svelte'],
|
||||
['fn', (value) => called.push(value)]
|
||||
])
|
||||
});
|
||||
assert.htmlEqual(target.innerHTML, '<div>svelte</div><button></button>');
|
||||
|
||||
const button = target.querySelector('button');
|
||||
await button.dispatchEvent(new window.MouseEvent('click'));
|
||||
|
||||
assert.deepEqual(called, ['hello world']);
|
||||
|
||||
component.$destroy();
|
||||
},
|
||||
test_ssr({ assert }) {
|
||||
const Component = require('./Component.svelte').default;
|
||||
|
||||
const called = [];
|
||||
const { html } = Component.render(undefined, { context: new Map([
|
||||
['key', 'svelte'],
|
||||
['fn', (value) => called.push(value)]
|
||||
]) });
|
||||
assert.htmlEqual(html, '<div>svelte</div><button></button>');
|
||||
}
|
||||
};
|
Loading…
Reference in new issue