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); window.addEventListener('error', handler, true);
const [b1, b2] = target.querySelectorAll('button'); const [b1, b2, b3] = target.querySelectorAll('button');
b1.click(); b1.click();
b2.click(); b2.click();
b3.click();
assert.deepEqual(logs, []); assert.deepEqual(logs, []);
assert.deepEqual(warnings, [ 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[0], 'is not a function');
assert.include(errors[2], '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); window.removeEventListener('error', handler, true);
} }

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

@ -13,10 +13,10 @@ export default test({
flushSync(); flushSync();
buttons.forEach((b) => b.click()); buttons.forEach((b) => b.click());
flushSync(); flushSync();
assert.deepEqual(logs, [2, 5, 6, 7, 9, 11]); assert.deepEqual(logs, [true, true, "mutated", "mutated", "assigned", "assigned"]);
assert.htmlEqual( assert.htmlEqual(
target.innerHTML, 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> <script>
import { on } from 'svelte/events'; import { on } from 'svelte/events';
let count = $state(0);
let onclick = $state.raw({ let onclick = $state.raw({
handleEvent() { handleEvent(ev) {
count += +this.dataset.step; console.log(this === ev.currentTarget);
console.log(count);
} }
}); });
function click2() { function click2() {
count++; console.log("mutated");
console.log(count);
} }
function click3() { function click3() {
count += 2; console.log("assigned");
console.log(count);
} }
let btn; let btn;
let step = 1;
$effect(() => { $effect(() => {
return on(btn, 'click', () => { return on(btn, 'click', () => {
if (onclick.handleEvent !== click2) { switch (step) {
onclick.handleEvent = click2; case 1: {
} else { onclick.handleEvent = click2;
onclick = { handleEvent: click3 }; step = 2;
break;
}
case 2: {
onclick = { handleEvent: click3 };
}
} }
}); });
}); });
</script> </script>
<button data-step="2" {onclick}> <button {onclick}>a</button>
clicks: {count} <button {...{onclick}}>b</button>
</button>
<button data-step="3" {...{onclick}}>inc</button>
<button bind:this={btn}>next</button> <button bind:this={btn}>next</button>

Loading…
Cancel
Save