@openpeepshq/rn-components 0.12.0 → 0.12.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/rn-components",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "description": "React Native community screens and shared UI (extracted from the Black Ambition app) for the OpenPeeps stack.",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./src/index.ts",
@@ -0,0 +1,262 @@
1
+ /**
2
+ * Regression tests for the ResourceCard download action (list layout).
3
+ *
4
+ * The action used to be enabled with `actionLabel === 'Download'`,
5
+ * comparing the *translated* label against the English copy. In any
6
+ * non-English locale the comparison fails and the download action
7
+ * silently degrades to inert text — resources could no longer be
8
+ * opened/downloaded (issue #1820). The gate must use the stable
9
+ * `resource.resourceKind === 'file'` property, mirroring the web card.
10
+ */
11
+
12
+ const React = require('react');
13
+ const ReactTestRenderer = require('react-test-renderer');
14
+
15
+ let mockTranslations: Record<string, string> = {};
16
+
17
+ jest.mock('react-native', () => ({
18
+ View: (props: { children?: unknown }) =>
19
+ React.createElement('View', null, props.children),
20
+ Pressable: (props: {
21
+ children?: unknown;
22
+ onPress?: () => void;
23
+ disabled?: boolean;
24
+ }) =>
25
+ React.createElement(
26
+ 'Pressable',
27
+ { onPress: props.onPress, disabled: props.disabled },
28
+ props.children
29
+ ),
30
+ Linking: { openURL: jest.fn() },
31
+ }));
32
+
33
+ jest.mock('react-i18next', () => ({
34
+ useTranslation: () => ({
35
+ t: (key: string) => mockTranslations[key] ?? key,
36
+ }),
37
+ }));
38
+
39
+ jest.mock('@react-navigation/native', () => ({
40
+ useNavigation: () => ({ navigate: jest.fn() }),
41
+ }));
42
+
43
+ jest.mock('@openpeepshq/common/lib', () => {
44
+ // Mirrored from platform/common/src/lib/resources.ts — the built
45
+ // package ships ESM which Jest's node transform doesn't cover.
46
+ const attachmentMediaKind = (att: {
47
+ type?: string;
48
+ meta?: { mimetype?: string };
49
+ }): string => {
50
+ const mime = att.meta?.mimetype ?? '';
51
+ const type = att.type ?? '';
52
+ if (type === 'image' || mime.startsWith('image/')) return 'image';
53
+ if (type === 'video' || mime.startsWith('video/')) return 'video';
54
+ if (type === 'audio' || mime.startsWith('audio/')) return 'audio';
55
+ return 'file';
56
+ };
57
+ const formatSize = (bytes: number): string => `${bytes} B`;
58
+ const normalizeResourceTags = (
59
+ tags: readonly string[] | undefined
60
+ ): string[] => {
61
+ const seen = new Set<string>();
62
+ const out: string[] = [];
63
+ for (const raw of tags ?? []) {
64
+ const tag = raw.replace(/^#/, '').trim().toLowerCase();
65
+ if (!tag || seen.has(tag)) continue;
66
+ seen.add(tag);
67
+ out.push(tag);
68
+ }
69
+ return out;
70
+ };
71
+ return {
72
+ attachmentMediaKind,
73
+ formatSize,
74
+ groupName: (group: { name?: string } | null) => group?.name,
75
+ normalizeResourceTags,
76
+ };
77
+ });
78
+
79
+ jest.mock('@openpeepshq/react', () => ({
80
+ useOpenpeeps: () => ({
81
+ openpeepsApi: {
82
+ useCurrentProfile: () => ({ data: null }),
83
+ useCurrentProfileBookmarkedIds: () => ({ data: undefined }),
84
+ bookmarkPostAction: () => () => {},
85
+ unbookmarkPostAction: () => () => {},
86
+ },
87
+ }),
88
+ }));
89
+
90
+ jest.mock('../navigation/helpers', () => ({
91
+ buildGoto: () => jest.fn(),
92
+ }));
93
+
94
+ jest.mock('../custom/common', () => ({
95
+ CachedImage: () => null,
96
+ }));
97
+
98
+ jest.mock('../ui/themed-text', () => ({
99
+ ThemedText: (props: { children?: unknown }) =>
100
+ React.createElement('ThemedText', null, props.children),
101
+ }));
102
+
103
+ jest.mock('../post/VideoPlayOverlay', () => ({
104
+ VideoPlayOverlay: () => null,
105
+ }));
106
+
107
+ jest.mock('../post/TruncatedMarkdown', () => ({
108
+ TruncatedMarkdown: () => null,
109
+ }));
110
+
111
+ jest.mock('../icons', () => ({
112
+ BookmarkPlusIcon: () => React.createElement('BookmarkPlusIcon'),
113
+ BookmarkMinusIcon: () => React.createElement('BookmarkMinusIcon'),
114
+ LinkIcon: () => React.createElement('LinkIcon'),
115
+ ImageOffIcon: () => React.createElement('ImageOffIcon'),
116
+ AudioLinesIcon: () => React.createElement('AudioLinesIcon'),
117
+ }));
118
+
119
+ import type { ResourceCardProps } from './ResourceCard';
120
+ import { ResourceCard } from './ResourceCard';
121
+
122
+ const Linking = require('react-native').Linking;
123
+
124
+ const EN = {
125
+ 'resources.actions.download': 'Download',
126
+ 'resources.actions.watch': 'Watch',
127
+ 'resources.kinds.file': 'File',
128
+ 'resources.kinds.video': 'Video',
129
+ };
130
+
131
+ // German stand-in: the translated label never equals the English copy.
132
+ const DE = {
133
+ 'resources.actions.download': 'Herunterladen',
134
+ 'resources.actions.watch': 'Ansehen',
135
+ 'resources.kinds.file': 'Datei',
136
+ 'resources.kinds.video': 'Video',
137
+ };
138
+
139
+ const resourcePost = (
140
+ data: Record<string, unknown>
141
+ ): ResourceCardProps['post'] =>
142
+ ({
143
+ id: 'post-1',
144
+ group: null,
145
+ data: { type: 'resource', ...data },
146
+ }) as unknown as ResourceCardProps['post'];
147
+
148
+ const filePost = resourcePost({
149
+ title: 'Spec PDF',
150
+ resourceKind: 'file',
151
+ attachments: [
152
+ {
153
+ url: 'https://example.com/spec.pdf',
154
+ type: 'file',
155
+ filename: 'spec.pdf',
156
+ meta: { mimetype: 'application/pdf', size: 1234 },
157
+ },
158
+ ],
159
+ });
160
+
161
+ const videoPost = resourcePost({
162
+ title: 'Talk',
163
+ resourceKind: 'video',
164
+ attachments: [
165
+ {
166
+ url: 'https://example.com/talk.mp4',
167
+ type: 'video',
168
+ meta: { mimetype: 'video/mp4', size: 99 },
169
+ },
170
+ ],
171
+ });
172
+
173
+ const linkPost = resourcePost({
174
+ title: 'Docs',
175
+ resourceKind: 'link',
176
+ url: 'https://example.com/docs',
177
+ });
178
+
179
+ const renderCard = (post: ResourceCardProps['post']) => {
180
+ let tree: ReturnType<typeof ReactTestRenderer.create>;
181
+ ReactTestRenderer.act(() => {
182
+ tree = ReactTestRenderer.create(
183
+ React.createElement(ResourceCard, {
184
+ post,
185
+ layout: 'list' as const,
186
+ })
187
+ );
188
+ });
189
+ return tree;
190
+ };
191
+
192
+ // The bookmark button is also a Pressable; it renders disabled for the
193
+ // anonymous profile the mock returns, so enabled Pressables are the
194
+ // card's actions only.
195
+ const enabledPressables = (tree: ReturnType<typeof ReactTestRenderer.create>) =>
196
+ tree.root
197
+ .findAllByType('Pressable')
198
+ .filter((node: { props: { disabled?: boolean } }) => !node.props.disabled);
199
+
200
+ const actionTexts = (tree: ReturnType<typeof ReactTestRenderer.create>) =>
201
+ tree.root
202
+ .findAllByType('ThemedText')
203
+ .map((node: { props: { children?: unknown } }) => node.props.children);
204
+
205
+ describe('ResourceCard list-layout actions', () => {
206
+ beforeEach(() => {
207
+ mockTranslations = EN;
208
+ Linking.openURL.mockClear();
209
+ });
210
+
211
+ it('enables the download action for a file resource (en locale)', () => {
212
+ const tree = renderCard(filePost);
213
+ const [action] = enabledPressables(tree);
214
+ expect(action).toBeDefined();
215
+ expect(actionTexts(tree)).toContain('Download');
216
+ ReactTestRenderer.act(() => {
217
+ action.props.onPress();
218
+ });
219
+ expect(Linking.openURL).toHaveBeenCalledWith(
220
+ 'https://example.com/spec.pdf'
221
+ );
222
+ tree.unmount();
223
+ });
224
+
225
+ it('keeps the download action enabled in non-English locales', () => {
226
+ mockTranslations = DE;
227
+ const tree = renderCard(filePost);
228
+ const [action] = enabledPressables(tree);
229
+ expect(action).toBeDefined();
230
+ expect(actionTexts(tree)).toContain('Herunterladen');
231
+ ReactTestRenderer.act(() => {
232
+ action.props.onPress();
233
+ });
234
+ expect(Linking.openURL).toHaveBeenCalledWith(
235
+ 'https://example.com/spec.pdf'
236
+ );
237
+ tree.unmount();
238
+ });
239
+
240
+ it('renders an inert label for a file resource without an attachment', () => {
241
+ const tree = renderCard(
242
+ resourcePost({ title: 'Empty', resourceKind: 'file' })
243
+ );
244
+ expect(enabledPressables(tree)).toHaveLength(0);
245
+ expect(actionTexts(tree)).toContain('Download');
246
+ tree.unmount();
247
+ });
248
+
249
+ it('does not make the watch label pressable for a video resource', () => {
250
+ const tree = renderCard(videoPost);
251
+ expect(enabledPressables(tree)).toHaveLength(0);
252
+ expect(actionTexts(tree)).toContain('Watch');
253
+ tree.unmount();
254
+ });
255
+
256
+ it('renders no action for a link resource', () => {
257
+ const tree = renderCard(linkPost);
258
+ expect(enabledPressables(tree)).toHaveLength(0);
259
+ expect(actionTexts(tree)).not.toContain('Download');
260
+ tree.unmount();
261
+ });
262
+ });
@@ -231,7 +231,10 @@ export const ResourceCard = ({
231
231
  </View>
232
232
  <View className="flex-row items-center gap-2">
233
233
  {resource.resourceKind !== 'link' && actionLabel ? (
234
- actionLabel === 'Download' && downloadUrl ? (
234
+ // Gate on the stable resource kind, not the translated label:
235
+ // in non-English locales the label differs and the download
236
+ // action would silently stop working (web gates the same way).
237
+ resource.resourceKind === 'file' && downloadUrl ? (
235
238
  <Pressable onPress={() => Linking.openURL(downloadUrl)}>
236
239
  <ThemedText className="text-primary text-xs font-medium">
237
240
  {actionLabel}