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