allow passing in context in constructor

pull/6032/head
Tan Li Hau 6 years ago committed by tanhauhau
parent ff0ccb8755
commit 7f6e5997db

@ -120,7 +120,7 @@ export function init(component, options, instance, create_fragment, not_equal, p
on_disconnect: [], on_disconnect: [],
before_update: [], before_update: [],
after_update: [], after_update: [],
context: new Map(parent_component ? parent_component.$$.context : []), context: new Map(parent_component ? parent_component.$$.context : options.context ? Object.entries(options.context) : []),
// everything else // everything else
callbacks: blank_object(), callbacks: blank_object(),

@ -3,7 +3,7 @@
"include": ["."], "include": ["."],
"compilerOptions": { "compilerOptions": {
"lib": ["es2015", "dom", "dom.iterable"], "lib": ["es2017", "dom", "dom.iterable"],
"target": "es2015", "target": "es2015",
"types": [], "types": [],

@ -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,22 @@
export default {
async test({ assert, target, window }) {
const Component = require('./Component.svelte').default;
const called = [];
const component = new Component({
target,
context: {
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();
}
};
Loading…
Cancel
Save