From 4b09132c7464ab6edf9e35b757df3736ceaf7a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= <1844749591@qq.com> Date: Thu, 2 Jan 2025 17:28:20 +0800 Subject: [PATCH] chore: clean code --- src/Popup.tsx | 35 +---------------- tests/popup.test.tsx | 92 -------------------------------------------- 2 files changed, 2 insertions(+), 125 deletions(-) diff --git a/src/Popup.tsx b/src/Popup.tsx index 7447d85..069e6b9 100644 --- a/src/Popup.tsx +++ b/src/Popup.tsx @@ -11,53 +11,22 @@ export interface ContentProps { bodyClassName?: string; } -const getTextContent = (node: (() => React.ReactNode) | React.ReactNode): string => { - if (!node) { - return ''; - } - - const resolvedNode = typeof node === 'function' ? node() : node; - - if (typeof resolvedNode === 'string' || typeof resolvedNode === 'number') { - return String(resolvedNode); - } - - if (Array.isArray(resolvedNode)) { - return resolvedNode.map(getTextContent).join(' '); - } - - if (React.isValidElement(resolvedNode)) { - return getTextContent(resolvedNode.props.children); - } - - return ''; -}; - export default function Popup(props: ContentProps) { const { children, prefixCls, id, overlayInnerStyle: innerStyle, bodyClassName, className, style } = props; - const tooltipText = getTextContent(children); - return ( <>
- {tooltipText && ( - - )} ); } diff --git a/tests/popup.test.tsx b/tests/popup.test.tsx index 7165d21..52a97f3 100644 --- a/tests/popup.test.tsx +++ b/tests/popup.test.tsx @@ -1,5 +1,3 @@ -import React from 'react'; -import { render } from '@testing-library/react'; import Popup from '../src/Popup'; describe('Popup', () => { @@ -7,94 +5,4 @@ describe('Popup', () => { it('should export', () => { expect(Popup).toBeTruthy(); }); - - it('should correctly extract text from string, number, function, and element', () => { - const { getByRole } = render( - - {() => ( - <> - {'Hello'} - {123} - World - - )} - , - ); - - const tooltip = getByRole('tooltip'); - const hiddenTextContainer = tooltip.querySelector('div > div'); - - expect(hiddenTextContainer.textContent).toBe('Hello 123 World'); - }); - - it('should apply updated hidden text styles correctly', () => { - const { getByRole } = render( - - test hidden text - , - ); - - const tooltip = getByRole('tooltip'); - const hiddenTextContainer = tooltip.querySelector('div > div'); - - expect(hiddenTextContainer).toHaveStyle({ - width: '0', - height: '0', - position: 'absolute', - overflow: 'hidden', - opacity: '0', - }); - }); - - it('should return empty string if children is null or undefined', () => { - const { getByRole } = render( - - {null} - , - ); - const tooltip = getByRole('tooltip'); - - expect(tooltip.querySelector('div > div')).toBeNull(); - }); - - it('should handle nested arrays correctly', () => { - const { getByRole } = render( - - {[ - 'First', - ['Second', 'Third'], - Fourth, - ]} - , - ); - const tooltip = getByRole('tooltip'); - const hiddenTextContainer = tooltip.querySelector('div > div'); - - // "First Second Third Fourth" - expect(hiddenTextContainer.textContent).toBe('First Second Third Fourth'); - }); - - it('should handle function returning an array', () => { - const { getByRole } = render( - - {() => ['Alpha', Beta]} - , - ); - const tooltip = getByRole('tooltip'); - const hiddenTextContainer = tooltip.querySelector('div > div'); - - // "Alpha Beta" - expect(hiddenTextContainer.textContent).toBe('Alpha Beta'); - }); - - it('should handle function returning undefined', () => { - const { getByRole } = render( - - {() => undefined} - , - ); - const tooltip = getByRole('tooltip'); - - expect(tooltip.querySelector('div > div')).toBeNull(); - }); });