|
1 | | -import React from 'react'; |
2 | | -import { Text, View, ScrollView, StyleSheet } from 'react-native'; |
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { |
| 3 | + Text, |
| 4 | + View, |
| 5 | + FlatList, |
| 6 | + StyleSheet, |
| 7 | + TouchableOpacity, |
| 8 | +} from 'react-native'; |
3 | 9 | import { SafeAreaView } from 'react-native-safe-area-context'; |
4 | 10 | import { Button } from '../../components/Button'; |
5 | 11 | import { TestItem } from '../../components/TestItem'; |
6 | 12 | import { useTestsList } from '../../hooks/useTestsList'; |
7 | 13 | import { useTestsRun } from '../../hooks/useTestsRun'; |
8 | 14 | import { colors } from '../../styles/colors'; |
9 | 15 |
|
| 16 | +type SuiteEntry = { |
| 17 | + name: string; |
| 18 | + suite: { value: boolean; tests: Record<string, () => void | Promise<void>> }; |
| 19 | + count: number; |
| 20 | +}; |
| 21 | + |
10 | 22 | export const TestSuitesScreen = () => { |
11 | 23 | const [suites, toggle, clearAll, checkAll] = useTestsList(); |
12 | 24 | const [results, runTests, stats] = useTestsRun(); |
13 | | - let totalCount = 0; |
| 25 | + |
| 26 | + const suiteEntries = useMemo(() => { |
| 27 | + return Object.entries(suites).map(([name, suite]) => ({ |
| 28 | + name, |
| 29 | + suite, |
| 30 | + count: Object.keys(suite.tests).length, |
| 31 | + })); |
| 32 | + }, [suites]); |
| 33 | + |
| 34 | + const totalCount = useMemo( |
| 35 | + () => suiteEntries.reduce((sum, entry) => sum + entry.count, 0), |
| 36 | + [suiteEntries], |
| 37 | + ); |
| 38 | + |
| 39 | + const renderItem = ({ item, index }: { item: SuiteEntry; index: number }) => ( |
| 40 | + <TestItem |
| 41 | + suiteIndex={index} |
| 42 | + description={item.name} |
| 43 | + value={item.suite.value} |
| 44 | + count={item.count} |
| 45 | + results={results[item.name]?.results || []} |
| 46 | + onToggle={toggle} |
| 47 | + /> |
| 48 | + ); |
14 | 49 |
|
15 | 50 | return ( |
16 | 51 | <SafeAreaView style={styles.mainContainer} edges={['left', 'right']}> |
17 | 52 | <View style={styles.testList}> |
18 | | - <ScrollView style={styles.scrollView} testID="test-suites-list"> |
19 | | - {Object.entries(suites).map(([suiteName, suite], index) => { |
20 | | - const suiteTestCount = Object.keys(suite.tests).length; |
21 | | - totalCount += suiteTestCount; |
22 | | - return ( |
23 | | - <TestItem |
24 | | - key={index.toString()} |
25 | | - suiteIndex={index} |
26 | | - description={suiteName} |
27 | | - value={suite.value} |
28 | | - count={suiteTestCount} |
29 | | - results={results[suiteName]?.results || []} |
30 | | - onToggle={toggle} |
31 | | - /> |
32 | | - ); |
33 | | - })} |
34 | | - </ScrollView> |
| 53 | + <FlatList |
| 54 | + data={suiteEntries} |
| 55 | + renderItem={renderItem} |
| 56 | + keyExtractor={(_item, index) => index.toString()} |
| 57 | + testID="test-suites-list" |
| 58 | + /> |
35 | 59 | </View> |
36 | 60 | {results && Object.keys(results).length > 0 && stats && ( |
37 | | - <View style={styles.statsContainer}> |
38 | | - <Text style={styles.timeLabel}>⏱️ {stats.duration}ms</Text> |
39 | | - <Text |
40 | | - style={[styles.pass, styles.statNumber]} |
41 | | - testID="completion-stats" |
42 | | - > |
43 | | - {Object.values(results).reduce( |
44 | | - (sum, suite) => |
45 | | - sum + suite.results.filter(r => r.type === 'correct').length, |
46 | | - 0, |
47 | | - )} |
48 | | - </Text> |
49 | | - <Text |
50 | | - style={[styles.fail, styles.statNumber]} |
51 | | - testID="total-fail-count" |
52 | | - > |
53 | | - {Object.values(results).reduce( |
54 | | - (sum, suite) => |
55 | | - sum + suite.results.filter(r => r.type === 'incorrect').length, |
56 | | - 0, |
57 | | - )} |
58 | | - </Text> |
59 | | - <Text style={styles.statNumber}>{totalCount}</Text> |
| 61 | + <View style={styles.footerItem}> |
| 62 | + <View style={styles.footerCheckbox} /> |
| 63 | + <TouchableOpacity style={styles.footerContent} activeOpacity={1}> |
| 64 | + <Text style={styles.footerLabel}>⏱️ {stats.duration}ms</Text> |
| 65 | + <Text |
| 66 | + style={[styles.pass, styles.footerCount]} |
| 67 | + testID="completion-stats" |
| 68 | + > |
| 69 | + {Object.values(results).reduce( |
| 70 | + (sum, suite) => |
| 71 | + sum + suite.results.filter(r => r.type === 'correct').length, |
| 72 | + 0, |
| 73 | + )} |
| 74 | + </Text> |
| 75 | + <Text |
| 76 | + style={[styles.fail, styles.footerCount]} |
| 77 | + testID="total-fail-count" |
| 78 | + > |
| 79 | + {Object.values(results).reduce( |
| 80 | + (sum, suite) => |
| 81 | + sum + |
| 82 | + suite.results.filter(r => r.type === 'incorrect').length, |
| 83 | + 0, |
| 84 | + )} |
| 85 | + </Text> |
| 86 | + <Text style={styles.footerCount} testID="total-test-count"> |
| 87 | + {totalCount} |
| 88 | + </Text> |
| 89 | + </TouchableOpacity> |
60 | 90 | </View> |
61 | 91 | )} |
62 | 92 | <View style={styles.menu}> |
@@ -97,24 +127,31 @@ const styles = StyleSheet.create({ |
97 | 127 | alignContent: 'space-around', |
98 | 128 | justifyContent: 'space-around', |
99 | 129 | }, |
100 | | - scrollView: {}, |
101 | | - statsContainer: { |
102 | | - paddingHorizontal: 10, |
103 | | - paddingVertical: 5, |
| 130 | + footerItem: { |
| 131 | + width: '100%', |
104 | 132 | flexDirection: 'row', |
105 | | - alignItems: 'center', |
106 | 133 | alignContent: 'center', |
107 | | - justifyContent: 'space-evenly', |
| 134 | + alignItems: 'center', |
108 | 135 | gap: 10, |
| 136 | + borderTopWidth: 1, |
| 137 | + borderTopColor: colors.gray, |
| 138 | + paddingHorizontal: 10, |
| 139 | + paddingVertical: 5, |
| 140 | + }, |
| 141 | + footerCheckbox: { |
| 142 | + width: 24, |
109 | 143 | }, |
110 | | - timeLabel: { |
111 | | - fontSize: 12, |
| 144 | + footerContent: { |
| 145 | + flex: 1, |
| 146 | + flexDirection: 'row', |
| 147 | + }, |
| 148 | + footerLabel: { |
| 149 | + fontSize: 11, |
112 | 150 | fontWeight: 'bold', |
113 | 151 | flex: 8, |
114 | 152 | }, |
115 | | - statNumber: { |
116 | | - fontSize: 12, |
117 | | - fontWeight: 'bold', |
| 153 | + footerCount: { |
| 154 | + fontSize: 11, |
118 | 155 | flex: 1, |
119 | 156 | textAlign: 'right', |
120 | 157 | }, |
|
0 commit comments