@applicaster/zapp-react-native-utils 17.0.0-alpha.5804334713 → 17.0.0-alpha.9322493301

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,313 @@
1
+ import { localStorage as NativeLocalStorageMock } from "../../../zapp-react-native-bridge/ZappStorage/__mocks__/android";
2
+
3
+ const STATE_KEY = "__applicasterDebugFeatures";
4
+
5
+ type LoadedModule = {
6
+ DebugFeaturesModule: typeof import("../DebugFeatures");
7
+ LegacyModule: typeof import("../DebugFeatues");
8
+ ReactNative: typeof import("react-native");
9
+ };
10
+
11
+ const loadModules = (
12
+ nativeDebugBridge?: Record<string, jest.Mock>
13
+ ): LoadedModule => {
14
+ delete (globalThis as any)[STATE_KEY];
15
+ NativeLocalStorageMock.__clear();
16
+
17
+ let DebugFeaturesModule: typeof import("../DebugFeatures");
18
+ let LegacyModule: typeof import("../DebugFeatues");
19
+ let ReactNative: typeof import("react-native");
20
+
21
+ jest.isolateModules(() => {
22
+ ReactNative = require("react-native");
23
+ ReactNative.NativeModules.LocalStorage = NativeLocalStorageMock;
24
+
25
+ if (nativeDebugBridge) {
26
+ ReactNative.NativeModules.DebugFeaturesBridge = nativeDebugBridge;
27
+ } else {
28
+ delete ReactNative.NativeModules.DebugFeaturesBridge;
29
+ }
30
+
31
+ DebugFeaturesModule = require("../DebugFeatures");
32
+ LegacyModule = require("../DebugFeatues");
33
+ });
34
+
35
+ return {
36
+ DebugFeaturesModule,
37
+ LegacyModule,
38
+ ReactNative,
39
+ };
40
+ };
41
+
42
+ describe("DebugFeatures", () => {
43
+ afterEach(() => {
44
+ delete (globalThis as any)[STATE_KEY];
45
+ const ReactNative = require("react-native");
46
+ delete ReactNative.NativeModules.DebugFeaturesBridge;
47
+ jest.restoreAllMocks();
48
+ });
49
+
50
+ describe("when native DebugFeaturesBridge is available", () => {
51
+ it("delegates registration, reading, writing, and receives native change events", async () => {
52
+ const nativeBridge = {
53
+ registerFeatureFlag: jest.fn().mockResolvedValue(true),
54
+ getFeatures: jest.fn().mockResolvedValue([
55
+ {
56
+ id: "plugin.flag",
57
+ group: "Plugin",
58
+ description: "Test flag",
59
+ initialState: true,
60
+ enabled: true,
61
+ value: "true",
62
+ },
63
+ ]),
64
+ isFeatureFlagEnabled: jest.fn().mockResolvedValue(true),
65
+ toggleFeatureFlag: jest.fn().mockResolvedValue(false),
66
+ getStringValue: jest.fn().mockResolvedValue("123"),
67
+ setStringValue: jest.fn().mockResolvedValue("abc"),
68
+ };
69
+
70
+ const { DebugFeaturesModule, ReactNative } = loadModules(nativeBridge);
71
+ const { default: DebugFeatures, subscribe } = DebugFeaturesModule;
72
+ const instance = DebugFeatures.getInstance();
73
+
74
+ await expect(
75
+ instance.registerFeatureFlag({
76
+ id: "plugin.flag",
77
+ group: "Plugin",
78
+ description: "Test flag",
79
+ initialState: true,
80
+ })
81
+ ).resolves.toBe(true);
82
+
83
+ expect(nativeBridge.registerFeatureFlag).toHaveBeenCalledWith({
84
+ id: "plugin.flag",
85
+ group: "Plugin",
86
+ description: "Test flag",
87
+ initialState: true,
88
+ });
89
+
90
+ await expect(instance.getFeatures()).resolves.toEqual([
91
+ {
92
+ id: "plugin.flag",
93
+ group: "Plugin",
94
+ description: "Test flag",
95
+ initialState: true,
96
+ enabled: true,
97
+ value: "true",
98
+ },
99
+ ]);
100
+
101
+ await expect(instance.isFeatureFlagEnabled("plugin.flag")).resolves.toBe(
102
+ true
103
+ );
104
+
105
+ await expect(
106
+ instance.toggleFeatureFlag("plugin.flag", false)
107
+ ).resolves.toBe(false);
108
+
109
+ await expect(instance.getStringValue("plugin.str")).resolves.toBe("123");
110
+
111
+ await expect(instance.setStringValue("plugin.str", "abc")).resolves.toBe(
112
+ "abc"
113
+ );
114
+
115
+ const globalEvents = [];
116
+ const keyEvents = [];
117
+ const unsubGlobal = subscribe((event) => globalEvents.push(event));
118
+
119
+ const unsubKey = subscribe(
120
+ (event) => keyEvents.push(event),
121
+ "plugin.flag"
122
+ );
123
+
124
+ ReactNative.DeviceEventEmitter.emit("DebugFeatures.changed", {
125
+ id: "plugin.flag",
126
+ value: "true",
127
+ enabled: true,
128
+ });
129
+
130
+ ReactNative.DeviceEventEmitter.emit("DebugFeatures.changed", {
131
+ id: "other.flag",
132
+ value: "hello",
133
+ enabled: false,
134
+ });
135
+
136
+ expect(globalEvents).toEqual([
137
+ { id: "plugin.flag", value: "true", enabled: true },
138
+ { id: "other.flag", value: "hello", enabled: false },
139
+ ]);
140
+
141
+ expect(keyEvents).toEqual([
142
+ { id: "plugin.flag", value: "true", enabled: true },
143
+ ]);
144
+
145
+ unsubGlobal();
146
+ unsubKey();
147
+
148
+ ReactNative.DeviceEventEmitter.emit("DebugFeatures.changed", {
149
+ id: "plugin.flag",
150
+ value: "false",
151
+ enabled: false,
152
+ });
153
+
154
+ expect(globalEvents).toHaveLength(2);
155
+ expect(keyEvents).toHaveLength(1);
156
+ });
157
+ });
158
+
159
+ describe("when native DebugFeaturesBridge is missing", () => {
160
+ it("falls back to LocalStorage and in-JS registry and notifies subscribers", async () => {
161
+ const { DebugFeaturesModule, LegacyModule } = loadModules();
162
+
163
+ const {
164
+ default: DebugFeatures,
165
+ registerFeatureFlag,
166
+ getFeatures,
167
+ isFeatureFlagEnabled,
168
+ toggleFeatureFlag,
169
+ getStringValue,
170
+ setStringValue,
171
+ subscribe,
172
+ } = DebugFeaturesModule;
173
+
174
+ const globalListener = jest.fn();
175
+ const keyListener = jest.fn();
176
+ const unsubGlobal = subscribe(globalListener);
177
+ const unsubKey = subscribe(keyListener, "flag.initial_true");
178
+
179
+ await expect(
180
+ registerFeatureFlag({
181
+ id: "flag.initial_true",
182
+ group: "Debug",
183
+ description: "Starts enabled",
184
+ initialState: true,
185
+ })
186
+ ).resolves.toBe(true);
187
+
188
+ await expect(
189
+ registerFeatureFlag({
190
+ id: "flag.default_off",
191
+ group: "Debug",
192
+ description: "Starts disabled",
193
+ })
194
+ ).resolves.toBe(false);
195
+
196
+ expect(globalListener).toHaveBeenCalledWith({
197
+ id: "flag.initial_true",
198
+ value: "true",
199
+ enabled: true,
200
+ });
201
+
202
+ expect(keyListener).toHaveBeenCalledWith({
203
+ id: "flag.initial_true",
204
+ value: "true",
205
+ enabled: true,
206
+ });
207
+
208
+ // Re-registering initialState: true does not overwrite an explicit false
209
+ await expect(toggleFeatureFlag("flag.initial_true", false)).resolves.toBe(
210
+ false
211
+ );
212
+
213
+ await expect(
214
+ registerFeatureFlag({
215
+ id: "flag.initial_true",
216
+ group: "Debug",
217
+ description: "Starts enabled",
218
+ initialState: true,
219
+ })
220
+ ).resolves.toBe(false);
221
+
222
+ await expect(isFeatureFlagEnabled("flag.initial_true")).resolves.toBe(
223
+ false
224
+ );
225
+
226
+ await expect(
227
+ LegacyModule.default
228
+ .getInstance()
229
+ .isFeatureFlagEnabled("flag.initial_true")
230
+ ).resolves.toBe(false);
231
+
232
+ await expect(
233
+ DebugFeatures.getInstance().isFeatureFlagEnabled("flag.default_off")
234
+ ).resolves.toBe(false);
235
+
236
+ await expect(setStringValue("flag.str", "123")).resolves.toBe("123");
237
+ await expect(getStringValue("flag.str")).resolves.toBe("123");
238
+ await expect(setStringValue("flag.str", "")).resolves.toBeNull();
239
+ await expect(getStringValue("flag.str")).resolves.toBeNull();
240
+
241
+ await expect(getFeatures()).resolves.toEqual([
242
+ {
243
+ id: "flag.default_off",
244
+ group: "Debug",
245
+ description: "Starts disabled",
246
+ initialState: false,
247
+ enabled: false,
248
+ value: null,
249
+ },
250
+ {
251
+ id: "flag.initial_true",
252
+ group: "Debug",
253
+ description: "Starts enabled",
254
+ initialState: true,
255
+ enabled: false,
256
+ value: "false",
257
+ },
258
+ ]);
259
+
260
+ expect(globalListener).toHaveBeenLastCalledWith({
261
+ id: "flag.str",
262
+ value: null,
263
+ enabled: false,
264
+ });
265
+
266
+ unsubGlobal();
267
+ unsubKey();
268
+ });
269
+
270
+ it("logs an error via X-Ray logger when native module is present but a method is missing", async () => {
271
+ const logErrorMock = jest.fn();
272
+
273
+ jest.doMock("../../logger", () => ({
274
+ ...jest.requireActual("../../logger"),
275
+ createLogger: () => ({
276
+ log_verbose: jest.fn(),
277
+ log_debug: jest.fn(),
278
+ log_info: jest.fn(),
279
+ log_warning: jest.fn(),
280
+ log_error: logErrorMock,
281
+ }),
282
+ }));
283
+
284
+ const partialBridge = {
285
+ registerFeatureFlag: jest.fn().mockResolvedValue(true),
286
+ };
287
+
288
+ const { DebugFeaturesModule } = loadModules(partialBridge);
289
+
290
+ const { registerFeatureFlag, isFeatureFlagEnabled, getStringValue } =
291
+ DebugFeaturesModule;
292
+
293
+ try {
294
+ await expect(
295
+ registerFeatureFlag({ id: "flag.partial", initialState: true })
296
+ ).resolves.toBe(true);
297
+
298
+ await expect(isFeatureFlagEnabled("flag.partial")).resolves.toBe(false);
299
+ await expect(getStringValue("flag.partial")).resolves.toBeNull();
300
+
301
+ expect(logErrorMock).toHaveBeenCalledWith(
302
+ "DebugFeaturesBridge.isFeatureFlagEnabled is missing on native module"
303
+ );
304
+
305
+ expect(logErrorMock).toHaveBeenCalledWith(
306
+ "DebugFeaturesBridge.getStringValue is missing on native module"
307
+ );
308
+ } finally {
309
+ jest.dontMock("../../logger");
310
+ }
311
+ });
312
+ });
313
+ });
@@ -0,0 +1,3 @@
1
+ export * from "./DebugFeatures";
2
+
3
+ export { default } from "./DebugFeatures";
@@ -14,6 +14,8 @@ export const getParsedResponse = (err): XRayEventData => {
14
14
 
15
15
  const xRayData: XRayEventData = {
16
16
  data: {
17
+ // What X-Ray diagnostics read is top level in `data`: the error's context carries it.
18
+ diagnostic: context?.diagnostic,
17
19
  context,
18
20
  name,
19
21
  params,
@@ -222,30 +222,37 @@ class FocusManager {
222
222
  return () => this.unregisterFocusable(focusableId);
223
223
  }
224
224
 
225
- unregisterFocusable(focusableId: string) {
225
+ unregisterFocusable(component: string | FocusManager.TouchableReactRef) {
226
+ const focusableId = isString(component)
227
+ ? component
228
+ : getFocusableId(component);
229
+
226
230
  const node = this.tree.find(focusableId);
227
231
  const parentNode = this.tree.findParent(focusableId);
228
232
 
233
+ const removesFocusedSubtree =
234
+ this._focusedId === focusableId ||
235
+ !!(this._focusedId && node?.findNode(this._focusedId));
236
+
229
237
  this.focusableComponents = this.focusableComponents.filter(
230
- (c) => c !== node?.component
238
+ (c) => c !== node?.component && getFocusableId(c) !== focusableId
231
239
  );
232
240
 
233
241
  this.tree.remove(focusableId);
234
242
 
235
- if (this._focusedId === focusableId) {
243
+ if (removesFocusedSubtree) {
236
244
  this._focusedId = null;
237
245
  this.retainFocus(parentNode);
238
246
  }
239
247
  }
240
248
 
241
- // Empty groups drop their focusable list after the feed fails, which is
242
- // after Layout's one-shot focus pass. Land on the nearest ancestor that is
243
- // still mounted so key handlers keep a target.
249
+ // Recover before parent-first unmounts discard the focused child's ancestry.
250
+ // Land on a mounted ancestor so key handlers keep a target.
244
251
  private retainFocus(node: TreeNode | null) {
245
252
  let current = node;
246
253
 
247
254
  while (current) {
248
- const ref = current.component;
255
+ const ref = FocusManager.findFocusable(current.id);
249
256
 
250
257
  if (ref?.current?.props?.id) {
251
258
  this.setFocusLocal(ref);
@@ -444,13 +451,17 @@ class FocusManager {
444
451
  }
445
452
 
446
453
  private hasFocus = (node) => {
447
- if (node.children?.length > 0) {
448
- return node.children.some((item) => this.hasFocus(item));
449
- } else if (this.isFocused(node?.component?.current?.props.id)) {
454
+ const propsId = node?.component?.current?.props?.id;
455
+
456
+ // A focused cell keeps its buttons as children. Count that cell, not only leaves.
457
+ if (
458
+ this.isFocused(propsId) ||
459
+ (node?.id !== propsId && this.isFocused(node?.id))
460
+ ) {
450
461
  return true;
451
462
  }
452
463
 
453
- return false;
464
+ return node.children?.some((child) => this.hasFocus(child)) ?? false;
454
465
  };
455
466
 
456
467
  isAnyDescendantFocused(id: string) {
@@ -1,4 +1,7 @@
1
1
  import { focusManager } from "../FocusManager";
2
+ import { Touchable } from "@applicaster/zapp-react-native-ui-components/Components/Focusable/Touchable";
3
+ import { Tree } from "../Tree";
4
+ import { TreeNode } from "../TreeNode";
2
5
 
3
6
  const isFocusableCell = true;
4
7
  const parentFocusableId = "parentFocusableId";
@@ -120,11 +123,11 @@ describe("FocusManager", () => {
120
123
  /* check state of focus */
121
124
  expect(focusManager.isAnyDescendantFocused("group")).toBe(false);
122
125
 
123
- /* set focus on 2nd child */
126
+ /* set focus on 2nd child, which itself has children */
124
127
  focusManager.setFocus(child2.current.props.id);
125
128
 
126
- /* check state of focus */
127
- expect(focusManager.isAnyDescendantFocused("group")).toBe(false);
129
+ /* the focused node is the child, not one of its leaves */
130
+ expect(focusManager.isAnyDescendantFocused("group")).toBe(true);
128
131
  });
129
132
 
130
133
  it("focusManager isFocusableChildOf should be defined", () => {
@@ -266,3 +269,112 @@ describe("FocusManager", () => {
266
269
  expect(focusManager.focusedId).toBe("child2");
267
270
  });
268
271
  });
272
+
273
+ describe("FocusManager subtree replacement", () => {
274
+ const createFocusable = (id: string) => ({
275
+ current: new Touchable({ id, children: null, onFocus: jest.fn() }),
276
+ });
277
+
278
+ let root: FocusManager.TouchableReactRef;
279
+ let rail: FocusManager.TouchableReactRef;
280
+ let cell: FocusManager.TouchableReactRef;
281
+ let sibling: FocusManager.TouchableReactRef;
282
+
283
+ const register = (
284
+ touchableRef: FocusManager.TouchableReactRef,
285
+ parentFocusableRef: FocusManager.TouchableReactRef
286
+ ) =>
287
+ focusManager.registerFocusable({
288
+ touchableRef,
289
+ parentFocusableRef,
290
+ isFocusableCell: true,
291
+ parentFocusableId: "",
292
+ });
293
+
294
+ beforeEach(() => {
295
+ root = createFocusable("replacement-root");
296
+ rail = createFocusable("replacement-rail");
297
+ cell = createFocusable("replacement-cell");
298
+ sibling = createFocusable("replacement-sibling");
299
+ register(root, { current: null });
300
+ register(rail, root);
301
+ register(cell, rail);
302
+ register(sibling, root);
303
+ focusManager.setFocus("replacement-cell");
304
+ });
305
+
306
+ afterEach(() => {
307
+ [
308
+ "replacement-cell",
309
+ "replacement-rail",
310
+ "replacement-sibling",
311
+ "replacement-root",
312
+ ].forEach((id) => focusManager.unregisterFocusable(id));
313
+
314
+ focusManager.resetFocus();
315
+ });
316
+
317
+ it("retains focus before a parent-first unmount removes the focused descendant's ancestry", () => {
318
+ focusManager.unregisterFocusable("replacement-rail");
319
+
320
+ expect(focusManager.focusedId).toBe("replacement-root");
321
+ expect(focusManager.focused).toBe(root.current);
322
+
323
+ focusManager.unregisterFocusable("replacement-cell");
324
+
325
+ expect(focusManager.focused).toBe(root.current);
326
+ });
327
+
328
+ it("removes descendant registrations even after their parent removed the tree nodes", () => {
329
+ focusManager.unregisterFocusable("replacement-rail");
330
+ focusManager.unregisterFocusable("replacement-cell");
331
+
332
+ expect(focusManager.focusableComponents).not.toContain(cell);
333
+ expect(focusManager.focusableComponents).not.toContain(rail);
334
+
335
+ const replacementRail = createFocusable("replacement-rail");
336
+ const replacementCell = createFocusable("replacement-cell");
337
+ register(replacementRail, root);
338
+ register(replacementCell, replacementRail);
339
+ focusManager.setFocus("replacement-cell");
340
+
341
+ expect(focusManager.focused).toBe(replacementCell.current);
342
+ expect(focusManager.isFocusableChildOf(replacementCell, root)).toBe(true);
343
+ });
344
+
345
+ it("does not move focus when an unrelated subtree unregisters", () => {
346
+ focusManager.setFocus("replacement-sibling");
347
+ focusManager.unregisterFocusable("replacement-rail");
348
+ focusManager.unregisterFocusable("replacement-cell");
349
+
350
+ expect(focusManager.focused).toBe(sibling.current);
351
+ });
352
+
353
+ it("resolves a placeholder ancestor through its mounted registration", () => {
354
+ const findParent = jest
355
+ .spyOn(Tree.prototype, "findParent")
356
+ .mockReturnValueOnce(new TreeNode(null, "replacement-root", "", false));
357
+
358
+ focusManager.unregisterFocusable("replacement-rail");
359
+ findParent.mockRestore();
360
+
361
+ expect(focusManager.focused).toBe(root.current);
362
+ });
363
+
364
+ it("retains focus through child-first cleanup", () => {
365
+ focusManager.unregisterFocusable("replacement-cell");
366
+ expect(focusManager.focused).toBe(rail.current);
367
+
368
+ focusManager.unregisterFocusable("replacement-rail");
369
+ expect(focusManager.focused).toBe(root.current);
370
+ });
371
+
372
+ it("clears focus when the entire root containing the focused cell unregisters", () => {
373
+ focusManager.unregisterFocusable("replacement-root");
374
+ focusManager.unregisterFocusable("replacement-rail");
375
+ focusManager.unregisterFocusable("replacement-cell");
376
+
377
+ expect(focusManager.focusedId).toBeNull();
378
+ expect(focusManager.focused).toBeNull();
379
+ });
380
+ });