@zeix/cause-effect 0.15.2 → 0.16.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.
@@ -1,12 +1,12 @@
1
1
  import { describe, expect, test } from 'bun:test'
2
- import { isComputed, isState, state } from '../'
2
+ import { createState, isComputed, isState } from '../'
3
3
 
4
4
  /* === Tests === */
5
5
 
6
6
  describe('State', () => {
7
7
  describe('State type guard', () => {
8
8
  test('isState identifies state signals', () => {
9
- const count = state(42)
9
+ const count = createState(42)
10
10
  expect(isState(count)).toBe(true)
11
11
  expect(isComputed(count)).toBe(false)
12
12
  })
@@ -14,28 +14,28 @@ describe('State', () => {
14
14
 
15
15
  describe('Boolean cause', () => {
16
16
  test('should be boolean', () => {
17
- const cause = state(false)
17
+ const cause = createState(false)
18
18
  expect(typeof cause.get()).toBe('boolean')
19
19
  })
20
20
 
21
21
  test('should set initial value to false', () => {
22
- const cause = state(false)
22
+ const cause = createState(false)
23
23
  expect(cause.get()).toBe(false)
24
24
  })
25
25
 
26
26
  test('should set initial value to true', () => {
27
- const cause = state(true)
27
+ const cause = createState(true)
28
28
  expect(cause.get()).toBe(true)
29
29
  })
30
30
 
31
31
  test('should set new value with .set(true)', () => {
32
- const cause = state(false)
32
+ const cause = createState(false)
33
33
  cause.set(true)
34
34
  expect(cause.get()).toBe(true)
35
35
  })
36
36
 
37
37
  test('should toggle initial value with .set(v => !v)', () => {
38
- const cause = state(false)
38
+ const cause = createState(false)
39
39
  cause.update(v => !v)
40
40
  expect(cause.get()).toBe(true)
41
41
  })
@@ -43,23 +43,23 @@ describe('State', () => {
43
43
 
44
44
  describe('Number cause', () => {
45
45
  test('should be number', () => {
46
- const cause = state(0)
46
+ const cause = createState(0)
47
47
  expect(typeof cause.get()).toBe('number')
48
48
  })
49
49
 
50
50
  test('should set initial value to 0', () => {
51
- const cause = state(0)
51
+ const cause = createState(0)
52
52
  expect(cause.get()).toBe(0)
53
53
  })
54
54
 
55
55
  test('should set new value with .set(42)', () => {
56
- const cause = state(0)
56
+ const cause = createState(0)
57
57
  cause.set(42)
58
58
  expect(cause.get()).toBe(42)
59
59
  })
60
60
 
61
61
  test('should increment value with .set(v => ++v)', () => {
62
- const cause = state(0)
62
+ const cause = createState(0)
63
63
  cause.update(v => ++v)
64
64
  expect(cause.get()).toBe(1)
65
65
  })
@@ -67,23 +67,23 @@ describe('State', () => {
67
67
 
68
68
  describe('String cause', () => {
69
69
  test('should be string', () => {
70
- const cause = state('foo')
70
+ const cause = createState('foo')
71
71
  expect(typeof cause.get()).toBe('string')
72
72
  })
73
73
 
74
74
  test('should set initial value to "foo"', () => {
75
- const cause = state('foo')
75
+ const cause = createState('foo')
76
76
  expect(cause.get()).toBe('foo')
77
77
  })
78
78
 
79
79
  test('should set new value with .set("bar")', () => {
80
- const cause = state('foo')
80
+ const cause = createState('foo')
81
81
  cause.set('bar')
82
82
  expect(cause.get()).toBe('bar')
83
83
  })
84
84
 
85
85
  test('should upper case value with .set(v => v.toUpperCase())', () => {
86
- const cause = state('foo')
86
+ const cause = createState('foo')
87
87
  cause.update(v => (v ? v.toUpperCase() : ''))
88
88
  expect(cause.get()).toBe('FOO')
89
89
  })
@@ -91,56 +91,243 @@ describe('State', () => {
91
91
 
92
92
  describe('Array cause', () => {
93
93
  test('should be array', () => {
94
- const cause = state([1, 2, 3])
94
+ const cause = createState([1, 2, 3])
95
95
  expect(Array.isArray(cause.get())).toBe(true)
96
96
  })
97
97
 
98
98
  test('should set initial value to [1, 2, 3]', () => {
99
- const cause = state([1, 2, 3])
99
+ const cause = createState([1, 2, 3])
100
100
  expect(cause.get()).toEqual([1, 2, 3])
101
101
  })
102
102
 
103
103
  test('should set new value with .set([4, 5, 6])', () => {
104
- const cause = state([1, 2, 3])
104
+ const cause = createState([1, 2, 3])
105
105
  cause.set([4, 5, 6])
106
106
  expect(cause.get()).toEqual([4, 5, 6])
107
107
  })
108
108
 
109
109
  test('should reflect current value of array after modification', () => {
110
110
  const array = [1, 2, 3]
111
- const cause = state(array)
111
+ const cause = createState(array)
112
112
  array.push(4) // don't do this! the result will be correct, but we can't trigger effects
113
113
  expect(cause.get()).toEqual([1, 2, 3, 4])
114
114
  })
115
115
 
116
116
  test('should set new value with .set([...array, 4])', () => {
117
117
  const array = [1, 2, 3]
118
- const cause = state(array)
118
+ const cause = createState(array)
119
119
  cause.set([...array, 4]) // use destructuring instead!
120
120
  expect(cause.get()).toEqual([1, 2, 3, 4])
121
121
  })
122
+
123
+ describe('Input Validation', () => {
124
+ test('should throw NullishSignalValueError when initialValue is nullish', () => {
125
+ expect(() => {
126
+ // @ts-expect-error - Testing invalid input
127
+ createState(null)
128
+ }).toThrow('Nullish signal values are not allowed in state')
129
+
130
+ expect(() => {
131
+ // @ts-expect-error - Testing invalid input
132
+ createState(undefined)
133
+ }).toThrow('Nullish signal values are not allowed in state')
134
+ })
135
+
136
+ test('should throw NullishSignalValueError when newValue is nullish in set()', () => {
137
+ const state = createState(42)
138
+
139
+ expect(() => {
140
+ // @ts-expect-error - Testing invalid input
141
+ state.set(null)
142
+ }).toThrow('Nullish signal values are not allowed in state')
143
+
144
+ expect(() => {
145
+ // @ts-expect-error - Testing invalid input
146
+ state.set(undefined)
147
+ }).toThrow('Nullish signal values are not allowed in state')
148
+ })
149
+
150
+ test('should throw specific error types for nullish values', () => {
151
+ try {
152
+ // @ts-expect-error - Testing invalid input
153
+ createState(null)
154
+ expect(true).toBe(false) // Should not reach here
155
+ } catch (error) {
156
+ expect(error).toBeInstanceOf(TypeError)
157
+ expect(error.name).toBe('NullishSignalValueError')
158
+ expect(error.message).toBe(
159
+ 'Nullish signal values are not allowed in state',
160
+ )
161
+ }
162
+
163
+ const state = createState(42)
164
+ try {
165
+ // @ts-expect-error - Testing invalid input
166
+ state.set(null)
167
+ expect(true).toBe(false) // Should not reach here
168
+ } catch (error) {
169
+ expect(error).toBeInstanceOf(TypeError)
170
+ expect(error.name).toBe('NullishSignalValueError')
171
+ expect(error.message).toBe(
172
+ 'Nullish signal values are not allowed in state',
173
+ )
174
+ }
175
+ })
176
+
177
+ test('should allow valid non-nullish values', () => {
178
+ // These should not throw
179
+ expect(() => {
180
+ createState(0)
181
+ }).not.toThrow()
182
+
183
+ expect(() => {
184
+ createState('')
185
+ }).not.toThrow()
186
+
187
+ expect(() => {
188
+ createState(false)
189
+ }).not.toThrow()
190
+
191
+ expect(() => {
192
+ createState({})
193
+ }).not.toThrow()
194
+
195
+ expect(() => {
196
+ createState([])
197
+ }).not.toThrow()
198
+
199
+ const state = createState(42)
200
+ expect(() => {
201
+ state.set(0)
202
+ }).not.toThrow()
203
+
204
+ expect(() => {
205
+ // @ts-expect-error - Testing valid input of invalid type
206
+ state.set('')
207
+ }).not.toThrow()
208
+ })
209
+
210
+ test('should throw InvalidCallbackError for non-function updater in update()', () => {
211
+ const state = createState(42)
212
+
213
+ expect(() => {
214
+ // @ts-expect-error - Testing invalid input
215
+ state.update(null)
216
+ }).toThrow('Invalid state update callback null')
217
+
218
+ expect(() => {
219
+ // @ts-expect-error - Testing invalid input
220
+ state.update(undefined)
221
+ }).toThrow('Invalid state update callback undefined')
222
+
223
+ expect(() => {
224
+ // @ts-expect-error - Testing invalid input
225
+ state.update('not a function')
226
+ }).toThrow('Invalid state update callback "not a function"')
227
+
228
+ expect(() => {
229
+ // @ts-expect-error - Testing invalid input
230
+ state.update(42)
231
+ }).toThrow('Invalid state update callback 42')
232
+ })
233
+
234
+ test('should throw specific error type for non-function updater', () => {
235
+ const state = createState(42)
236
+
237
+ try {
238
+ // @ts-expect-error - Testing invalid input
239
+ state.update(null)
240
+ expect(true).toBe(false) // Should not reach here
241
+ } catch (error) {
242
+ expect(error).toBeInstanceOf(TypeError)
243
+ expect(error.name).toBe('InvalidCallbackError')
244
+ expect(error.message).toBe(
245
+ 'Invalid state update callback null',
246
+ )
247
+ }
248
+ })
249
+
250
+ test('should handle updater function that throws an error', () => {
251
+ const state = createState(42)
252
+
253
+ expect(() => {
254
+ state.update(() => {
255
+ throw new Error('Updater error')
256
+ })
257
+ }).toThrow('Updater error')
258
+
259
+ // State should remain unchanged after error
260
+ expect(state.get()).toBe(42)
261
+ })
262
+
263
+ test('should handle updater function that returns nullish value', () => {
264
+ const state = createState(42)
265
+
266
+ expect(() => {
267
+ // @ts-expect-error - Testing invalid return value
268
+ state.update(() => null)
269
+ }).toThrow('Nullish signal values are not allowed in state')
270
+
271
+ expect(() => {
272
+ // @ts-expect-error - Testing invalid return value
273
+ state.update(() => undefined)
274
+ }).toThrow('Nullish signal values are not allowed in state')
275
+
276
+ // State should remain unchanged after error
277
+ expect(state.get()).toBe(42)
278
+ })
279
+
280
+ test('should handle valid updater functions', () => {
281
+ const numberState = createState(10)
282
+ expect(() => {
283
+ numberState.update(x => x + 5)
284
+ }).not.toThrow()
285
+ expect(numberState.get()).toBe(15)
286
+
287
+ const stringState = createState('hello')
288
+ expect(() => {
289
+ stringState.update(x => x.toUpperCase())
290
+ }).not.toThrow()
291
+ expect(stringState.get()).toBe('HELLO')
292
+
293
+ const arrayState = createState([1, 2, 3])
294
+ expect(() => {
295
+ arrayState.update(arr => [...arr, 4])
296
+ }).not.toThrow()
297
+ expect(arrayState.get()).toEqual([1, 2, 3, 4])
298
+
299
+ const objectState = createState({ count: 0 })
300
+ expect(() => {
301
+ objectState.update(obj => ({
302
+ ...obj,
303
+ count: obj.count + 1,
304
+ }))
305
+ }).not.toThrow()
306
+ expect(objectState.get()).toEqual({ count: 1 })
307
+ })
308
+ })
122
309
  })
123
310
 
124
311
  describe('Object cause', () => {
125
312
  test('should be object', () => {
126
- const cause = state({ a: 'a', b: 1 })
313
+ const cause = createState({ a: 'a', b: 1 })
127
314
  expect(typeof cause.get()).toBe('object')
128
315
  })
129
316
 
130
317
  test('should set initial value to { a: "a", b: 1 }', () => {
131
- const cause = state({ a: 'a', b: 1 })
318
+ const cause = createState({ a: 'a', b: 1 })
132
319
  expect(cause.get()).toEqual({ a: 'a', b: 1 })
133
320
  })
134
321
 
135
322
  test('should set new value with .set({ c: true })', () => {
136
- const cause = state<Record<string, unknown>>({ a: 'a', b: 1 })
323
+ const cause = createState<Record<string, unknown>>({ a: 'a', b: 1 })
137
324
  cause.set({ c: true })
138
325
  expect(cause.get()).toEqual({ c: true })
139
326
  })
140
327
 
141
328
  test('should reflect current value of object after modification', () => {
142
329
  const obj = { a: 'a', b: 1 }
143
- const cause = state<Record<string, unknown>>(obj)
330
+ const cause = createState<Record<string, unknown>>(obj)
144
331
  // @ts-expect-error Property 'c' does not exist on type '{ a: string; b: number; }'. (ts 2339)
145
332
  obj.c = true // don't do this! the result will be correct, but we can't trigger effects
146
333
  expect(cause.get()).toEqual({ a: 'a', b: 1, c: true })
@@ -148,7 +335,7 @@ describe('State', () => {
148
335
 
149
336
  test('should set new value with .set({...obj, c: true})', () => {
150
337
  const obj = { a: 'a', b: 1 }
151
- const cause = state<Record<string, unknown>>(obj)
338
+ const cause = createState<Record<string, unknown>>(obj)
152
339
  cause.set({ ...obj, c: true }) // use destructuring instead!
153
340
  expect(cause.get()).toEqual({ a: 'a', b: 1, c: true })
154
341
  })