From ecc01478b55ae1a5c3a713c99d986898d2469ad2 Mon Sep 17 00:00:00 2001 From: Afsal K Date: Thu, 28 Nov 2024 09:56:31 +0530 Subject: [PATCH] test(tearsheet): implement test for onBlur method --- .../components/Tearsheet/Tearsheet.test.js | 83 +++++++++++-------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/packages/ibm-products/src/components/Tearsheet/Tearsheet.test.js b/packages/ibm-products/src/components/Tearsheet/Tearsheet.test.js index 0f7eeb44d5..4f9feba2cf 100644 --- a/packages/ibm-products/src/components/Tearsheet/Tearsheet.test.js +++ b/packages/ibm-products/src/components/Tearsheet/Tearsheet.test.js @@ -36,6 +36,7 @@ const componentNameCreateNarrow = CreateTearsheetNarrow.displayName; const onClick = jest.fn(); const onCloseReturnsFalse = jest.fn(() => false); const onCloseReturnsTrue = jest.fn(() => true); +const onBlur = jest.fn(); const createButton = `Create ${uuidv4()}`; const actions = [ @@ -92,6 +93,41 @@ const navigation = ( ); const title = `Title of the ${uuidv4()} tearsheet`; +const mainText = 'Main content 1'; +const inputId = 'stacked-input-1'; + +// eslint-disable-next-line react/prop-types +const DummyComponent = ({ props, open }) => { + const buttonRef = React.useRef(undefined); + + return ( + <> + + +
+ {mainText} + +
+
+ + ); +}; + // These are tests than apply to both Tearsheet and TearsheetNarrow // and also (with extra props and omitting button tests) to CreateTearsheetNarrow let tooManyButtonsTestedAlready = false; @@ -262,40 +298,6 @@ const commonTests = (Ts, name, props, testActions) => { }); it('should return focus to the launcher button', async () => { - const mainText = 'Main content 1'; - const inputId = 'stacked-input-1'; - - // eslint-disable-next-line react/prop-types - const DummyComponent = ({ open }) => { - const buttonRef = React.useRef(undefined); - - return ( - <> - - -
- {mainText} - -
-
- - ); - }; - const { rerender, getByText, getByTestId } = render( ); @@ -320,6 +322,19 @@ const commonTests = (Ts, name, props, testActions) => { await act(() => new Promise((resolve) => setTimeout(resolve, 0))); expect(launchButtonEl).toHaveFocus(); }); + + it('should call onBlur only once', async () => { + const { getByTestId } = render(); + + const inputEl = getByTestId(inputId); + const closeButton = screen.getByRole('button', { + name: closeIconDescription, + }); + + expect(inputEl).toHaveFocus(); + await act(() => userEvent.click(closeButton)); + expect(onBlur).toHaveBeenCalledTimes(1); + }); } it('is visible when open is true', async () => {