Skip to content

Commit 5ad1a8e

Browse files
committed
add tests for throttleTimeout option
1 parent c4e80b4 commit 5ad1a8e

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/index.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,63 @@ describe('useViewportSizes', () => {
117117
jest.runAllTimers();
118118
expect(screen.getByTestId('vpw').textContent).toEqual('500');
119119
});
120+
121+
test('debounces updated render of vpw/vph with debounceTimeout', async () => {
122+
jest.useFakeTimers();
123+
setViewportDimensions(640, 480);
124+
await jest.runAllTimersAsync();
125+
render(<DimensionsView options={{ debounceTimeout: 500 }} />);
126+
await act(async () => {
127+
await jest.runAllTimersAsync();
128+
});
129+
expect(screen.getByTestId('vpw').textContent).toEqual('640');
130+
expect(screen.getByTestId('vph').textContent).toEqual('480');
131+
132+
await act(async () => {
133+
await jest.advanceTimersByTimeAsync(100);
134+
});
135+
expect(screen.getByTestId('vpw').textContent).toEqual('640');
136+
expect(screen.getByTestId('vph').textContent).toEqual('480');
137+
138+
await act(async () => {
139+
await jest.advanceTimersByTimeAsync(450);
140+
setViewportDimensions(100, 100);
141+
await jest.runAllTimersAsync();
142+
});
143+
expect(screen.getByTestId('vpw').textContent).toEqual('100');
144+
expect(screen.getByTestId('vph').textContent).toEqual('100');
145+
});
146+
147+
test('throttles updated render of vpw/vph with throttleTimeout', async () => {
148+
jest.useFakeTimers();
149+
150+
setViewportDimensions(640, 480);
151+
render(<DimensionsView options={{ throttleTimeout: 100 }} />);
152+
153+
await act(async () => {
154+
await jest.runAllTimersAsync();
155+
setViewportDimensions(200, 200);
156+
await jest.advanceTimersByTimeAsync(50);
157+
});
158+
159+
expect(screen.getByTestId('vpw').textContent).toEqual('640');
160+
expect(screen.getByTestId('vph').textContent).toEqual('480');
161+
162+
await act(async () => {
163+
await jest.advanceTimersByTimeAsync(150);
164+
});
165+
166+
expect(screen.getByTestId('vpw').textContent).toEqual('200');
167+
expect(screen.getByTestId('vph').textContent).toEqual('200');
168+
169+
await act(async () => {
170+
await jest.advanceTimersByTimeAsync(450);
171+
setViewportDimensions(100, 100);
172+
await jest.runAllTimersAsync();
173+
});
174+
175+
expect(screen.getByTestId('vpw').textContent).toEqual('100');
176+
expect(screen.getByTestId('vph').textContent).toEqual('100');
177+
});
120178
});
121179
});

0 commit comments

Comments
 (0)