@fun-land/fun-web 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/state.test.ts CHANGED
@@ -38,10 +38,13 @@ describe("funState", () => {
38
38
  const controller = new AbortController();
39
39
  const callback = jest.fn();
40
40
 
41
- state.subscribe(controller.signal, callback);
41
+ state.watch(controller.signal, callback);
42
42
  state.set({ count: 1 });
43
43
 
44
- expect(callback).toHaveBeenCalledWith({ count: 1 });
44
+ // Called with initial value, then with new value
45
+ expect(callback).toHaveBeenCalledTimes(2);
46
+ expect(callback).toHaveBeenNthCalledWith(1, { count: 0 });
47
+ expect(callback).toHaveBeenNthCalledWith(2, { count: 1 });
45
48
  });
46
49
 
47
50
  it("should call subscriber multiple times", () => {
@@ -49,13 +52,15 @@ describe("funState", () => {
49
52
  const controller = new AbortController();
50
53
  const callback = jest.fn();
51
54
 
52
- state.subscribe(controller.signal, callback);
55
+ state.watch(controller.signal, callback);
53
56
  state.set({ count: 1 });
54
57
  state.set({ count: 2 });
55
58
 
56
- expect(callback).toHaveBeenCalledTimes(2);
57
- expect(callback).toHaveBeenNthCalledWith(1, { count: 1 });
58
- expect(callback).toHaveBeenNthCalledWith(2, { count: 2 });
59
+ // watch calls immediately with initial value, then on each change
60
+ expect(callback).toHaveBeenCalledTimes(3);
61
+ expect(callback).toHaveBeenNthCalledWith(1, { count: 0 });
62
+ expect(callback).toHaveBeenNthCalledWith(2, { count: 1 });
63
+ expect(callback).toHaveBeenNthCalledWith(3, { count: 2 });
59
64
  });
60
65
 
61
66
  it("should call focused state subscriber only when that property changes", () => {
@@ -64,7 +69,10 @@ describe("funState", () => {
64
69
  const controller = new AbortController();
65
70
  const callback = jest.fn();
66
71
 
67
- countState.subscribe(controller.signal, callback);
72
+ countState.watch(controller.signal, callback);
73
+
74
+ // Initial call with 0
75
+ expect(callback).toHaveBeenCalledWith(0);
68
76
 
69
77
  // Change count - should trigger
70
78
  state.set({ count: 1, name: "test" });
@@ -86,13 +94,18 @@ describe("funState", () => {
86
94
  const callback1 = jest.fn();
87
95
  const callback2 = jest.fn();
88
96
 
89
- state.subscribe(controller.signal, callback1);
90
- state.subscribe(controller.signal, callback2);
97
+ state.watch(controller.signal, callback1);
98
+ state.watch(controller.signal, callback2);
91
99
 
92
100
  state.set({ count: 1 });
93
101
 
94
- expect(callback1).toHaveBeenCalledWith({ count: 1 });
95
- expect(callback2).toHaveBeenCalledWith({ count: 1 });
102
+ // Both should be called with initial value, then with new value
103
+ expect(callback1).toHaveBeenCalledTimes(2);
104
+ expect(callback1).toHaveBeenNthCalledWith(1, { count: 0 });
105
+ expect(callback1).toHaveBeenNthCalledWith(2, { count: 1 });
106
+ expect(callback2).toHaveBeenCalledTimes(2);
107
+ expect(callback2).toHaveBeenNthCalledWith(1, { count: 0 });
108
+ expect(callback2).toHaveBeenNthCalledWith(2, { count: 1 });
96
109
  });
97
110
 
98
111
  it("should work with accessor-based focus", () => {
@@ -105,11 +118,14 @@ describe("funState", () => {
105
118
  const controller = new AbortController();
106
119
  const callback = jest.fn();
107
120
 
108
- nameState.subscribe(controller.signal, callback);
121
+ nameState.watch(controller.signal, callback);
109
122
 
110
123
  state.set({ name: "Bob", age: 30 });
111
124
 
112
- expect(callback).toHaveBeenCalledWith("Bob");
125
+ // Called with initial value "Alice", then "Bob"
126
+ expect(callback).toHaveBeenCalledTimes(2);
127
+ expect(callback).toHaveBeenNthCalledWith(1, "Alice");
128
+ expect(callback).toHaveBeenNthCalledWith(2, "Bob");
113
129
  });
114
130
  });
115
131
 
@@ -178,11 +194,14 @@ describe("funState", () => {
178
194
  const controller = new AbortController();
179
195
  const callback = jest.fn();
180
196
 
181
- nameState.subscribe(controller.signal, callback);
197
+ nameState.watch(controller.signal, callback);
182
198
 
183
199
  state.set({ user: { profile: { name: "Bob" } } });
184
200
 
185
- expect(callback).toHaveBeenCalledWith("Bob");
201
+ // Called with initial value "Alice", then "Bob"
202
+ expect(callback).toHaveBeenCalledTimes(2);
203
+ expect(callback).toHaveBeenNthCalledWith(1, "Alice");
204
+ expect(callback).toHaveBeenNthCalledWith(2, "Bob");
186
205
 
187
206
  controller.abort();
188
207
  });
@@ -207,17 +226,24 @@ describe("funState", () => {
207
226
  const controller = new AbortController();
208
227
  const callback = jest.fn();
209
228
 
210
- nameState.subscribe(controller.signal, callback);
229
+ nameState.watch(controller.signal, callback);
230
+
231
+ // Initial call with "Alice"
232
+ expect(callback).toHaveBeenCalledTimes(1);
233
+ expect(callback).toHaveBeenCalledWith("Alice");
234
+
235
+ callback.mockClear();
211
236
 
212
- // Change age, not name
237
+ // Change age, not name - should not trigger again
213
238
  state.set({ user: { profile: { name: "Alice", age: 31 } } });
214
239
 
215
240
  expect(callback).not.toHaveBeenCalled();
216
241
 
217
- // Change name
242
+ // Change name - should trigger
218
243
  state.set({ user: { profile: { name: "Bob", age: 31 } } });
219
244
 
220
245
  expect(callback).toHaveBeenCalledWith("Bob");
246
+ expect(callback).toHaveBeenCalledTimes(1);
221
247
 
222
248
  controller.abort();
223
249
  });
package/tsconfig.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "moduleResolution": "node",
11
11
  "module": "CommonJS",
12
12
  "composite": true,
13
- "lib": ["ES2015", "DOM"]
13
+ "lib": ["ES2020", "DOM"]
14
14
  },
15
15
  "include": ["src/**/*.ts"]
16
16
  }