From 88bb3bf65a8413146c0e456f7c341b53ea558d5b Mon Sep 17 00:00:00 2001 From: David Menendez Date: Fri, 29 Mar 2024 13:21:23 -0500 Subject: [PATCH 1/2] test: useWindowResizing --- .../hooks/__tests__/useWindowResize.test.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 packages/ibm-products/src/global/js/hooks/__tests__/useWindowResize.test.js diff --git a/packages/ibm-products/src/global/js/hooks/__tests__/useWindowResize.test.js b/packages/ibm-products/src/global/js/hooks/__tests__/useWindowResize.test.js new file mode 100644 index 0000000000..1d115a350b --- /dev/null +++ b/packages/ibm-products/src/global/js/hooks/__tests__/useWindowResize.test.js @@ -0,0 +1,42 @@ +/* eslint-disable react/prop-types */ +/** + * Copyright IBM Corp. 2023, 2023 + * + * This source code is licensed under the Apache-2.0 license found in the + * LICENSE file in the root directory of this source tree. + */ +import React, { useState } from 'react'; +import { useWindowResize } from '../useWindowResize'; +import { render, screen, fireEvent } from '@testing-library/react'; + +const TestComponent = (props) => { + const { throttleInterval } = props; + const [height, setHeight] = useState(false); + useWindowResize( + ({ current }) => { + const { innerHeight } = current; + setHeight(innerHeight); + }, + [], + throttleInterval + ); + return
{height}
; +}; + +describe('useWindowsResize', () => { + it('works by default', () => { + render(); + window.innerWidth = 500; + fireEvent(window, new Event('resize')); + screen.getByText('768'); + }); + + it('works with throttle', () => { + render(); + window.innerWidth = 500; + fireEvent(window, new Event('resize')); + window.innerWidth = 505; + fireEvent(window, new Event('resize')); + screen.getByText('768'); + }); +}); From ea7c76f23873877713c95e7b7ba7a1ed3450381d Mon Sep 17 00:00:00 2001 From: David Menendez Date: Fri, 29 Mar 2024 13:24:08 -0500 Subject: [PATCH 2/2] chore: typo --- .../src/global/js/hooks/__tests__/useWindowResize.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ibm-products/src/global/js/hooks/__tests__/useWindowResize.test.js b/packages/ibm-products/src/global/js/hooks/__tests__/useWindowResize.test.js index 1d115a350b..64c9713d17 100644 --- a/packages/ibm-products/src/global/js/hooks/__tests__/useWindowResize.test.js +++ b/packages/ibm-products/src/global/js/hooks/__tests__/useWindowResize.test.js @@ -1,6 +1,6 @@ /* eslint-disable react/prop-types */ /** - * Copyright IBM Corp. 2023, 2023 + * Copyright IBM Corp. 2024 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree.