Skip to content

Commit

Permalink
[8.x] [SecuritySolution] Fixes flaky test (#203069) (#203098)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[SecuritySolution] Fixes flaky test
(#203069)](#203069)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Sergi
Massaneda","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-12-05T12:53:50Z","message":"[SecuritySolution]
Fixes flaky test (#203069)\n\n## Summary\r\n\r\nfixes:
https://github.com/elastic/kibana/issues/202147\r\n\r\nFixes flakyness
on:
\r\n\r\n`x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts`","sha":"54e103211ab9fb2eb844f4e5f8b1f9f45eb6e45d","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:version","v8.18.0"],"title":"[SecuritySolution]
Fixes flaky
test","number":203069,"url":"https://github.com/elastic/kibana/pull/203069","mergeCommit":{"message":"[SecuritySolution]
Fixes flaky test (#203069)\n\n## Summary\r\n\r\nfixes:
https://github.com/elastic/kibana/issues/202147\r\n\r\nFixes flakyness
on:
\r\n\r\n`x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts`","sha":"54e103211ab9fb2eb844f4e5f8b1f9f45eb6e45d"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/203069","number":203069,"mergeCommit":{"message":"[SecuritySolution]
Fixes flaky test (#203069)\n\n## Summary\r\n\r\nfixes:
https://github.com/elastic/kibana/issues/202147\r\n\r\nFixes flakyness
on:
\r\n\r\n`x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts`","sha":"54e103211ab9fb2eb844f4e5f8b1f9f45eb6e45d"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Sergi Massaneda <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2024
1 parent cae5444 commit d8e9182
Showing 1 changed file with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand All @@ -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(),
Expand All @@ -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' });
});
});
});

Expand All @@ -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();
});
});
});

Expand All @@ -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' });
});
});
});
});
Expand Down

0 comments on commit d8e9182

Please sign in to comment.