From 5ad1a8ea6cc1862d8ad8d1f24471c4e85941bd6c Mon Sep 17 00:00:00 2001 From: Robert Concepcion III Date: Wed, 11 Oct 2023 20:21:42 -0400 Subject: [PATCH] add tests for throttleTimeout option --- src/index.test.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/index.test.js b/src/index.test.js index 9a3bfdc..9bbbba3 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -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(); + 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(); + + 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'); + }); }); });