Skip to content

Commit

Permalink
clean up test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 2, 2024
1 parent 73204af commit 9236d04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@ export default test({
async test({ assert, target }) {
let [btn1, btn2] = target.querySelectorAll('button');

btn1?.click();
flushSync();
await Promise.resolve();

assert.htmlEqual(target.innerHTML, `<button>hide</button><button>show</button`);

btn2?.click();
await Promise.resolve();
await Promise.resolve();

flushSync(() => btn1?.click());
assert.htmlEqual(
target.innerHTML,
`<h1>John Doe</h1><p>Body</p><div>123</div><button>hide</button><button>show</button>`
`
<button>toggle a</button>
<button>toggle b</button>
false/true/true
`
);

btn1?.click();
flushSync();
await Promise.resolve();

assert.htmlEqual(target.innerHTML, `<button>hide</button><button>show</button`);

btn2?.click();
await Promise.resolve();

This comment has been minimized.

Copy link
@Rich-Harris

Rich-Harris Jul 2, 2024

Author Member

You only need them because you were using queueMicrotask — if you just do the state update separately it becomes unnecessary

await Promise.resolve();
flushSync(() => btn2?.click());
assert.htmlEqual(
target.innerHTML,
`
<button>toggle a</button>
<button>toggle b</button>
`
);

flushSync(() => btn2?.click());
assert.htmlEqual(
target.innerHTML,
`<h1>John Doe</h1><p>Body</p><div>123</div><button>hide</button><button>show</button>`
`
<button>toggle a</button>
<button>toggle b</button>
false/true/true
`
);
}
});
Original file line number Diff line number Diff line change
@@ -1,49 +1,13 @@
<script>
import { writable } from "svelte/store";
let a = $state(true);
let b = $state({ c: true });
const store = writable({
url: {
pathname: '123'
}
})
const page = {
subscribe(fn) {
return store.subscribe(fn);
}
}
let data = $state({
event: {
author: 'John Doe',
body: 'Body',
foo: '123'
},
});
const { event } = $derived(data);
const x = $derived(b);
</script>

{#if event}
<h1>{event.author}</h1>
<p>{event.body}</p>
<div>{$page.url.pathname}</div>
{/if}

<button onclick={() => {
data = {}
store.update(v => ({...v}));
}}>hide</button>

<button onclick={() => {
data = {
event: {
author: 'John Doe',
body: 'Body',
foo: '123'
},
}
queueMicrotask(() => {
store.update(v => ({...v}));
})
}}>show</button>
<button onclick={() => (a = !a)}>toggle a</button>
<button onclick={() => (b = b ? null : { c: true })}>toggle b</button>

{#if x}
{a}/{x.c}/{x.c}
{/if}

0 comments on commit 9236d04

Please sign in to comment.