|
1 |
| -import { Derived, Ref } from "./box.svelte.js"; |
| 1 | +import { box, ref } from "./box.svelte.js"; |
2 | 2 |
|
3 | 3 | describe("box", () => {
|
4 | 4 | it("updates when the referenced value updates", () => {
|
5 |
| - let count = 0; |
6 |
| - const box = Ref.to(() => count); |
7 |
| - expect(box.current).toBe(0); |
| 5 | + let count = $state(0); |
| 6 | + const boxed = box(() => count); |
| 7 | + expect(boxed.current).toBe(0); |
8 | 8 |
|
9 | 9 | count = 1;
|
10 |
| - expect(box.current).toBe(1); |
| 10 | + expect(boxed.current).toBe(1); |
11 | 11 | });
|
12 | 12 |
|
13 | 13 | it("creates a readonly box when only a getter is passed", () => {
|
14 |
| - const box = Ref.to(() => 0); |
15 |
| - expect(box.current).toBe(0); |
| 14 | + const boxed = box(() => 0); |
16 | 15 |
|
17 | 16 | function set() {
|
18 | 17 | // @ts-expect-error It's readonly
|
19 |
| - box.current = 1; |
| 18 | + boxed.current = 1; |
20 | 19 | }
|
21 | 20 |
|
22 | 21 | expect(set).toThrowError();
|
23 | 22 | });
|
24 | 23 |
|
25 | 24 | it("creates a writable box when a getter and setter are passed", () => {
|
26 |
| - let count = 0; |
27 |
| - const box = Ref.to( |
| 25 | + let count = $state(0); |
| 26 | + const boxed = box( |
28 | 27 | () => count,
|
29 | 28 | (value) => {
|
30 | 29 | count = value;
|
31 | 30 | }
|
32 | 31 | );
|
33 |
| - expect(box.current).toBe(0); |
| 32 | + expect(boxed.current).toBe(0); |
34 | 33 |
|
35 |
| - box.current = 1; |
36 |
| - expect(box.current).toBe(1); |
| 34 | + boxed.current = 1; |
| 35 | + expect(boxed.current).toBe(1); |
37 | 36 | expect(count).toBe(1);
|
38 | 37 |
|
39 | 38 | count = 2;
|
40 |
| - expect(box.current).toBe(2); |
| 39 | + expect(boxed.current).toBe(2); |
41 | 40 | });
|
42 | 41 |
|
43 |
| - it("does not memoize the getter", () => { |
44 |
| - const fn = vi.fn(() => 0); |
45 |
| - const box = Ref.to(fn); |
| 42 | + it("memoizes the getter", () => { |
| 43 | + let count = $state(0); |
| 44 | + const fn = vi.fn(() => count); |
| 45 | + const boxed = box(fn); |
46 | 46 | expect(fn).not.toHaveBeenCalled();
|
47 | 47 |
|
48 |
| - box.current; |
| 48 | + boxed.current; |
| 49 | + expect(fn).toHaveBeenCalledTimes(1); |
| 50 | + |
| 51 | + boxed.current; |
49 | 52 | expect(fn).toHaveBeenCalledTimes(1);
|
50 | 53 |
|
51 |
| - box.current; |
| 54 | + count = 1; |
| 55 | + boxed.current; |
| 56 | + expect(fn).toHaveBeenCalledTimes(2); |
| 57 | + |
| 58 | + boxed.current; |
52 | 59 | expect(fn).toHaveBeenCalledTimes(2);
|
53 | 60 | });
|
54 | 61 | });
|
55 | 62 |
|
56 |
| -describe("Derived", () => { |
| 63 | +describe("ref", () => { |
57 | 64 | it("updates when the referenced value updates", () => {
|
58 |
| - let count = $state(0); |
59 |
| - const box = Derived.by(() => count); |
60 |
| - expect(box.current).toBe(0); |
| 65 | + let count = 0; |
| 66 | + const boxed = ref(() => count); |
| 67 | + expect(boxed.current).toBe(0); |
61 | 68 |
|
62 | 69 | count = 1;
|
63 |
| - expect(box.current).toBe(1); |
| 70 | + expect(boxed.current).toBe(1); |
64 | 71 | });
|
65 | 72 |
|
66 | 73 | it("creates a readonly box when only a getter is passed", () => {
|
67 |
| - const box = Derived.by(() => 0); |
68 |
| - expect(box.current).toBe(0); |
| 74 | + const boxed = ref(() => 0); |
69 | 75 |
|
70 | 76 | function set() {
|
71 | 77 | // @ts-expect-error It's readonly
|
72 |
| - box.current = 1; |
| 78 | + boxed.current = 1; |
73 | 79 | }
|
74 | 80 |
|
75 | 81 | expect(set).toThrowError();
|
76 | 82 | });
|
77 | 83 |
|
78 | 84 | it("creates a writable box when a getter and setter are passed", () => {
|
79 |
| - let count = $state(0); |
80 |
| - const box = Derived.by( |
| 85 | + let count = 0; |
| 86 | + const boxed = ref( |
81 | 87 | () => count,
|
82 | 88 | (value) => {
|
83 | 89 | count = value;
|
84 | 90 | }
|
85 | 91 | );
|
86 |
| - expect(box.current).toBe(0); |
| 92 | + expect(boxed.current).toBe(0); |
87 | 93 |
|
88 |
| - box.current = 1; |
89 |
| - expect(box.current).toBe(1); |
| 94 | + boxed.current = 1; |
| 95 | + expect(boxed.current).toBe(1); |
90 | 96 | expect(count).toBe(1);
|
91 | 97 |
|
92 | 98 | count = 2;
|
93 |
| - expect(box.current).toBe(2); |
| 99 | + expect(boxed.current).toBe(2); |
94 | 100 | });
|
95 | 101 |
|
96 |
| - it("memoizes the getter", () => { |
97 |
| - let count = $state(0); |
98 |
| - const fn = vi.fn(() => count); |
99 |
| - const box = Derived.by(fn); |
| 102 | + it("does not memoize the getter", () => { |
| 103 | + const fn = vi.fn(() => 0); |
| 104 | + const boxed = ref(fn); |
100 | 105 | expect(fn).not.toHaveBeenCalled();
|
101 | 106 |
|
102 |
| - box.current; |
103 |
| - expect(fn).toHaveBeenCalledTimes(1); |
104 |
| - |
105 |
| - box.current; |
| 107 | + boxed.current; |
106 | 108 | expect(fn).toHaveBeenCalledTimes(1);
|
107 | 109 |
|
108 |
| - count = 1; |
109 |
| - box.current; |
110 |
| - expect(fn).toHaveBeenCalledTimes(2); |
111 |
| - |
112 |
| - box.current; |
| 110 | + boxed.current; |
113 | 111 | expect(fn).toHaveBeenCalledTimes(2);
|
114 | 112 | });
|
115 | 113 | });
|
0 commit comments