Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React18] Migrate test suites to account for testing library upgrades security-solution #201176

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

eokoneyo
Copy link
Contributor

@eokoneyo eokoneyo commented Nov 21, 2024

This PR migrates test suites that use renderHook from the library @testing-library/react-hooks to adopt the equivalent and replacement of renderHook from the export that is now available from
@testing-library/react. This work is required for the planned migration to react18.

Context

In this PR, usages of waitForNextUpdate that previously could have been destructured from renderHook are now been replaced with waitFor exported from @testing-library/react, furthermore waitFor
that would also have been destructured from the same renderHook result is now been replaced with waitFor from the export of @testing-library/react.

Why is waitFor a sufficient enough replacement for waitForNextUpdate, and better for testing values subject to async computations?

WaitFor will retry the provided callback if an error is returned, till the configured timeout elapses. By default the retry interval is 50ms with a timeout value of 1000ms that
effectively translates to at least 20 retries for assertions placed within waitFor. See https://testing-library.com/docs/dom-testing-library/api-async/#waitfor for more information.
This however means that for person's writing tests, said person has to be explicit about expectations that describe the internal state of the hook being tested.
This implies checking for instance when a react query hook is being rendered, there's an assertion that said hook isn't loading anymore.

In this PR you'd notice that this pattern has been adopted, with most existing assertions following an invocation of waitForNextUpdate being placed within a waitFor
invocation. In some cases the replacement is simply a waitFor(() => new Promise((resolve) => resolve(null))) (many thanks to @kapral18, for point out exactly why this works),
where this suffices the assertions that follow aren't placed within a waitFor so this PR doesn't get larger than it needs to be.

It's also worth pointing out this PR might also contain changes to test and application code to improve said existing test.

What to do next?

  1. Review the changes in this PR.
  2. If you think the changes are correct, approve the PR.

Any questions?

If you have any questions or need help with this PR, please leave comments in this PR.

@eokoneyo eokoneyo added release_note:skip Skip the PR/issue when compiling release notes Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) React@18 labels Nov 21, 2024
@eokoneyo eokoneyo self-assigned this Nov 21, 2024
@elasticmachine
Copy link
Contributor

🤖 Jobs for this PR can be triggered through checkboxes. 🚧

ℹ️ To trigger the CI, please tick the checkbox below 👇

  • Click to trigger kibana-pull-request for this PR!
  • Click to trigger kibana-deploy-project-from-pr for this PR!

@eokoneyo
Copy link
Contributor Author

/ci

@eokoneyo eokoneyo marked this pull request as ready for review November 21, 2024 17:22
@eokoneyo eokoneyo requested a review from a team as a code owner November 21, 2024 17:22
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

@eokoneyo
Copy link
Contributor Author

@elasticmachine merge upstream

1 similar comment
@eokoneyo
Copy link
Contributor Author

@elasticmachine merge upstream

@eokoneyo eokoneyo force-pushed the chore/testing-library-migration-for-security-solution branch from 88cae85 to 0ec6d48 Compare December 2, 2024 10:51
@eokoneyo
Copy link
Contributor Author

eokoneyo commented Dec 4, 2024

@elasticmachine merge upstream

@elasticmachine elasticmachine requested review from a team as code owners December 4, 2024 13:18
@eokoneyo eokoneyo force-pushed the chore/testing-library-migration-for-security-solution branch from 57858f5 to ef19e82 Compare December 5, 2024 13:42
@eokoneyo
Copy link
Contributor Author

eokoneyo commented Dec 9, 2024

@elasticmachine merge upstream

signalIndexMappingOutdated: null,
});
expect(result.error).toBeUndefined();
expect(result.current).toEqual({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removing expect(result.all).toHaveLength(1); and expect(result.error).toBeUndefined(); here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renderHook no longer returns the result.all and result.error property

Copy link
Contributor

@PhilippeOberti PhilippeOberti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes for the Threat Hunting Investigations team look good. I left this comment just in case it wasn't done on purpose

@eokoneyo
Copy link
Contributor Author

@elasticmachine merge upstream

@maximpn maximpn requested review from maximpn and removed request for dplumlee December 11, 2024 14:37
@maximpn
Copy link
Contributor

maximpn commented Dec 11, 2024

Files by Code Owner

elastic/security-detection-rule-management

  • x-pack/plugins/security_solution/public/common/components/toolbar/bulk_actions/use_bulk_alert_assignees_items.test.tsx
  • x-pack/plugins/security_solution/public/common/components/toolbar/bulk_actions/use_bulk_alert_tags_items.test.tsx

elastic/security-solution

  • x-pack/plugins/security_solution/public/app/solution_navigation/use_panel_side_nav_items.test.tsx
  • x-pack/plugins/security_solution/public/common/components/alert_count_by_status/use_alert_count_by_rule_by_status.test.ts
  • x-pack/plugins/security_solution/public/common/components/chart_settings_popover/configurations/default/index.test.tsx
  • x-pack/plugins/security_solution/public/common/components/discover_in_timeline/use_discover_in_timeline_actions.test.tsx
  • x-pack/plugins/security_solution/public/common/components/local_storage/index.test.tsx
  • x-pack/plugins/security_solution/public/common/components/ml/anomaly/use_anomalies_search.test.ts
  • x-pack/plugins/security_solution/public/common/components/ml/hooks/use_installed_security_jobs.test.ts
  • x-pack/plugins/security_solution/public/common/components/ml/hooks/use_ml_rule_validations.test.ts
  • x-pack/plugins/security_solution/public/common/components/ml/links/create_explorer_link.test.tsx
  • x-pack/plugins/security_solution/public/common/components/toolbar/bulk_actions/use_bulk_alert_assignees_items.test.tsx
  • x-pack/plugins/security_solution/public/common/components/toolbar/bulk_actions/use_bulk_alert_tags_items.test.tsx
  • x-pack/plugins/security_solution/public/common/components/user_privileges/endpoint/use_endpoint_privileges.test.ts
  • x-pack/plugins/security_solution/public/common/components/user_profiles/use_bulk_get_user_profiles.test.tsx
  • x-pack/plugins/security_solution/public/common/components/user_profiles/use_get_current_user_profile.test.tsx
  • x-pack/plugins/security_solution/public/common/components/user_profiles/use_suggest_users.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/alerts/alerts_by_status_donut.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/alerts/alerts_histogram.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/alerts/alerts_table.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/alerts/rule_preview.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/authentication.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/event.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/external_alert.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_host_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_host_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_unique_ips_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_unique_ips_bar.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_unique_ips_destination_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_unique_ips_source_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/dns_top_domains.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_dns_queries.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_network_events.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_tls_handshakes.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_flow_ids.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_private_ips_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_private_ips_bar.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_private_ips_destination_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_private_ips_source_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_total_users_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_total_users_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_user_authentication_metric_failure.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_user_authentications_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_user_authentications_bar.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_user_authentications_metric_success.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/mocks.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_actions.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_add_to_existing_case.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_add_to_new_case.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_lens_attributes.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_save_to_library.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_visualization_response.test.tsx
  • x-pack/plugins/security_solution/public/common/containers/events/last_event_time/index.test.ts
  • x-pack/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx
  • x-pack/plugins/security_solution/public/common/containers/query_toggle/index.test.tsx
  • x-pack/plugins/security_solution/public/common/containers/related_entities/related_hosts/index.test.tsx
  • x-pack/plugins/security_solution/public/common/containers/related_entities/related_users/index.test.tsx
  • x-pack/plugins/security_solution/public/common/containers/source/index.test.tsx
  • x-pack/plugins/security_solution/public/common/containers/use_first_last_seen/use_first_last_seen.test.ts
  • x-pack/plugins/security_solution/public/common/containers/use_full_screen/index.test.tsx
  • x-pack/plugins/security_solution/public/common/containers/use_global_time/index.test.tsx
  • x-pack/plugins/security_solution/public/common/containers/use_search_strategy/index.test.ts
  • x-pack/plugins/security_solution/public/common/hooks/timeline/use_query_timeline_by_id_on_url_change.test.ts
  • x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.test.ts
  • x-pack/plugins/security_solution/public/common/hooks/use_data_view_id.test.ts
  • x-pack/plugins/security_solution/public/common/hooks/use_error_toast.test.ts
  • x-pack/plugins/security_solution/public/common/hooks/use_fetch/use_fetch.test.tsx
  • x-pack/plugins/security_solution/public/common/hooks/use_global_filter_query.test.ts
  • x-pack/plugins/security_solution/public/common/hooks/use_invalid_filter_query.test.tsx
  • x-pack/plugins/security_solution/public/common/hooks/use_navigate_to_alerts_page_with_filters.test.ts
  • x-pack/plugins/security_solution/public/common/hooks/use_resolve_conflict.test.tsx
  • x-pack/plugins/security_solution/public/common/hooks/use_resolve_redirect.test.ts
  • x-pack/plugins/security_solution/public/common/hooks/use_upselling.test.tsx
  • x-pack/plugins/security_solution/public/common/links/links.test.tsx
  • x-pack/plugins/security_solution/public/common/links/use_find_app_links_by_path.test.ts
  • x-pack/plugins/security_solution/public/common/mock/mock_assistant_provider.tsx
  • x-pack/plugins/security_solution/public/common/mock/test_providers.tsx
  • x-pack/plugins/security_solution/public/common/store/reducer.test.tsx
  • x-pack/plugins/security_solution/public/common/utils/get_mapped_non_ecs_value.test.ts
  • x-pack/plugins/security_solution/public/common/utils/global_query_string/helpers.test.tsx
  • x-pack/plugins/security_solution/public/common/utils/global_query_string/index.test.tsx
  • x-pack/plugins/security_solution/public/common/utils/timeline/use_show_timeline.test.tsx
  • x-pack/plugins/security_solution/public/detections/components/user_info/index.test.tsx
  • x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_alerts_privileges.test.tsx
  • x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_cases_from_alerts.test.tsx
  • x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_query.test.tsx
  • x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_signal_index.test.tsx
  • x-pack/plugins/security_solution/public/detections/hooks/alerts_visualization/use_alert_histogram_count.test.tsx
  • x-pack/plugins/security_solution/public/detections/hooks/trigger_actions_alert_table/use_cell_actions.test.tsx
  • x-pack/plugins/security_solution/public/detections/hooks/trigger_actions_alert_table/use_persistent_controls.test.tsx
  • x-pack/plugins/security_solution/public/flyout/rule_details/hooks/use_rule_details.test.ts
  • x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.test.ts
  • x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_body_config.test.ts
  • x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_completed_cards.test.ts
  • x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts

elastic/security-threat-hunting-explore

  • x-pack/plugins/security_solution/public/common/components/alert_count_by_status/use_alert_count_by_rule_by_status.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/alerts/alerts_by_status_donut.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/alerts/alerts_histogram.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/alerts/alerts_table.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/alerts/rule_preview.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/authentication.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/event.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/common/external_alert.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_host_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_host_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_unique_ips_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_unique_ips_bar.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_unique_ips_destination_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/hosts/kpi_unique_ips_source_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/dns_top_domains.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_dns_queries.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_network_events.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_tls_handshakes.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_flow_ids.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_private_ips_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_private_ips_bar.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_private_ips_destination_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/network/kpi_unique_private_ips_source_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_total_users_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_total_users_metric.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_user_authentication_metric_failure.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_user_authentications_area.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_user_authentications_bar.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/lens_attributes/users/kpi_user_authentications_metric_success.test.ts
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/mocks.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_actions.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_add_to_existing_case.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_add_to_new_case.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_lens_attributes.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_save_to_library.test.tsx
  • x-pack/plugins/security_solution/public/common/components/visualization_actions/use_visualization_response.test.tsx
  • x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.test.ts
  • x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_body_config.test.ts
  • x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_completed_cards.test.ts
  • x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/hooks/use_expanded_card.test.ts

elastic/security-threat-hunting-investigations

  • x-pack/plugins/security_solution/public/flyout/rule_details/hooks/use_rule_details.test.ts

Copy link
Contributor

@maximpn maximpn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eokoneyo Thanks for that huge effort to make React 18 upgrade possible 🙏

I have concerns regarding waitFor(() => new Promise((resolve) => resolve(null)) (it's used a lot in this PR). It's pretty tricky to comprehend why it replaces waitForNextUpdate. An utility function could be created to encapsulate waitFor(() => new Promise((resolve) => resolve(null)). Additionally it'd be great to have an explanation comment. Obviously these two don't match 1 to 1 and it's important to understand pros and cons. It'd be great to know performance and execution time wise difference between these two. It looks beneficial to get rid of such a hack in favor of explicit result state expectation though it's definitely out of scope of this PR.

Rule Management changes LGTM but the major part of the diff is in common Security Solution ownership where comments above are related.


await waitForNextUpdate();
await waitFor(() => new Promise((resolve) => resolve(null)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a comment why await waitFor(() => new Promise((resolve) => resolve(null))); gives the same result as await waitForNextUpdate(); would be great.

Is it possible to replace it with rerender invocation?

const { result } = renderHook(() => useAggregatedAnomaliesByJob({ skip: false, from, to }), {
wrapper: TestProviders,
});
await waitFor(() => new Promise((resolve) => resolve(null)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above. Maybe create a reusable utility function?

@maximpn
Copy link
Contributor

maximpn commented Dec 12, 2024

Hi @PhilippeOberti,

I'm curious for better understanding. Have you looked at some files changed in elastic/security-solution ownership as well? I've noticed some common hooks are related to timelines.

@eokoneyo eokoneyo force-pushed the chore/testing-library-migration-for-security-solution branch from d749341 to 5747fce Compare December 17, 2024 16:42
@eokoneyo
Copy link
Contributor Author

I have concerns regarding waitFor(() => new Promise((resolve) => resolve(null)) (it's used a lot in this PR). It's pretty tricky to comprehend why it replaces waitForNextUpdate. An utility function could be created to encapsulate waitFor(() => new Promise((resolve) => resolve(null)). Additionally it'd be great to have an explanation comment. Obviously these two don't match 1 to 1 and it's important to understand pros and cons. It'd be great to know performance and execution time wise difference between these two. It looks beneficial to get rid of such a hack in favor of explicit result state expectation though it's definitely out of scope of this PR.

Rule Management changes LGTM but the major part of the diff is in common Security Solution ownership where comments above are related.

Hi @maximpn there's no 1-to-1 replacement for waitForNextUpdate, but this particular approach gets use as close as we can to waitForNextUpdate. It's worth mentioning that waitFor is the recommended replacement for waitForNextUpdate, see here. That being said passing this specific callback allows us to hook into a specific part of the waitFor implementation that not only verifies that the function doesn't throw but actually waits, see here

@maximpn
Copy link
Contributor

maximpn commented Dec 18, 2024

there's no 1-to-1 replacement for waitForNextUpdate, but this particular approach gets use as close as we can to waitForNextUpdate. It's worth mentioning that waitFor is the recommended replacement for waitForNextUpdate, see testing-library/react-testing-library#1145. That being said passing this specific callback allows us to hook into a specific part of the waitFor implementation that not only verifies that the function doesn't throw but actually waits, see here

Hi @eokoneyo,

such question I asked may pop up in everyone's mind reading tests code. That's why I suggest to create an utility function named waitForNextTick or something like that. And add the explanation above to the function as a comment. It's important to also specify that waitForNextTick is some kind of a hack and the best option is to use waitFor to wait for some some value based on the business logic. Obviously you can't addresses such domain specific tasks in a scope of this PR. It will be up to the code owners to improve it.

Having multiple entries waitFor(() => new Promise((resolve) => resolve(null)) will cause questions but won't provide answers and actions items.

Do you have any concerns with that?

@eokoneyo eokoneyo force-pushed the chore/testing-library-migration-for-security-solution branch from 5747fce to 198bc5e Compare December 18, 2024 11:32
@eokoneyo eokoneyo force-pushed the chore/testing-library-migration-for-security-solution branch from 198bc5e to a80bf74 Compare December 18, 2024 11:36
@eokoneyo
Copy link
Contributor Author

there's no 1-to-1 replacement for waitForNextUpdate, but this particular approach gets use as close as we can to waitForNextUpdate. It's worth mentioning that waitFor is the recommended replacement for waitForNextUpdate, see testing-library/react-testing-library#1145. That being said passing this specific callback allows us to hook into a specific part of the waitFor implementation that not only verifies that the function doesn't throw but actually waits, see here

such question I asked may pop up in everyone's mind reading tests code. That's why I suggest to create an utility function named waitForNextTick or something like that. And add the explanation above to the function as a comment. It's important to also specify that waitForNextTick is some kind of a hack and the best option is to use waitFor to wait for some some value based on the business logic. Obviously you can't addresses such domain specific tasks in a scope of this PR. It will be up to the code owners to improve it.

Having multiple entries waitFor(() => new Promise((resolve) => resolve(null)) will cause questions but won't provide answers and actions items.

Do you have any concerns with that?

Hi @maximpn I'm sorry we've had this long conversation, whilst attempting to act on your suggestion, I took a second attempt at the test and actually removed all instances of the "hack".

@eokoneyo eokoneyo requested a review from maximpn December 18, 2024 15:06
Copy link
Contributor

@kapral18 kapral18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Epic work. Thanks much.

Copy link
Contributor

@maximpn maximpn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eokoneyo Thanks for addressing my comments 🙏 Tests look much better now 👍

});

mockSearchStrategy.mockReturnValue(searchStrategy$.asObservable());

(useKibana as jest.Mock).mockReturnValue(mockUseKibana);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Ideally we should have mock in each test below. The latter one will have of({lastSeen: '1 minute ago'}. It wil improve readability.

    (useKibana as jest.Mock).mockReturnValue({
      search: jest.fn(). mockReturnValue(of({ lastSeen: null }))
    });

@Dosant
Copy link
Contributor

Dosant commented Dec 20, 2024

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Unknown metric groups

ESLint disabled in files

id before after diff
securitySolution 84 83 -1

Total ESLint disabled count

id before after diff
securitySolution 656 655 -1

History

cc @eokoneyo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) React@18 release_note:skip Skip the PR/issue when compiling release notes Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants