-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.svelte.spec.ts
50 lines (39 loc) · 991 Bytes
/
demo.svelte.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { SvelteSet } from "svelte/reactivity";
import { expect, test } from "vitest";
class Group {
selected = new SvelteSet<string>();
}
class Item {
group: Group;
id: string;
constructor(group: Group, id: string) {
this.group = group;
this.id = id;
}
#selected = $derived.by(() => this.group.selected.has(this.id));
get selected() {
return this.#selected;
}
set selected(value: boolean) {
if (value) {
this.group.selected.add(this.id);
} else {
this.group.selected.delete(this.id);
}
}
}
test("Item.selected", () => {
const cleanup = $effect.root(() => {
const group = new Group();
const item = new Item(group, "foo");
expect(group.selected.has("foo")).toBe(false);
expect(item.selected).toBe(false);
item.selected = true;
expect(group.selected.has("foo")).toBe(true);
expect(item.selected).toBe(true);
item.selected = false;
expect(group.selected.has("foo")).toBe(false);
expect(item.selected).toBe(false);
});
cleanup();
});