Skip to content

Commit 9c3ad71

Browse files
committed
improve tests
1 parent 32ee6ef commit 9c3ad71

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

packages/svelte/tests/runtime-runes/samples/event-handler-object-invalid/_config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ export default test({
1414

1515
window.addEventListener('error', handler, true);
1616

17-
const [b1, b2] = target.querySelectorAll('button');
17+
const [b1, b2, b3] = target.querySelectorAll('button');
1818

1919
b1.click();
2020
b2.click();
21+
b3.click();
2122
assert.deepEqual(logs, []);
2223
assert.deepEqual(warnings, [
23-
'`click` handler at main.svelte:6:17 should be a function. Did you mean to add a leading `() =>`?',
24-
'`click` handler at main.svelte:7:17 should be a function. Did you mean to add a leading `() =>`?'
24+
'`click` handler at main.svelte:7:17 should be a function. Did you mean to add a leading `() =>`?',
25+
'`click` handler at main.svelte:8:17 should be a function. Did you mean to add a leading `() =>`?',
26+
'`click` handler at main.svelte:9:17 should be a function. Did you mean to add a leading `() =>`?'
2527
]);
2628
assert.include(errors[0], 'is not a function');
2729
assert.include(errors[2], 'is not a function');
30+
assert.include(errors[4], 'is not a function');
2831

2932
window.removeEventListener('error', handler, true);
3033
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<script>
22
let empty = {};
3+
let nully = { handleEvent: null };
34
let wrong = { handleEvent: "bad" };
45
</script>
56

67
<button onclick={empty}>click</button>
8+
<button onclick={nully}>click</button>
79
<button onclick={wrong}>click</button>

packages/svelte/tests/runtime-runes/samples/event-handler-object/_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export default test({
1313
flushSync();
1414
buttons.forEach((b) => b.click());
1515
flushSync();
16-
assert.deepEqual(logs, [2, 5, 6, 7, 9, 11]);
16+
assert.deepEqual(logs, [true, true, "mutated", "mutated", "assigned", "assigned"]);
1717
assert.htmlEqual(
1818
target.innerHTML,
19-
'<button data-step="2">clicks: 11</button><button data-step="3">inc</button><button>next</button>'
19+
'<button>a</button><button>b</button><button>next</button>'
2020
);
2121
}
2222
});
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
<script>
22
import { on } from 'svelte/events';
33
4-
let count = $state(0);
5-
64
let onclick = $state.raw({
7-
handleEvent() {
8-
count += +this.dataset.step;
9-
console.log(count);
5+
handleEvent(ev) {
6+
console.log(this === ev.currentTarget);
107
}
118
});
129
1310
function click2() {
14-
count++;
15-
console.log(count);
11+
console.log("mutated");
1612
}
1713
1814
function click3() {
19-
count += 2;
20-
console.log(count);
15+
console.log("assigned");
2116
}
2217
2318
let btn;
19+
let step = 1;
2420
$effect(() => {
2521
return on(btn, 'click', () => {
26-
if (onclick.handleEvent !== click2) {
27-
onclick.handleEvent = click2;
28-
} else {
29-
onclick = { handleEvent: click3 };
22+
switch (step) {
23+
case 1: {
24+
onclick.handleEvent = click2;
25+
step = 2;
26+
break;
27+
}
28+
case 2: {
29+
onclick = { handleEvent: click3 };
30+
}
3031
}
3132
});
3233
});
3334
</script>
3435

35-
<button data-step="2" {onclick}>
36-
clicks: {count}
37-
</button>
38-
<button data-step="3" {...{onclick}}>inc</button>
36+
<button {onclick}>a</button>
37+
<button {...{onclick}}>b</button>
3938
<button bind:this={btn}>next</button>

0 commit comments

Comments
 (0)