From f64fdb561e5d551f8bb610a35c34124a9ec1d358 Mon Sep 17 00:00:00 2001 From: defiQUG Date: Sun, 5 Oct 2025 09:43:55 -0700 Subject: [PATCH] refactor: Simplify LazyImage component by removing unnecessary state and optimizing IntersectionObserver usage; enhance test setup with improved window.location mock --- src/components/ui/LazyImage.tsx | 3 --- src/test/setup.ts | 25 ++++++++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/components/ui/LazyImage.tsx b/src/components/ui/LazyImage.tsx index 380ca72..350cfd7 100644 --- a/src/components/ui/LazyImage.tsx +++ b/src/components/ui/LazyImage.tsx @@ -23,7 +23,6 @@ export function LazyImage({ ...props }: LazyImageProps) { const [isLoaded, setIsLoaded] = useState(false) - const [isInView, setIsInView] = useState(priority) const [error, setError] = useState(false) const imgRef = useRef(null) const [imageSrc, setImageSrc] = useState(priority ? src : placeholder) @@ -35,14 +34,12 @@ export function LazyImage({ const observer = new IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { - setIsInView(true) setImageSrc(src) observer.disconnect() } }, { rootMargin: '50px' } ) - const currentImg = imgRef.current if (currentImg) { observer.observe(currentImg) diff --git a/src/test/setup.ts b/src/test/setup.ts index 648b129..9344518 100644 --- a/src/test/setup.ts +++ b/src/test/setup.ts @@ -58,10 +58,21 @@ Object.defineProperty(window, 'localStorage', { // Mock window.location delete (window as any).location -window.location = { - ...window.location, - hash: '#/', - pathname: '/', - search: '', - href: 'http://localhost:3000/', -} \ No newline at end of file + +Object.defineProperty(window, 'location', { + writable: true, + value: { + ...window.location, + hash: '#/', + pathname: '/', + get search() { + return ''; + }, + get href() { + return 'http://localhost:3000/'; + }, + assign: vi.fn(), + replace: vi.fn(), + reload: vi.fn(), + }, +}); \ No newline at end of file