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..64c9713d17 --- /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. 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. + */ +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'); + }); +});