Skip to content

Commit

Permalink
add tests for throttleTimeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
rob2d committed Oct 12, 2023
1 parent c4e80b4 commit 5ad1a8e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,63 @@ describe('useViewportSizes', () => {
jest.runAllTimers();
expect(screen.getByTestId('vpw').textContent).toEqual('500');
});

test('debounces updated render of vpw/vph with debounceTimeout', async () => {
jest.useFakeTimers();
setViewportDimensions(640, 480);
await jest.runAllTimersAsync();
render(<DimensionsView options={{ debounceTimeout: 500 }} />);
await act(async () => {
await jest.runAllTimersAsync();
});
expect(screen.getByTestId('vpw').textContent).toEqual('640');
expect(screen.getByTestId('vph').textContent).toEqual('480');

await act(async () => {
await jest.advanceTimersByTimeAsync(100);
});
expect(screen.getByTestId('vpw').textContent).toEqual('640');
expect(screen.getByTestId('vph').textContent).toEqual('480');

await act(async () => {
await jest.advanceTimersByTimeAsync(450);
setViewportDimensions(100, 100);
await jest.runAllTimersAsync();
});
expect(screen.getByTestId('vpw').textContent).toEqual('100');
expect(screen.getByTestId('vph').textContent).toEqual('100');
});

test('throttles updated render of vpw/vph with throttleTimeout', async () => {
jest.useFakeTimers();

setViewportDimensions(640, 480);
render(<DimensionsView options={{ throttleTimeout: 100 }} />);

await act(async () => {
await jest.runAllTimersAsync();
setViewportDimensions(200, 200);
await jest.advanceTimersByTimeAsync(50);
});

expect(screen.getByTestId('vpw').textContent).toEqual('640');
expect(screen.getByTestId('vph').textContent).toEqual('480');

await act(async () => {
await jest.advanceTimersByTimeAsync(150);
});

expect(screen.getByTestId('vpw').textContent).toEqual('200');
expect(screen.getByTestId('vph').textContent).toEqual('200');

await act(async () => {
await jest.advanceTimersByTimeAsync(450);
setViewportDimensions(100, 100);
await jest.runAllTimersAsync();
});

expect(screen.getByTestId('vpw').textContent).toEqual('100');
expect(screen.getByTestId('vph').textContent).toEqual('100');
});
});
});

0 comments on commit 5ad1a8e

Please sign in to comment.