2025-12-25 16:02:43 +00:00
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
|
|
|
import { renderHook } from '@testing-library/react';
|
|
|
|
|
import { useRef } from 'react';
|
|
|
|
|
import { useIntersectionObserver } from './useIntersectionObserver';
|
|
|
|
|
|
|
|
|
|
// Mock IntersectionObserver
|
|
|
|
|
class MockIntersectionObserver {
|
|
|
|
|
observe = vi.fn();
|
|
|
|
|
disconnect = vi.fn();
|
|
|
|
|
unobserve = vi.fn();
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
public callback: IntersectionObserverCallback,
|
|
|
|
|
public options?: IntersectionObserverInit,
|
2026-01-13 18:47:57 +00:00
|
|
|
) {}
|
2025-12-25 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('useIntersectionObserver', () => {
|
|
|
|
|
let mockObserver: MockIntersectionObserver;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2026-01-13 18:47:57 +00:00
|
|
|
mockObserver = new MockIntersectionObserver(() => {});
|
2025-12-25 16:02:43 +00:00
|
|
|
global.IntersectionObserver = MockIntersectionObserver as any;
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return undefined when element ref is null', () => {
|
|
|
|
|
const elementRef = { current: null };
|
|
|
|
|
const { result } = renderHook(() =>
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
useIntersectionObserver(elementRef as React.RefObject<Element>, {}),
|
2025-12-25 16:02:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Initially undefined, and stays undefined when ref is null
|
|
|
|
|
expect(result.current).toBeUndefined();
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
expect(mockObserver.observe).not.toHaveBeenCalled();
|
2025-12-25 16:02:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should observe element when ref is provided', () => {
|
|
|
|
|
const element = document.createElement('div');
|
|
|
|
|
const elementRef = { current: element };
|
|
|
|
|
|
2026-01-13 18:47:57 +00:00
|
|
|
renderHook(() =>
|
|
|
|
|
useIntersectionObserver(elementRef as React.RefObject<Element>, {}),
|
|
|
|
|
);
|
2025-12-25 16:02:43 +00:00
|
|
|
|
|
|
|
|
// Wait for effect to run
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
expect(mockObserver.observe).toHaveBeenCalled();
|
2025-12-25 16:02:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should disconnect observer on unmount', () => {
|
|
|
|
|
const element = document.createElement('div');
|
|
|
|
|
const elementRef = { current: element };
|
|
|
|
|
|
|
|
|
|
const { unmount } = renderHook(() =>
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
useIntersectionObserver(elementRef as React.RefObject<Element>, {}),
|
2025-12-25 16:02:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
unmount();
|
|
|
|
|
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
expect(mockObserver.disconnect).toHaveBeenCalled();
|
2025-12-25 16:02:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should use default options', () => {
|
|
|
|
|
const element = document.createElement('div');
|
|
|
|
|
const elementRef = { current: element };
|
|
|
|
|
|
2026-01-13 18:47:57 +00:00
|
|
|
renderHook(() =>
|
|
|
|
|
useIntersectionObserver(elementRef as React.RefObject<Element>, {}),
|
|
|
|
|
);
|
2025-12-25 16:02:43 +00:00
|
|
|
|
|
|
|
|
// Verify IntersectionObserver was called
|
|
|
|
|
expect(MockIntersectionObserver).toHaveBeenCalled();
|
|
|
|
|
const call = (MockIntersectionObserver as any).mock.calls[0];
|
|
|
|
|
expect(call[1]).toMatchObject({
|
|
|
|
|
threshold: 0,
|
|
|
|
|
root: null,
|
|
|
|
|
rootMargin: '0%',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should use custom options', () => {
|
|
|
|
|
const element = document.createElement('div');
|
|
|
|
|
const elementRef = { current: element };
|
|
|
|
|
|
|
|
|
|
renderHook(() =>
|
|
|
|
|
useIntersectionObserver(elementRef as React.RefObject<Element>, {
|
|
|
|
|
threshold: 0.5,
|
|
|
|
|
rootMargin: '10px',
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(MockIntersectionObserver).toHaveBeenCalled();
|
|
|
|
|
const call = (MockIntersectionObserver as any).mock.calls[0];
|
|
|
|
|
expect(call[1]).toMatchObject({
|
|
|
|
|
threshold: 0.5,
|
|
|
|
|
rootMargin: '10px',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should freeze when freezeOnceVisible is true and element is intersecting', () => {
|
|
|
|
|
const element = document.createElement('div');
|
|
|
|
|
const elementRef = { current: element };
|
|
|
|
|
|
|
|
|
|
const { result, rerender } = renderHook(
|
|
|
|
|
({ freeze }) =>
|
|
|
|
|
useIntersectionObserver(elementRef as React.RefObject<Element>, {
|
|
|
|
|
freezeOnceVisible: freeze,
|
|
|
|
|
}),
|
|
|
|
|
{
|
|
|
|
|
initialProps: { freeze: true },
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Get the callback from the constructor call
|
|
|
|
|
const constructorCall = (MockIntersectionObserver as any).mock.calls.find(
|
|
|
|
|
(call: any[]) => call[0] && typeof call[0] === 'function',
|
|
|
|
|
);
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
|
2025-12-25 16:02:43 +00:00
|
|
|
if (constructorCall) {
|
|
|
|
|
const callback = constructorCall[0];
|
|
|
|
|
const entry = {
|
|
|
|
|
isIntersecting: true,
|
|
|
|
|
} as IntersectionObserverEntry;
|
|
|
|
|
|
|
|
|
|
callback([entry], {} as IntersectionObserver);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rerender({ freeze: true });
|
|
|
|
|
|
|
|
|
|
// Should not observe again when frozen
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
expect(mockObserver.observe).toHaveBeenCalledTimes(1);
|
2025-12-25 16:02:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle missing IntersectionObserver gracefully', () => {
|
|
|
|
|
const originalIO = global.IntersectionObserver;
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
delete global.IntersectionObserver;
|
|
|
|
|
|
|
|
|
|
const element = document.createElement('div');
|
|
|
|
|
const elementRef = { current: element };
|
|
|
|
|
|
|
|
|
|
const { result } = renderHook(() =>
|
feat: Visual masterpiece - true light mode & premium UI
🎨 **True Light/Dark Mode**
- Implemented proper light mode with inverted color scheme
- Smooth theme transitions (0.3s ease)
- Light mode colors: white backgrounds, dark text, vibrant accents
- System theme detection with proper class application
🌈 **Enhanced Theme System**
- 4 color themes work in both light and dark modes
- Cyber (cyan/magenta), Ocean (blue/teal), Forest (green/lime), Sunset (orange/purple)
- Theme-specific glassmorphism effects
- Proper contrast in light mode
✨ **Premium Animations**
- Float, glow-pulse, slide-in, scale-in, rotate-in animations
- Smooth page transitions
- Hover effects with depth (lift, glow, scale)
- Micro-interactions on all interactive elements
🎯 **Visual Polish**
- Enhanced glassmorphism for light/dark modes
- Custom scrollbar with theme colors
- Beautiful text selection
- Focus indicators for accessibility
- Premium utility classes
🔧 **Technical Improvements**
- Updated UIStore to properly apply light/dark classes
- Added data-theme attribute for CSS targeting
- Smooth scroll behavior
- Optimized transitions
The app is now a visual masterpiece with perfect light/dark mode support!
2026-01-11 01:32:21 +00:00
|
|
|
useIntersectionObserver(elementRef as React.RefObject<Element>, {}),
|
2025-12-25 16:02:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.current).toBeUndefined();
|
|
|
|
|
|
|
|
|
|
global.IntersectionObserver = originalIO;
|
|
|
|
|
});
|
|
|
|
|
});
|