|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { useRespositionOnWindowResize } from './useRepositionOnResize'; |
| 4 | + |
| 5 | +describe('useRespositionOnWindowResize', () => { |
| 6 | + const mockRef = { current: document.createElement('div') }; |
| 7 | + const mockResetPosition = vi.fn(); |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + vi.spyOn(window, 'innerWidth', 'get').mockReturnValue(1024); |
| 11 | + vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(768); |
| 12 | + mockRef.current = document.createElement('div'); |
| 13 | + |
| 14 | + vi.clearAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should not reposition when element is within viewport', () => { |
| 18 | + const initialPosition = { x: 100, y: 100 }; |
| 19 | + mockRef.current.getBoundingClientRect = vi.fn().mockReturnValue({ |
| 20 | + width: 100, |
| 21 | + height: 100, |
| 22 | + top: 100, |
| 23 | + left: 100, |
| 24 | + right: 200, |
| 25 | + bottom: 200, |
| 26 | + }); |
| 27 | + |
| 28 | + renderHook(() => |
| 29 | + useRespositionOnWindowResize(mockRef, initialPosition, mockResetPosition), |
| 30 | + ); |
| 31 | + |
| 32 | + window.dispatchEvent(new Event('resize')); |
| 33 | + |
| 34 | + // Get the callback function that was passed to resetPosition |
| 35 | + const callback = mockResetPosition.mock.calls[0][0]; |
| 36 | + // Call the callback with current position and verify it returns same position |
| 37 | + expect(callback(initialPosition)).toEqual(initialPosition); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should not reposition when ref.current is falsey', () => { |
| 41 | + const initialPosition = { x: 100, y: 100 }; |
| 42 | + // @ts-expect-error - we are testing the case where ref.current is falsey |
| 43 | + mockRef.current = null; |
| 44 | + |
| 45 | + renderHook(() => |
| 46 | + useRespositionOnWindowResize(mockRef, initialPosition, mockResetPosition), |
| 47 | + ); |
| 48 | + |
| 49 | + window.dispatchEvent(new Event('resize')); |
| 50 | + expect(mockResetPosition).not.toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should reposition when element is outside right viewport boundary', () => { |
| 54 | + mockRef.current.getBoundingClientRect = vi.fn().mockReturnValue({ |
| 55 | + width: 100, |
| 56 | + height: 100, |
| 57 | + top: 100, |
| 58 | + left: 1000, |
| 59 | + right: 1100, |
| 60 | + bottom: 200, |
| 61 | + }); |
| 62 | + |
| 63 | + renderHook(() => |
| 64 | + useRespositionOnWindowResize( |
| 65 | + mockRef, |
| 66 | + { x: 1000, y: 100 }, |
| 67 | + mockResetPosition, |
| 68 | + ), |
| 69 | + ); |
| 70 | + |
| 71 | + window.dispatchEvent(new Event('resize')); |
| 72 | + const callback = mockResetPosition.mock.calls[0][0]; |
| 73 | + const newPosition = callback({ x: 1000, y: 100 }); |
| 74 | + expect(newPosition.x).toBe(914); // 1024 - 100 - 10 |
| 75 | + expect(newPosition.y).toBe(100); // y shouldn't change |
| 76 | + }); |
| 77 | + |
| 78 | + it('should reposition when element is outside bottom viewport boundary', () => { |
| 79 | + mockRef.current.getBoundingClientRect = vi.fn().mockReturnValue({ |
| 80 | + width: 100, |
| 81 | + height: 100, |
| 82 | + top: 700, |
| 83 | + left: 100, |
| 84 | + right: 200, |
| 85 | + bottom: 800, |
| 86 | + }); |
| 87 | + |
| 88 | + renderHook(() => |
| 89 | + useRespositionOnWindowResize( |
| 90 | + mockRef, |
| 91 | + { x: 100, y: 700 }, |
| 92 | + mockResetPosition, |
| 93 | + ), |
| 94 | + ); |
| 95 | + |
| 96 | + window.dispatchEvent(new Event('resize')); |
| 97 | + const callback = mockResetPosition.mock.calls[0][0]; |
| 98 | + const newPosition = callback({ x: 100, y: 700 }); |
| 99 | + expect(newPosition.x).toBe(100); // x shouldn't change |
| 100 | + expect(newPosition.y).toBe(658); // 768 - 100 - 10 |
| 101 | + }); |
| 102 | + |
| 103 | + it('should reposition when element is outside left viewport boundary', () => { |
| 104 | + mockRef.current.getBoundingClientRect = vi.fn().mockReturnValue({ |
| 105 | + width: 100, |
| 106 | + height: 100, |
| 107 | + top: 100, |
| 108 | + left: -100, |
| 109 | + right: 0, |
| 110 | + bottom: 200, |
| 111 | + }); |
| 112 | + |
| 113 | + renderHook(() => |
| 114 | + useRespositionOnWindowResize( |
| 115 | + mockRef, |
| 116 | + { x: -100, y: 100 }, |
| 117 | + mockResetPosition, |
| 118 | + ), |
| 119 | + ); |
| 120 | + |
| 121 | + window.dispatchEvent(new Event('resize')); |
| 122 | + const callback = mockResetPosition.mock.calls[0][0]; |
| 123 | + const newPosition = callback({ x: -100, y: 100 }); |
| 124 | + expect(newPosition.x).toBe(10); // reset to 10 |
| 125 | + expect(newPosition.y).toBe(100); // y shouldn't change |
| 126 | + }); |
| 127 | + |
| 128 | + it('should reposition when element is outside top viewport boundary', () => { |
| 129 | + // Mock viewport size |
| 130 | + vi.spyOn(window, 'innerWidth', 'get').mockReturnValue(800); |
| 131 | + vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(600); |
| 132 | + |
| 133 | + mockRef.current.getBoundingClientRect = vi.fn().mockReturnValue({ |
| 134 | + width: 100, |
| 135 | + height: 100, |
| 136 | + top: -50, |
| 137 | + left: 100, |
| 138 | + right: 200, |
| 139 | + bottom: 50, |
| 140 | + }); |
| 141 | + |
| 142 | + renderHook(() => |
| 143 | + useRespositionOnWindowResize( |
| 144 | + mockRef, |
| 145 | + { x: 100, y: -50 }, |
| 146 | + mockResetPosition, |
| 147 | + ), |
| 148 | + ); |
| 149 | + |
| 150 | + window.dispatchEvent(new Event('resize')); |
| 151 | + |
| 152 | + const callback = mockResetPosition.mock.calls[0][0]; |
| 153 | + const newPosition = callback({ x: 100, y: -50 }); |
| 154 | + expect(newPosition.x).toBe(100); // x shouldn't change |
| 155 | + expect(newPosition.y).toBe(10); // reset to 10 |
| 156 | + }); |
| 157 | +}); |
0 commit comments