-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTestsPage.tsx
More file actions
386 lines (355 loc) · 10.3 KB
/
Copy pathTestsPage.tsx
File metadata and controls
386 lines (355 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import {
View,
Text,
StyleSheet,
ScrollView,
TouchableOpacity,
ActivityIndicator,
LayoutAnimation,
} from 'react-native';
import { getTestCollector } from 'react-native-harness';
// @ts-expect-error - internal module not exported
import { TestComponentOverlay } from '@react-native-harness/runtime/dist/render/TestComponentOverlay';
// @ts-expect-error - internal module not exported
import { useRenderedElement } from '@react-native-harness/runtime/dist/ui/state';
// @ts-expect-error - internal module not exported
import { cleanup as cleanupRenderedElement } from '@react-native-harness/runtime/dist/render/cleanup';
import type { TestSuite, TestCase } from '@react-native-harness/bridge';
import { useState, useEffect, useRef } from 'react';
import type { Metadata } from '../shared/metadata';
const testContext = require.context(
'../../__tests__',
false,
/\.harness\.tsx?$/
);
// Cache collected suites globally (persists across HMR, require.context only executes once)
const CACHE_KEY = '__RIVE_TEST_SUITES__';
type GlobalCache = { [CACHE_KEY]?: TestSuite[] };
function getCachedSuites(): TestSuite[] | null {
return (global as unknown as GlobalCache)[CACHE_KEY] ?? null;
}
function setCachedSuites(suites: TestSuite[]): void {
(global as unknown as GlobalCache)[CACHE_KEY] = suites;
}
type TestStatus = 'pending' | 'running' | 'passed' | 'failed';
interface TestState {
status: TestStatus;
error?: string;
}
function buildTestStates(suites: TestSuite[]): Map<string, TestState> {
const states = new Map<string, TestState>();
for (const suite of suites) {
for (const test of suite.tests) {
states.set(`${suite.name}::${test.name}`, { status: 'pending' });
}
}
return states;
}
const OVERLAY_BAR_HEIGHT = 32;
const OVERLAY_EXPANDED_HEIGHT = 250;
function CollapsibleOverlay() {
const { element } = useRenderedElement();
const [expanded, setExpanded] = useState(false);
const hasContent = element !== null;
const prevHasContent = useRef(hasContent);
useEffect(() => {
if (prevHasContent.current !== hasContent) {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
prevHasContent.current = hasContent;
}
}, [hasContent]);
return (
<View>
{hasContent && (
<TouchableOpacity
onPress={() => {
LayoutAnimation.configureNext(
LayoutAnimation.Presets.easeInEaseOut
);
setExpanded((prev) => !prev);
}}
style={{
height: OVERLAY_BAR_HEIGHT,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2a2a3e',
borderTopWidth: 1,
borderTopColor: '#444',
}}
>
<Text style={{ color: '#aaa', fontSize: 11, fontWeight: '600' }}>
{expanded ? 'Hide test view' : 'Show test view'}
</Text>
</TouchableOpacity>
)}
{/* TestComponentOverlay uses absoluteFillObject, so we contain it
with position:relative + explicit height + overflow:hidden */}
<View
style={{
height: hasContent && expanded ? OVERLAY_EXPANDED_HEIGHT : 0,
position: 'relative',
overflow: 'hidden',
}}
>
<TestComponentOverlay />
</View>
</View>
);
}
export default function TestsPage() {
const cached = getCachedSuites();
const [suites, setSuites] = useState<TestSuite[]>(cached ?? []);
const [loading, setLoading] = useState(cached === null);
const [testStates, setTestStates] = useState<Map<string, TestState>>(() =>
cached ? buildTestStates(cached) : new Map()
);
const [runningAll, setRunningAll] = useState(false);
useEffect(() => {
if (getCachedSuites() !== null) return;
async function collectTests() {
const collector = getTestCollector();
const result = await collector.collect(() => {
testContext.keys().forEach((key) => testContext(key));
}, 'harness-tests');
const collectedSuites = result.testSuite.suites;
setCachedSuites(collectedSuites);
setSuites(collectedSuites);
setTestStates(buildTestStates(collectedSuites));
setLoading(false);
}
collectTests();
}, []);
function getTestKey(suiteName: string, testName: string): string {
return `${suiteName}::${testName}`;
}
async function runTest(suiteName: string, test: TestCase) {
const key = getTestKey(suiteName, test.name);
setTestStates((prev) => new Map(prev).set(key, { status: 'running' }));
try {
await (test.fn as () => void | Promise<void>)();
setTestStates((prev) => new Map(prev).set(key, { status: 'passed' }));
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e);
cleanupRenderedElement();
setTestStates((prev) =>
new Map(prev).set(key, {
status: 'failed',
error: errorMessage,
})
);
}
}
async function runSuite(suite: TestSuite) {
setTestStates((prev) => {
const next = new Map(prev);
for (const test of suite.tests) {
next.set(getTestKey(suite.name, test.name), { status: 'pending' });
}
return next;
});
for (const test of suite.tests) {
await runTest(suite.name, test);
}
}
async function runAllTests() {
setRunningAll(true);
for (const suite of suites) {
await runSuite(suite);
}
setRunningAll(false);
}
function getStatusIcon(status: TestStatus): string {
switch (status) {
case 'pending':
return '○';
case 'running':
return '◌';
case 'passed':
return '✓';
case 'failed':
return '✗';
}
}
function getStatusColor(status: TestStatus): string {
switch (status) {
case 'pending':
return '#888';
case 'running':
return '#007AFF';
case 'passed':
return '#34C759';
case 'failed':
return '#FF3B30';
}
}
if (loading) {
return (
<View style={styles.centered}>
<ActivityIndicator size="large" color="#007AFF" />
<Text style={styles.loadingText}>Collecting tests...</Text>
</View>
);
}
const passedCount = Array.from(testStates.values()).filter(
(s) => s.status === 'passed'
).length;
const failedCount = Array.from(testStates.values()).filter(
(s) => s.status === 'failed'
).length;
const totalCount = testStates.size;
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.title}>Test Runner</Text>
<Text style={styles.summary}>
{passedCount}/{totalCount} passed
{failedCount > 0 && ` • ${failedCount} failed`}
</Text>
<TouchableOpacity
style={[styles.runAllButton, runningAll && styles.buttonDisabled]}
onPress={runAllTests}
disabled={runningAll}
>
<Text style={styles.runAllButtonText}>
{runningAll ? 'Running...' : 'Run All Tests'}
</Text>
</TouchableOpacity>
</View>
<ScrollView style={styles.scrollView}>
{suites.map((suite) => (
<View key={suite.name} style={styles.suite}>
<TouchableOpacity onPress={() => runSuite(suite)}>
<Text style={styles.suiteName}>{suite.name}</Text>
</TouchableOpacity>
{suite.tests.map((test) => {
const key = getTestKey(suite.name, test.name);
const state = testStates.get(key) || { status: 'pending' };
return (
<TouchableOpacity
key={test.name}
style={styles.testRow}
onPress={() => runTest(suite.name, test)}
disabled={state.status === 'running'}
>
{state.status === 'running' ? (
<ActivityIndicator
size="small"
color="#007AFF"
style={styles.statusIcon}
/>
) : (
<Text
style={[
styles.statusIcon,
{ color: getStatusColor(state.status) },
]}
>
{getStatusIcon(state.status)}
</Text>
)}
<View style={styles.testInfo}>
<Text style={styles.testName}>{test.name}</Text>
{state.error && (
<Text style={styles.testError}>{state.error}</Text>
)}
</View>
</TouchableOpacity>
);
})}
</View>
))}
</ScrollView>
<CollapsibleOverlay />
</View>
);
}
TestsPage.metadata = {
name: 'Tests',
description: 'In-app test runner for Rive React Native',
} satisfies Metadata;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
centered: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
loadingText: {
marginTop: 10,
fontSize: 16,
color: '#666',
},
header: {
padding: 16,
borderBottomWidth: 1,
borderBottomColor: '#e0e0e0',
backgroundColor: '#f8f8f8',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 4,
},
summary: {
fontSize: 14,
color: '#666',
marginBottom: 12,
},
runAllButton: {
backgroundColor: '#007AFF',
paddingVertical: 10,
paddingHorizontal: 16,
borderRadius: 8,
alignItems: 'center',
},
buttonDisabled: {
opacity: 0.6,
},
runAllButtonText: {
color: '#fff',
fontWeight: '600',
fontSize: 16,
},
scrollView: {
flex: 1,
},
suite: {
padding: 16,
borderBottomWidth: 1,
borderBottomColor: '#e0e0e0',
},
suiteName: {
fontSize: 18,
fontWeight: '600',
marginBottom: 12,
color: '#333',
},
testRow: {
flexDirection: 'row',
alignItems: 'flex-start',
paddingVertical: 8,
paddingLeft: 8,
},
statusIcon: {
fontSize: 18,
fontWeight: 'bold',
width: 24,
marginRight: 8,
},
testInfo: {
flex: 1,
},
testName: {
fontSize: 14,
color: '#333',
},
testError: {
fontSize: 12,
color: '#FF3B30',
marginTop: 4,
fontFamily: 'monospace',
},
});