mirror of https://github.com/sveltejs/svelte
fix: ensure correct context for action update/destroy functions (#11023)
parent
34748ba015
commit
ad11c5087f
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: ensure correct context for action update/destroy functions
|
@ -0,0 +1,22 @@
|
||||
import { ok, test } from '../../test';
|
||||
import { flushSync, tick } from 'svelte';
|
||||
import { log } from './log.js';
|
||||
|
||||
export default test({
|
||||
html: `<button>0</button>`,
|
||||
|
||||
before_test() {
|
||||
log.length = 0;
|
||||
},
|
||||
|
||||
async test({ assert, target }) {
|
||||
const btn = target.querySelector('button');
|
||||
ok(btn);
|
||||
|
||||
flushSync(() => btn.click());
|
||||
assert.deepEqual(log, ['update', 0, 1]);
|
||||
|
||||
flushSync(() => btn.click());
|
||||
assert.deepEqual(log, ['update', 0, 1, 'destroy', 1]);
|
||||
}
|
||||
});
|
@ -0,0 +1,2 @@
|
||||
/** @type {any[]} */
|
||||
export const log = [];
|
@ -0,0 +1,28 @@
|
||||
<script>
|
||||
import { log } from './log.js';
|
||||
|
||||
let count = $state(0);
|
||||
|
||||
/**
|
||||
* @param {Element} _
|
||||
* @param {number} count
|
||||
*/
|
||||
function action(_, count) {
|
||||
return {
|
||||
count,
|
||||
/** @param {number} count */
|
||||
update(count) {
|
||||
log.push('update', this.count, (this.count = count));
|
||||
},
|
||||
destroy() {
|
||||
log.push('destroy', this.count);
|
||||
},
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
{#if count < 2}
|
||||
<button use:action={count} onclick={() => count++}>
|
||||
{count}
|
||||
</button>
|
||||
{/if}
|
Loading…
Reference in new issue