improve tests

pull/15856/head
7nik 1 year ago
parent 32ee6efa6c
commit 9c3ad71081

@ -14,17 +14,20 @@ export default test({
window.addEventListener('error', handler, true);
const [b1, b2] = target.querySelectorAll('button');
const [b1, b2, b3] = target.querySelectorAll('button');
b1.click();
b2.click();
b3.click();
assert.deepEqual(logs, []);
assert.deepEqual(warnings, [
'`click` handler at main.svelte:6:17 should be a function. Did you mean to add a leading `() =>`?',
'`click` handler at main.svelte:7:17 should be a function. Did you mean to add a leading `() =>`?'
'`click` handler at main.svelte:7:17 should be a function. Did you mean to add a leading `() =>`?',
'`click` handler at main.svelte:8:17 should be a function. Did you mean to add a leading `() =>`?',
'`click` handler at main.svelte:9:17 should be a function. Did you mean to add a leading `() =>`?'
]);
assert.include(errors[0], 'is not a function');
assert.include(errors[2], 'is not a function');
assert.include(errors[4], 'is not a function');
window.removeEventListener('error', handler, true);
}

@ -1,7 +1,9 @@
<script>
let empty = {};
let nully = { handleEvent: null };
let wrong = { handleEvent: "bad" };
</script>
<button onclick={empty}>click</button>
<button onclick={nully}>click</button>
<button onclick={wrong}>click</button>

@ -13,10 +13,10 @@ export default test({
flushSync();
buttons.forEach((b) => b.click());
flushSync();
assert.deepEqual(logs, [2, 5, 6, 7, 9, 11]);
assert.deepEqual(logs, [true, true, "mutated", "mutated", "assigned", "assigned"]);
assert.htmlEqual(
target.innerHTML,
'<button data-step="2">clicks: 11</button><button data-step="3">inc</button><button>next</button>'
'<button>a</button><button>b</button><button>next</button>'
);
}
});

@ -1,39 +1,38 @@
<script>
import { on } from 'svelte/events';
let count = $state(0);
let onclick = $state.raw({
handleEvent() {
count += +this.dataset.step;
console.log(count);
handleEvent(ev) {
console.log(this === ev.currentTarget);
}
});
function click2() {
count++;
console.log(count);
console.log("mutated");
}
function click3() {
count += 2;
console.log(count);
console.log("assigned");
}
let btn;
let step = 1;
$effect(() => {
return on(btn, 'click', () => {
if (onclick.handleEvent !== click2) {
onclick.handleEvent = click2;
} else {
onclick = { handleEvent: click3 };
switch (step) {
case 1: {
onclick.handleEvent = click2;
step = 2;
break;
}
case 2: {
onclick = { handleEvent: click3 };
}
}
});
});
</script>
<button data-step="2" {onclick}>
clicks: {count}
</button>
<button data-step="3" {...{onclick}}>inc</button>
<button {onclick}>a</button>
<button {...{onclick}}>b</button>
<button bind:this={btn}>next</button>

Loading…
Cancel
Save