@openpeepshq/rn-components 0.12.4 → 0.12.5

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,334 @@
1
+ /**
2
+ * Regression tests for the folder-tree collision (issue #2148) and the
3
+ * RN CategoryPathPicker's web-parity semantics.
4
+ *
5
+ * A folder literally named `[TEST] A/B` and a folder `[TEST] A` containing
6
+ * a subfolder `B` used to produce the same `/`-joined identity key, so the
7
+ * two collapsed into one tree node (counts 2 instead of 1/1, shared
8
+ * expand/selection). The picker test locks in the maintainer's ask: typing
9
+ * `A/B` into "Add a folder" must report a single segment, never split.
10
+ */
11
+
12
+ const React = require('react');
13
+ const ReactTestRenderer = require('react-test-renderer');
14
+
15
+ let mockTranslations: Record<string, string> = {};
16
+ let mockLibrary: Record<string, unknown> = {};
17
+
18
+ jest.mock('react-native', () => ({
19
+ View: (props: { children?: unknown }) =>
20
+ React.createElement('View', null, props.children),
21
+ Pressable: (props: {
22
+ children?: unknown;
23
+ onPress?: () => void;
24
+ accessibilityLabel?: string;
25
+ accessibilityRole?: string;
26
+ }) =>
27
+ React.createElement(
28
+ 'Pressable',
29
+ {
30
+ onPress: props.onPress,
31
+ accessibilityLabel: props.accessibilityLabel,
32
+ accessibilityRole: props.accessibilityRole,
33
+ },
34
+ props.children
35
+ ),
36
+ ScrollView: (props: { children?: unknown }) =>
37
+ React.createElement('ScrollView', null, props.children),
38
+ TextInput: (props: {
39
+ value?: string;
40
+ onChangeText?: (text: string) => void;
41
+ placeholder?: string;
42
+ }) =>
43
+ React.createElement('TextInput', {
44
+ value: props.value,
45
+ onChangeText: props.onChangeText,
46
+ placeholder: props.placeholder,
47
+ }),
48
+ }));
49
+
50
+ jest.mock('react-i18next', () => ({
51
+ useTranslation: () => ({
52
+ t: (key: string, opts?: { defaultValue?: string }) =>
53
+ mockTranslations[key] ?? opts?.defaultValue ?? key,
54
+ }),
55
+ }));
56
+
57
+ jest.mock('@react-navigation/native', () => ({
58
+ useNavigation: () => ({ navigate: jest.fn() }),
59
+ }));
60
+
61
+ jest.mock('@openpeepshq/common/lib', () => ({
62
+ // Mirrored from platform/common/src/lib/resources.ts — the built
63
+ // package ships ESM which Jest's node transform doesn't cover.
64
+ categoryPathKey: (path: readonly string[]): string => JSON.stringify(path),
65
+ RESOURCE_KINDS: ['video', 'file', 'gallery', 'audio', 'link'],
66
+ }));
67
+
68
+ jest.mock('@openpeepshq/react', () => ({
69
+ useResourceLibrary: () => mockLibrary,
70
+ }));
71
+
72
+ jest.mock('../ui/input', () => ({
73
+ Input: (props: Record<string, unknown>) =>
74
+ React.createElement('TextInput', props),
75
+ }));
76
+
77
+ jest.mock('../ui/loading-spinner', () => ({
78
+ LoadingSpinner: () => null,
79
+ }));
80
+
81
+ jest.mock('../ui/themed-text', () => ({
82
+ ThemedText: (props: { children?: unknown }) =>
83
+ React.createElement('ThemedText', null, props.children),
84
+ }));
85
+
86
+ jest.mock('../icons', () => ({
87
+ LayoutGridIcon: () => null,
88
+ LayoutListIcon: () => null,
89
+ BookmarkIcon: () => null,
90
+ ChevronDownIcon: () => null,
91
+ ChevronRightIcon: () => null,
92
+ }));
93
+
94
+ jest.mock('./ResourceCard', () => ({
95
+ ResourceCard: () => null,
96
+ }));
97
+
98
+ import type { CategoryPathPickerProps } from './CategoryPathPicker';
99
+ import { CategoryPathPicker } from './CategoryPathPicker';
100
+ import type { ResourceLibraryProps } from './ResourceLibrary';
101
+ import { ResourceLibrary } from './ResourceLibrary';
102
+
103
+ const slashPost = {
104
+ id: 'p-slash',
105
+ type: 'resource',
106
+ data: {
107
+ type: 'resource',
108
+ title: 'Slash folder',
109
+ categoryPath: ['[TEST] A/B'],
110
+ },
111
+ };
112
+
113
+ const nestedPost = {
114
+ id: 'p-nested',
115
+ type: 'resource',
116
+ data: {
117
+ type: 'resource',
118
+ title: 'Nested folder',
119
+ categoryPath: ['[TEST] A', 'B'],
120
+ },
121
+ };
122
+
123
+ const libraryFor = (posts: unknown[]) => ({
124
+ posts,
125
+ tree: [
126
+ {
127
+ name: '[TEST] A/B',
128
+ path: ['[TEST] A/B'],
129
+ children: [],
130
+ },
131
+ {
132
+ name: '[TEST] A',
133
+ path: ['[TEST] A'],
134
+ children: [{ name: 'B', path: ['[TEST] A', 'B'], children: [] }],
135
+ },
136
+ ],
137
+ tags: [],
138
+ pathPrefix: [] as string[],
139
+ hierarchy: true,
140
+ setPathPrefix: jest.fn(),
141
+ setHierarchy: jest.fn(),
142
+ query: { isLoading: false },
143
+ search: '',
144
+ setSearch: jest.fn(),
145
+ kind: 'all',
146
+ setKind: jest.fn(),
147
+ view: 'list',
148
+ setView: jest.fn(),
149
+ saved: false,
150
+ setSaved: jest.fn(),
151
+ selectedTags: [] as string[],
152
+ toggleTag: jest.fn(),
153
+ tagsOpen: false,
154
+ setTagsOpen: jest.fn(),
155
+ activeFilterCount: 0,
156
+ });
157
+
158
+ const render = (element: React.ReactElement) => {
159
+ let tree: ReturnType<typeof ReactTestRenderer.create>;
160
+ ReactTestRenderer.act(() => {
161
+ tree = ReactTestRenderer.create(element);
162
+ });
163
+ return tree!;
164
+ };
165
+
166
+ const pressables = (tree: ReturnType<typeof ReactTestRenderer.create>) =>
167
+ tree.root.findAllByType('Pressable');
168
+
169
+ const pressLabel = (
170
+ tree: ReturnType<typeof ReactTestRenderer.create>,
171
+ label: string
172
+ ) => {
173
+ const targets = pressables(tree).filter(
174
+ (node: { props: { accessibilityLabel?: string } }) =>
175
+ node.props.accessibilityLabel === label
176
+ );
177
+ ReactTestRenderer.act(() => {
178
+ targets.forEach((node: { props: { onPress?: () => void } }) =>
179
+ node.props.onPress?.()
180
+ );
181
+ });
182
+ };
183
+
184
+ const textOf = (node: unknown): string => {
185
+ const props = (node as { props?: { children?: unknown } }).props;
186
+ if (!props) return '';
187
+ return React.Children.toArray(props.children ?? [])
188
+ .map((child) =>
189
+ typeof child === 'string'
190
+ ? child
191
+ : textOf(child as unknown as { props?: { children?: unknown } })
192
+ )
193
+ .join('');
194
+ };
195
+
196
+ describe('ResourceLibrary folder tree collision (issue #2148)', () => {
197
+ beforeEach(() => {
198
+ mockTranslations = {};
199
+ mockLibrary = libraryFor([slashPost, nestedPost]);
200
+ });
201
+
202
+ it('renders two separate top-level nodes with counts of 1 each', () => {
203
+ const tree = render(
204
+ React.createElement(ResourceLibrary, {
205
+ groupId: 'g1',
206
+ } as ResourceLibraryProps)
207
+ );
208
+
209
+ const labels = pressables(tree).map(
210
+ (node: { props: { accessibilityLabel?: string } }) =>
211
+ node.props.accessibilityLabel
212
+ );
213
+ expect(labels).toContain('Hide folder tree');
214
+ expect(labels).toContain('Expand folder');
215
+
216
+ // The tree collapses behind an "All folders" root; expand it so the
217
+ // two top-level nodes render.
218
+ pressLabel(tree, 'Expand folder');
219
+
220
+ const texts = tree.root
221
+ .findAll((node: { props?: { children?: unknown } }) =>
222
+ Array.isArray(node.props?.children)
223
+ )
224
+ .map(textOf);
225
+ const flat = texts.join('|');
226
+ // The name renders as one leaf ("[TEST] A/B") and the count as another
227
+ // ("(1)"), separated by nested text nodes, so assert them independently.
228
+ expect(flat).toContain('[TEST] A/B');
229
+ expect(flat).toContain('[TEST] A');
230
+ const counts = flat.match(/\(\d+\)/g) ?? [];
231
+ expect(counts).not.toContain('(2)');
232
+ });
233
+
234
+ it('selects the slash-named folder without highlighting the nested one', () => {
235
+ const tree = render(
236
+ React.createElement(ResourceLibrary, {
237
+ groupId: 'g1',
238
+ } as ResourceLibraryProps)
239
+ );
240
+
241
+ // The tree collapses behind an "All folders" root; expand it so the
242
+ // slash-named top-level node renders.
243
+ pressLabel(tree, 'Expand folder');
244
+
245
+ // Selecting is a pathPrefix change; the slash-named folder's path must
246
+ // be passed through as a single segment.
247
+ const slashNode = pressables(tree).find(
248
+ (node: { props: { children?: unknown } }) =>
249
+ textOf(node).startsWith('[TEST] A/B')
250
+ );
251
+ expect(slashNode).toBeTruthy();
252
+ ReactTestRenderer.act(() => {
253
+ slashNode!.props.onPress();
254
+ });
255
+
256
+ const setPathPrefix = mockLibrary.setPathPrefix as jest.Mock;
257
+ expect(setPathPrefix).toHaveBeenCalledWith(['[TEST] A/B']);
258
+ });
259
+ });
260
+
261
+ describe('CategoryPathPicker RN parity', () => {
262
+ beforeEach(() => {
263
+ mockTranslations = {};
264
+ });
265
+
266
+ it('reports a single segment for a slash-containing folder name (never splits)', () => {
267
+ const onChange = jest.fn();
268
+ const tree = render(
269
+ React.createElement(CategoryPathPicker, {
270
+ path: [],
271
+ tree: [{ name: 'A', path: ['A'], children: [] }],
272
+ onChange,
273
+ } as unknown as CategoryPathPickerProps)
274
+ );
275
+
276
+ const input = tree.root.findAllByType('TextInput')[0];
277
+ ReactTestRenderer.act(() => {
278
+ input.props.onChangeText('A/B');
279
+ });
280
+
281
+ const add = tree.root
282
+ .findAllByType('Pressable')
283
+ .find(
284
+ (node: { props: { accessibilityLabel?: string } }) =>
285
+ node.props.accessibilityLabel === 'Add'
286
+ );
287
+ expect(add).toBeTruthy();
288
+ ReactTestRenderer.act(() => {
289
+ add!.props.onPress();
290
+ });
291
+
292
+ expect(onChange).toHaveBeenCalledWith(['A/B']);
293
+ expect(onChange).not.toHaveBeenCalledWith(['A', 'B']);
294
+ });
295
+
296
+ it('ignores blank input and selects an existing folder from the tree', () => {
297
+ const onChange = jest.fn();
298
+ const tree = render(
299
+ React.createElement(CategoryPathPicker, {
300
+ path: [],
301
+ tree: [
302
+ {
303
+ name: '[TEST] A',
304
+ path: ['[TEST] A'],
305
+ children: [{ name: 'B', path: ['[TEST] A', 'B'], children: [] }],
306
+ },
307
+ ],
308
+ onChange,
309
+ } as unknown as CategoryPathPickerProps)
310
+ );
311
+
312
+ // Blank Add must not fire.
313
+ const add = tree.root
314
+ .findAllByType('Pressable')
315
+ .find(
316
+ (node: { props: { accessibilityLabel?: string } }) =>
317
+ node.props.accessibilityLabel === 'Add'
318
+ );
319
+ ReactTestRenderer.act(() => {
320
+ add!.props.onPress();
321
+ });
322
+ expect(onChange).not.toHaveBeenCalled();
323
+
324
+ // Tapping the nested folder selects its full path.
325
+ const nested = pressables(tree).find(
326
+ (node: { props: { children?: unknown } }) => textOf(node) === 'B'
327
+ );
328
+ expect(nested).toBeTruthy();
329
+ ReactTestRenderer.act(() => {
330
+ nested!.props.onPress();
331
+ });
332
+ expect(onChange).toHaveBeenCalledWith(['[TEST] A', 'B']);
333
+ });
334
+ });
@@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
4
4
  import { useNavigation } from '@react-navigation/native';
5
5
  import { NativeStackNavigationProp } from '@react-navigation/native-stack';
6
6
  import type { ResourceKind } from '@openpeepshq/common/types';
7
- import { RESOURCE_KINDS } from '@openpeepshq/common/lib';
7
+ import { RESOURCE_KINDS, categoryPathKey } from '@openpeepshq/common/lib';
8
8
  import type { CategoryNode } from '@openpeepshq/common/lib';
9
9
  import { useResourceLibrary } from '@openpeepshq/react';
10
10
  import { MainStackParamList } from '../navigation/types/index';
@@ -45,10 +45,10 @@ const Tree = ({
45
45
  }) => (
46
46
  <View className="gap-y-1">
47
47
  {nodes.map((node) => {
48
- const nodeKey = node.path.join('/');
48
+ const nodeKey = categoryPathKey(node.path);
49
49
  const nodeCount = counts.get(nodeKey) ?? 0;
50
50
  const isExpanded = expanded.has(nodeKey);
51
- const isSelected = selected.join('/') === nodeKey;
51
+ const isSelected = categoryPathKey(selected) === nodeKey;
52
52
  const hasChildren = node.children.length > 0;
53
53
  return (
54
54
  <View key={nodeKey}>
@@ -133,11 +133,11 @@ export const ResourceLibrary = ({ groupId }: ResourceLibraryProps) => {
133
133
  if (!path?.length) continue;
134
134
  rootCount++;
135
135
  for (let i = 1; i <= path.length; i++) {
136
- const key = path.slice(0, i).join('/');
136
+ const key = categoryPathKey(path.slice(0, i));
137
137
  counts.set(key, (counts.get(key) ?? 0) + 1);
138
138
  }
139
139
  }
140
- counts.set('', rootCount);
140
+ counts.set(categoryPathKey([]), rootCount);
141
141
  return counts;
142
142
  }, [library.posts]);
143
143
 
@@ -2,3 +2,5 @@ export { ResourceLibrary } from './ResourceLibrary';
2
2
  export type { ResourceLibraryProps } from './ResourceLibrary';
3
3
  export { ResourceCard } from './ResourceCard';
4
4
  export type { ResourceCardProps } from './ResourceCard';
5
+ export { CategoryPathPicker } from './CategoryPathPicker';
6
+ export type { CategoryPathPickerProps } from './CategoryPathPicker';