From ecf096061e572b12246969210d1140bfb65380b3 Mon Sep 17 00:00:00 2001 From: Sergi Massaneda Date: Thu, 5 Dec 2024 13:53:50 +0100 Subject: [PATCH] [SecuritySolution] Fixes flaky test (#203069) ## Summary fixes: https://github.com/elastic/kibana/issues/202147 Fixes flakyness on: `x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts` (cherry picked from commit 54e103211ab9fb2eb844f4e5f8b1f9f45eb6e45d) --- .../hooks/use_expanded_card.test.ts | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts index 26612d83b565f..0c053b5ea0b69 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts @@ -6,12 +6,9 @@ */ import { renderHook, act } from '@testing-library/react-hooks'; -import { useExpandedCard } from './use_expanded_card'; -import { HEIGHT_ANIMATION_DURATION } from '../onboarding_card_panel.styles'; -import type { OnboardingCardId } from '../../../constants'; import { waitFor } from '@testing-library/react'; - -const scrollTimeout = HEIGHT_ANIMATION_DURATION + 50; +import type { OnboardingCardId } from '../../../constants'; +import { useExpandedCard } from './use_expanded_card'; const mockSetCardDetail = jest.fn(); jest.mock('../../hooks/use_url_detail', () => ({ @@ -28,6 +25,7 @@ describe('useExpandedCard Hook', () => { const mockCardId = 'card-1' as OnboardingCardId; const mockScrollTo = jest.fn(); global.window.scrollTo = mockScrollTo; + jest.useFakeTimers(); const mockGetElementById = jest.fn().mockReturnValue({ focus: jest.fn(), @@ -46,13 +44,10 @@ describe('useExpandedCard Hook', () => { it('should scroll to the expanded card id from the hash', async () => { // Ensure that scroll and focus were triggered - await waitFor( - () => { - expect(mockGetElementById).toHaveBeenCalledWith(mockCardId); - expect(mockScrollTo).toHaveBeenCalledWith({ top: 60, behavior: 'smooth' }); - }, - { timeout: scrollTimeout } - ); + await waitFor(() => { + expect(mockGetElementById).toHaveBeenCalledWith(mockCardId); + expect(mockScrollTo).toHaveBeenCalledWith({ top: 60, behavior: 'smooth' }); + }); }); }); @@ -78,13 +73,10 @@ describe('useExpandedCard Hook', () => { it('should not scroll', async () => { // Ensure that scroll and focus were triggered - await waitFor( - () => { - expect(mockGetElementById).not.toHaveBeenCalled(); - expect(mockScrollTo).not.toHaveBeenCalled(); - }, - { timeout: scrollTimeout } - ); + await waitFor(() => { + expect(mockGetElementById).not.toHaveBeenCalled(); + expect(mockScrollTo).not.toHaveBeenCalled(); + }); }); }); @@ -102,13 +94,10 @@ describe('useExpandedCard Hook', () => { it('should scroll', async () => { // Ensure that scroll and focus were triggered - await waitFor( - () => { - expect(mockGetElementById).toHaveBeenCalledWith(mockCardId); - expect(mockScrollTo).toHaveBeenCalledWith({ top: 160, behavior: 'smooth' }); - }, - { timeout: scrollTimeout } - ); + await waitFor(() => { + expect(mockGetElementById).toHaveBeenCalledWith(mockCardId); + expect(mockScrollTo).toHaveBeenCalledWith({ top: 160, behavior: 'smooth' }); + }); }); }); });