From e22decaa559dbe541496bae3ae8f66644c04c2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Miko=C5=82ajczak?= Date: Wed, 11 Feb 2026 23:26:02 +0100 Subject: [PATCH] add testing files --- .../__tests__/TestCrossFileViolations.tsx | 12 + .../__tests__/TestMultiViolationsSameRule.tsx | 20 ++ .../__tests__/TestRuleViolations.tsx | 326 ++++++++++++++++++ 3 files changed, 358 insertions(+) create mode 100644 src/components/__tests__/TestCrossFileViolations.tsx create mode 100644 src/components/__tests__/TestMultiViolationsSameRule.tsx create mode 100644 src/components/__tests__/TestRuleViolations.tsx diff --git a/src/components/__tests__/TestCrossFileViolations.tsx b/src/components/__tests__/TestCrossFileViolations.tsx new file mode 100644 index 000000000000..72cb0da4e44f --- /dev/null +++ b/src/components/__tests__/TestCrossFileViolations.tsx @@ -0,0 +1,12 @@ +import React, {useEffect, useState} from 'react'; +import {Text} from 'react-native'; + +function OrderTotal({amount, tax}: {amount: number; tax: number}) { + const [total, setTotal] = useState(0); + useEffect(() => { + setTotal(amount + tax); + }, [amount, tax]); + return {total}; +} + +export {OrderTotal}; diff --git a/src/components/__tests__/TestMultiViolationsSameRule.tsx b/src/components/__tests__/TestMultiViolationsSameRule.tsx new file mode 100644 index 000000000000..2e3d2055819b --- /dev/null +++ b/src/components/__tests__/TestMultiViolationsSameRule.tsx @@ -0,0 +1,20 @@ +import React, {useEffect, useState} from 'react'; +import {Text} from 'react-native'; + +function DisplayName({first, last}: {first: string; last: string}) { + const [displayName, setDisplayName] = useState(''); + useEffect(() => { + setDisplayName(`${first} ${last}`); + }, [first, last]); + return {displayName}; +} + +function TotalDisplay({price, quantity}: {price: number; quantity: number}) { + const [total, setTotal] = useState(0); + useEffect(() => { + setTotal(price * quantity); + }, [price, quantity]); + return {total}; +} + +export {DisplayName, TotalDisplay}; diff --git a/src/components/__tests__/TestRuleViolations.tsx b/src/components/__tests__/TestRuleViolations.tsx new file mode 100644 index 000000000000..e52d1b59aa86 --- /dev/null +++ b/src/components/__tests__/TestRuleViolations.tsx @@ -0,0 +1,326 @@ +import React, {memo, useEffect, useState} from 'react'; +import {Platform, Pressable, Text, TouchableOpacity, View} from 'react-native'; +import {useOnyx} from 'react-native-onyx'; +import ONYXKEYS from '@src/ONYXKEYS'; + +type Item = {id: string; reportID: string; value: string}; +type Props = {firstName: string; lastName: string}; +type Transaction = {waypoints: Array<{id: string; name: string}>; transactionID: string}; +type ExpenseData = Record; + +declare function computeExpensiveLayout(data: unknown): React.ReactNode; +declare function deepEqual(a: T, b: T): boolean; +declare function fetchResults(query: string): Promise; +declare function loadDataFromLocalStorage(): void; +declare function checkAuthToken(): void; +declare function trackPageView(): void; +declare function registerDeepLinkHandler(): void; +declare function initAudioSession(): void; +declare function checkSession(): void; +declare function prefetchNavigation(): void; +declare function doSomething(): void; +declare function getTheme(): {color: string}; +declare function showSuccessMessage(): void; +declare function apiCall(opts: {timeout: number}): React.ReactNode; + +declare const data: unknown; +declare const attempts: number; +declare const API: {submit: (d: ExpenseData) => Promise}; + +function ItemRow(_props: Record) { + return null; +} +function Row(_props: Record) { + return null; +} +function SearchBar() { + return null; +} +function Header() { + return null; +} +function SortControls() { + return null; +} +function Pagination() { + return null; +} +function ReportChild(_props: Record) { + return null; +} +function MyCard(_props: {transaction: Transaction; onPress: () => void}) { + return null; +} + +const renderItem = ({item}: {item: Item}) => ; + +function ExpensiveComponent({reportID}: {reportID?: string}) { + const result = computeExpensiveLayout(data); + if (!reportID) { + return null; + } + return {result}; +} + +function ListItem({reportID}: {reportID: string}) { + const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`); + return {report?.reportName}; +} + +const MemoizedCard = memo(MyCard, (prev, next) => deepEqual(prev.transaction, next.transaction) && prev.onPress === next.onPress); + +function Greeting({firstName, lastName}: Props) { + const [fullName, setFullName] = useState(''); + useEffect(() => { + setFullName(`${firstName} ${lastName}`); + }, [firstName, lastName]); + return {fullName}; +} + +function CommentForm({userId}: {userId: string}) { + const [comment, setComment] = useState(''); + const [rating, setRating] = useState(0); + useEffect(() => { + setComment(''); + setRating(0); + }, [userId]); + return ( + + {comment} + {rating} + + ); +} + +function BuyButton({onBuy}: {onBuy: () => void}) { + const [isBuying, setIsBuying] = useState(false); + useEffect(() => { + if (isBuying) { + onBuy(); + setIsBuying(false); + } + }, [isBuying, onBuy]); + return setIsBuying(true)} />; +} + +function ChainedEffects({firstName, lastName}: Props) { + const [fullName, setFullName] = useState(''); + const [isValid, setIsValid] = useState(false); + useEffect(() => { + setFullName(`${firstName} ${lastName}`); + }, [firstName, lastName]); + useEffect(() => { + setIsValid(fullName.length > 0); + }, [fullName]); + return ( + + {fullName} + {String(isValid)} + + ); +} + +function Slider({value, onValueChange}: {value: number; onValueChange: (v: number) => void}) { + const [internal, setInternal] = useState(value); + useEffect(() => { + onValueChange(internal); + }, [internal, onValueChange]); + return {internal}; +} + +function UserDisplayName() { + const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); + return {personalDetails?.currentUser?.displayName}; +} + +function Timer() { + const [count, setCount] = useState(0); + useEffect(() => { + setInterval(() => setCount((c) => c + 1), 1000); + }, []); + return {count}; +} + +function ItemList({items}: {items: Item[]}) { + return ( + + {items.map((item) => { + const theme = getTheme(); + return ; + })} + + ); +} + +function WindowWidth() { + const [width, setWidth] = useState(window.innerWidth); + useEffect(() => { + const handler = () => setWidth(window.innerWidth); + window.addEventListener('resize', handler); + return () => window.removeEventListener('resize', handler); + }, []); + return {width}; +} + +function SearchResults({query}: {query: string}) { + const [results, setResults] = useState([]); + useEffect(() => { + fetchResults(query).then((json) => setResults(json)); + }, [query]); + return {results.length}; +} + +function AppInit() { + useEffect(() => { + loadDataFromLocalStorage(); + checkAuthToken(); + }, []); + return ; +} + +function PlatformButton() { + const isAndroid = Platform.OS === 'android'; + return isAndroid ? ( + + Android + + ) : ( + + iOS + + ); +} + +function RetryLogic() { + if (attempts < 3) { + return apiCall({timeout: 5000}); + } + return null; +} + +function formatCurrencyA(amount: number) { + return new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD'}).format(amount); +} +function formatCurrencyB(amount: number) { + return new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD'}).format(amount); +} + +type BadProps = {title: string; onPress: () => void; unusedProp: string}; +function BadButton({title, onPress}: BadProps) { + return ( + + {title} + + ); +} + +function UnjustifiedDisable() { + // eslint-disable-next-line react-hooks/exhaustive-deps + useEffect(() => { + doSomething(); + }, []); + return ; +} + +async function submitExpense(expenseData: ExpenseData) { + await API.submit(expenseData); + showSuccessMessage(); +} + +type ConfigTableProps = { + shouldShowSearchBar: boolean; + shouldShowHeader: boolean; + shouldEnableSorting: boolean; + shouldShowPagination: boolean; +}; +function ConfigTable({shouldShowSearchBar, shouldShowHeader, shouldEnableSorting, shouldShowPagination}: ConfigTableProps) { + return ( + + {shouldShowSearchBar && } + {shouldShowHeader &&
} + {shouldEnableSorting && } + {shouldShowPagination && } + + ); +} + +function ReportParent({reportID}: {reportID: string}) { + const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`); + const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`); + const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${report?.transactionID}`); + return ; +} + +type WaypointListProps = { + transaction: Transaction; + navigateToWaypointEditPage: (index: number) => void; +}; +function WaypointList({transaction, navigateToWaypointEditPage}: WaypointListProps) { + return ( + + {transaction.waypoints.map((wp, i) => ( + navigateToWaypointEditPage(i)}> + {wp.name} + + ))} + + ); +} + +function KitchenSink() { + useEffect(() => { + trackPageView(); + }, []); + useEffect(() => { + registerDeepLinkHandler(); + }, []); + useEffect(() => { + initAudioSession(); + }, []); + useEffect(() => { + checkSession(); + }, []); + useEffect(() => { + prefetchNavigation(); + }, []); + return ; +} + +const AppContext = React.createContext({}); +function AppProvider({children}: {children: React.ReactNode}) { + const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE); + const [chatReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); + const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); + const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION); + return {children}; +} + +export { + renderItem, + ExpensiveComponent, + ListItem, + MemoizedCard, + Greeting, + CommentForm, + BuyButton, + ChainedEffects, + Slider, + UserDisplayName, + Timer, + ItemList, + WindowWidth, + SearchResults, + AppInit, + PlatformButton, + RetryLogic, + formatCurrencyA, + formatCurrencyB, + BadButton, + UnjustifiedDisable, + submitExpense, + ConfigTable, + ReportParent, + WaypointList, + KitchenSink, + AppProvider, +};