@applicaster/zapp-react-native-ui-components 14.0.0-rc.44 → 14.0.0-rc.45

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.
@@ -0,0 +1,379 @@
1
+ import { renderHook, act } from "@testing-library/react-hooks";
2
+ import { BehaviorSubject } from "rxjs";
3
+ import { useLoadingState } from "../useLoadingState";
4
+
5
+ // Mock the useRefWithInitialValue hook
6
+ jest.mock(
7
+ "@applicaster/zapp-react-native-utils/reactHooks/state/useRefWithInitialValue",
8
+ () => ({
9
+ useRefWithInitialValue: jest.fn((initializer) => ({
10
+ current: initializer(),
11
+ })),
12
+ })
13
+ );
14
+
15
+ describe("useLoadingState", () => {
16
+ let onLoadDone: jest.Mock;
17
+
18
+ beforeEach(() => {
19
+ onLoadDone = jest.fn();
20
+ jest.clearAllMocks();
21
+ });
22
+
23
+ describe("initialization", () => {
24
+ it("should initialize with correct default state for zero components", () => {
25
+ const { result } = renderHook(() => useLoadingState(0, onLoadDone));
26
+
27
+ const initialState = result.current.loadingState.getValue();
28
+
29
+ expect(initialState).toEqual({
30
+ index: -1,
31
+ done: true,
32
+ waitForAllComponents: false,
33
+ });
34
+
35
+ expect(result.current.shouldShowLoadingError).toBe(false);
36
+ });
37
+
38
+ it("should initialize with correct default state for multiple components", () => {
39
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
40
+
41
+ const initialState = result.current.loadingState.getValue();
42
+
43
+ expect(initialState).toEqual({
44
+ index: -1,
45
+ done: false,
46
+ waitForAllComponents: false,
47
+ });
48
+
49
+ expect(result.current.shouldShowLoadingError).toBe(false);
50
+ });
51
+
52
+ it("should return a BehaviorSubject for loadingState", () => {
53
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
54
+
55
+ expect(result.current.loadingState).toBeInstanceOf(BehaviorSubject);
56
+ });
57
+ });
58
+
59
+ describe("arePreviousComponentsLoaded", () => {
60
+ it("should return true for index 0 (first component)", () => {
61
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
62
+
63
+ expect(result.current.arePreviousComponentsLoaded(0)).toBe(true);
64
+ });
65
+
66
+ it("should return false when previous components are not loaded", () => {
67
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
68
+
69
+ expect(result.current.arePreviousComponentsLoaded(1)).toBe(false);
70
+ expect(result.current.arePreviousComponentsLoaded(2)).toBe(false);
71
+ });
72
+
73
+ it("should return true when all previous components are loaded", () => {
74
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
75
+
76
+ act(() => {
77
+ result.current.onLoadFinished(0);
78
+ });
79
+
80
+ expect(result.current.arePreviousComponentsLoaded(1)).toBe(true);
81
+ expect(result.current.arePreviousComponentsLoaded(2)).toBe(false);
82
+
83
+ act(() => {
84
+ result.current.onLoadFinished(1);
85
+ });
86
+
87
+ expect(result.current.arePreviousComponentsLoaded(2)).toBe(true);
88
+ });
89
+ });
90
+
91
+ describe("onLoadFinished", () => {
92
+ it("should update component state and loading state when component finishes loading", () => {
93
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
94
+
95
+ act(() => {
96
+ result.current.onLoadFinished(0);
97
+ });
98
+
99
+ const state = result.current.loadingState.getValue();
100
+ expect(state.index).toBe(0);
101
+ expect(state.done).toBe(false);
102
+ });
103
+
104
+ it("should update index to highest loaded component", () => {
105
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
106
+
107
+ act(() => {
108
+ result.current.onLoadFinished(2);
109
+ });
110
+
111
+ let state = result.current.loadingState.getValue();
112
+ expect(state.index).toBe(2);
113
+
114
+ act(() => {
115
+ result.current.onLoadFinished(1);
116
+ });
117
+
118
+ state = result.current.loadingState.getValue();
119
+ expect(state.index).toBe(2); // Should remain 2, not decrease to 1
120
+ });
121
+
122
+ it("should mark as done when all components are loaded", () => {
123
+ const { result } = renderHook(() => useLoadingState(2, onLoadDone));
124
+
125
+ act(() => {
126
+ result.current.onLoadFinished(0);
127
+ });
128
+
129
+ let state = result.current.loadingState.getValue();
130
+ expect(state.done).toBe(true); // True because arePreviousComponentsLoaded(1) returns true when component 0 is loaded
131
+ expect(onLoadDone).toHaveBeenCalledTimes(1);
132
+
133
+ act(() => {
134
+ result.current.onLoadFinished(1);
135
+ });
136
+
137
+ state = result.current.loadingState.getValue();
138
+ expect(state.done).toBe(true);
139
+ expect(onLoadDone).toHaveBeenCalledTimes(2); // Called again
140
+ });
141
+
142
+ it("should call onLoadDone when count is 0", () => {
143
+ const { result } = renderHook(() => useLoadingState(0, onLoadDone));
144
+
145
+ const state = result.current.loadingState.getValue();
146
+ expect(state.done).toBe(true);
147
+ // onLoadDone is not called on initialization for count 0, only when all components are loaded via dispatch
148
+ });
149
+
150
+ it("should handle loading components out of order", () => {
151
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
152
+
153
+ // Load component 2 first
154
+ act(() => {
155
+ result.current.onLoadFinished(2);
156
+ });
157
+
158
+ let state = result.current.loadingState.getValue();
159
+ expect(state.done).toBe(false);
160
+
161
+ // Load component 0
162
+ act(() => {
163
+ result.current.onLoadFinished(0);
164
+ });
165
+
166
+ state = result.current.loadingState.getValue();
167
+ expect(state.done).toBe(false);
168
+
169
+ // Load component 1 - should complete loading
170
+ act(() => {
171
+ result.current.onLoadFinished(1);
172
+ });
173
+
174
+ state = result.current.loadingState.getValue();
175
+ expect(state.done).toBe(true);
176
+ expect(onLoadDone).toHaveBeenCalledTimes(1);
177
+ });
178
+
179
+ it("should call onLoadDone again on subsequent dispatches", () => {
180
+ const { result } = renderHook(() => useLoadingState(2, onLoadDone));
181
+
182
+ act(() => {
183
+ result.current.onLoadFinished(0);
184
+ result.current.onLoadFinished(1);
185
+ });
186
+
187
+ expect(onLoadDone).toHaveBeenCalledTimes(2); // Called for each dispatch when done is true
188
+
189
+ // Try loading again - onLoadDone will be called again because dispatch runs again
190
+ act(() => {
191
+ result.current.onLoadFinished(0);
192
+ });
193
+
194
+ expect(onLoadDone).toHaveBeenCalledTimes(3); // Will be called again
195
+ });
196
+ });
197
+
198
+ describe("onLoadFailed", () => {
199
+ it("should treat failed components as loaded when SHOULD_FAIL_ON_COMPONENT_LOADING is false", () => {
200
+ const { result } = renderHook(() => useLoadingState(2, onLoadDone));
201
+
202
+ const error = new Error("Load failed");
203
+
204
+ act(() => {
205
+ result.current.onLoadFailed({ error, index: 0 });
206
+ });
207
+
208
+ const state = result.current.loadingState.getValue();
209
+ expect(state.index).toBe(0);
210
+ expect(result.current.shouldShowLoadingError).toBe(false);
211
+ });
212
+
213
+ it("should complete loading when all components fail", () => {
214
+ const { result } = renderHook(() => useLoadingState(2, onLoadDone));
215
+
216
+ const error = new Error("Load failed");
217
+
218
+ act(() => {
219
+ result.current.onLoadFailed({ error, index: 0 });
220
+ result.current.onLoadFailed({ error, index: 1 });
221
+ });
222
+
223
+ const state = result.current.loadingState.getValue();
224
+ expect(state.done).toBe(true);
225
+ expect(onLoadDone).toHaveBeenCalledTimes(2); // Called for each failed component
226
+ });
227
+
228
+ it("should handle mixed success and failure", () => {
229
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone));
230
+
231
+ const error = new Error("Load failed");
232
+
233
+ act(() => {
234
+ result.current.onLoadFinished(0);
235
+ result.current.onLoadFailed({ error, index: 1 });
236
+ result.current.onLoadFinished(2);
237
+ });
238
+
239
+ const state = result.current.loadingState.getValue();
240
+ expect(state.done).toBe(true);
241
+ expect(onLoadDone).toHaveBeenCalledTimes(2); // Called when all components 0,1,2 are handled
242
+ });
243
+ });
244
+
245
+ describe("loading state observable", () => {
246
+ it("should emit state changes through BehaviorSubject", () => {
247
+ const { result } = renderHook(() => useLoadingState(3, onLoadDone)); // Use 3 components so loading component 0 doesn't complete everything
248
+ const mockSubscriber = jest.fn();
249
+
250
+ result.current.loadingState.subscribe(mockSubscriber);
251
+
252
+ act(() => {
253
+ result.current.onLoadFinished(0);
254
+ });
255
+
256
+ // Should have been called twice: initial state + update
257
+ expect(mockSubscriber).toHaveBeenCalledTimes(2);
258
+
259
+ expect(mockSubscriber).toHaveBeenLastCalledWith({
260
+ index: 0,
261
+ done: false, // Will be false because we need components 1 and 2 as well
262
+ waitForAllComponents: false,
263
+ });
264
+ });
265
+
266
+ it("should preserve waitForAllComponents flag in state updates", () => {
267
+ const { result } = renderHook(() => useLoadingState(2, onLoadDone));
268
+
269
+ act(() => {
270
+ result.current.onLoadFinished(0);
271
+ });
272
+
273
+ const state = result.current.loadingState.getValue();
274
+ expect(state.waitForAllComponents).toBe(false);
275
+ });
276
+ });
277
+
278
+ describe("memoization", () => {
279
+ it("should return stable references for functions", () => {
280
+ const { result, rerender } = renderHook(() =>
281
+ useLoadingState(2, onLoadDone)
282
+ );
283
+
284
+ const firstRender = {
285
+ onLoadFinished: result.current.onLoadFinished,
286
+ onLoadFailed: result.current.onLoadFailed,
287
+ arePreviousComponentsLoaded: result.current.arePreviousComponentsLoaded,
288
+ };
289
+
290
+ rerender();
291
+
292
+ expect(result.current.onLoadFinished).toBe(firstRender.onLoadFinished);
293
+ expect(result.current.onLoadFailed).toBe(firstRender.onLoadFailed);
294
+
295
+ expect(result.current.arePreviousComponentsLoaded).toBe(
296
+ firstRender.arePreviousComponentsLoaded
297
+ );
298
+ });
299
+
300
+ it("should return stable function references (current behavior)", () => {
301
+ const { result, rerender } = renderHook(
302
+ ({ onLoadDone }) => useLoadingState(2, onLoadDone),
303
+ { initialProps: { onLoadDone } }
304
+ );
305
+
306
+ const firstResult = result.current;
307
+
308
+ const newOnLoadDone = jest.fn();
309
+ rerender({ onLoadDone: newOnLoadDone });
310
+
311
+ // Functions should remain the same due to empty dependency arrays (this is the current behavior)
312
+ expect(result.current.onLoadFinished).toBe(firstResult.onLoadFinished);
313
+ expect(result.current.onLoadFailed).toBe(firstResult.onLoadFailed);
314
+ });
315
+ });
316
+
317
+ describe("edge cases", () => {
318
+ it("should handle duplicate load finished calls gracefully", () => {
319
+ const { result } = renderHook(() => useLoadingState(2, onLoadDone));
320
+
321
+ act(() => {
322
+ result.current.onLoadFinished(0);
323
+ result.current.onLoadFinished(0); // Duplicate call
324
+ });
325
+
326
+ const state = result.current.loadingState.getValue();
327
+ expect(state.index).toBe(0);
328
+ expect(state.done).toBe(true); // True because loading component 0 makes it done in a 2-component setup
329
+ });
330
+
331
+ it("should handle loading index greater than component count", () => {
332
+ const { result } = renderHook(() => useLoadingState(2, onLoadDone));
333
+
334
+ act(() => {
335
+ result.current.onLoadFinished(5); // Index out of bounds
336
+ });
337
+
338
+ const state = result.current.loadingState.getValue();
339
+ expect(state.index).toBe(5);
340
+ expect(state.done).toBe(false); // Should not be done as not all components loaded
341
+ });
342
+
343
+ it("should handle negative indices", () => {
344
+ const { result } = renderHook(() => useLoadingState(2, onLoadDone));
345
+
346
+ act(() => {
347
+ result.current.onLoadFinished(-1);
348
+ });
349
+
350
+ const state = result.current.loadingState.getValue();
351
+ expect(state.index).toBe(-1); // Should remain -1
352
+ });
353
+ });
354
+
355
+ describe("component count changes", () => {
356
+ it("should handle changing component count", () => {
357
+ const { result, rerender } = renderHook(
358
+ ({ count }) => useLoadingState(count, onLoadDone),
359
+ { initialProps: { count: 2 } }
360
+ );
361
+
362
+ act(() => {
363
+ result.current.onLoadFinished(0);
364
+ });
365
+
366
+ // Change count
367
+ rerender({ count: 3 });
368
+
369
+ // The hook should work with the new count
370
+ act(() => {
371
+ result.current.onLoadFinished(1);
372
+ result.current.onLoadFinished(2);
373
+ });
374
+
375
+ const state = result.current.loadingState.getValue();
376
+ expect(state.done).toBe(true);
377
+ });
378
+ });
379
+ });
@@ -1,5 +1,5 @@
1
1
  import * as React from "react";
2
- import { isNil, set, lensIndex, T, slice } from "ramda";
2
+ import { isNil, set, lensIndex, slice } from "ramda";
3
3
  import { BehaviorSubject } from "rxjs";
4
4
  import { useRefWithInitialValue } from "@applicaster/zapp-react-native-utils/reactHooks/state/useRefWithInitialValue";
5
5
 
@@ -53,7 +53,7 @@ export const useLoadingState = (
53
53
 
54
54
  const componentsBefore = slice(0, index, componentStateRef.current);
55
55
 
56
- return componentsBefore.every(T);
56
+ return componentsBefore.every(Boolean);
57
57
  }, []);
58
58
 
59
59
  const dispatch = React.useCallback(({ payload }) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-ui-components",
3
- "version": "14.0.0-rc.44",
3
+ "version": "14.0.0-rc.45",
4
4
  "description": "Applicaster Zapp React Native ui components for the Quick Brick App",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -28,10 +28,10 @@
28
28
  },
29
29
  "homepage": "https://github.com/applicaster/quickbrick#readme",
30
30
  "dependencies": {
31
- "@applicaster/applicaster-types": "14.0.0-rc.44",
32
- "@applicaster/zapp-react-native-bridge": "14.0.0-rc.44",
33
- "@applicaster/zapp-react-native-redux": "14.0.0-rc.44",
34
- "@applicaster/zapp-react-native-utils": "14.0.0-rc.44",
31
+ "@applicaster/applicaster-types": "14.0.0-rc.45",
32
+ "@applicaster/zapp-react-native-bridge": "14.0.0-rc.45",
33
+ "@applicaster/zapp-react-native-redux": "14.0.0-rc.45",
34
+ "@applicaster/zapp-react-native-utils": "14.0.0-rc.45",
35
35
  "promise": "^8.3.0",
36
36
  "url": "^0.11.0",
37
37
  "uuid": "^3.3.2"