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

[React@18] fix outstanding easy unit tests #206917

Merged

Conversation

Dosant
Copy link
Contributor

@Dosant Dosant commented Jan 16, 2025

Summary

Extracted remaining easy backward-compatible unit test fixes that fail with React@18 from #206411

The idea is that the tests should pass for both React@17 and React@18 (legacy mode):
To run with react@18:

REACT_18=true yarn test:jest src/platform/plugins/private/kibana_overview/public/components/overview/overview.test.tsx

Also to run the app with React@18 please use this pr #196673 or from main:

REACT_18=true yarn bootstrap
REACT_18=true yarn start

* Without those styles in DOM the custom snapshot serializer is not able to replace the emotion class names.
* This workaround ensures a fake emotion style tag is present in the DOM before rendering the component with enzyme making the snapshot serializer work.
*/
function mockEnsureEmotionStyleTag() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I described the problem in the comment, but here is the thread with investigation for even more context https://elastic.slack.com/archives/C7QC1JV6F/p1736790569444059

The problem is that snapshot serialization didn't work with React@18 and import {render} from 'enzyme' .

-     class="euiText emotion-euiText-m"
+     class="euiText css-1py2xv4-euiText-m"

Longer term we should get rid of enzyme. I plan to start this conversation post react@18 work

@@ -80,6 +80,8 @@ console.error = (...args) => {
* Tracking issue to clean this up https://github.com/elastic/kibana/issues/199100
*/
if (REACT_VERSION.startsWith('18.')) {
console.warn('Running with React@18 and muting the legacy ReactDOM.render warning');
if (!process.env.CI) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

just wanted to reduce noise on ci

const componentErrors = consoleErrorSpy.mock.calls.filter(
(c) =>
!c[0]?.startsWith?.(
'Warning: ReactDOM.render'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixing [job] [logs] Jest Tests #3 / Filter Group Component Filter Changed Banner should ignore url params if there is an error in using them

The problem is that when running React@18 in legacy mode there is an additional log message that warns about running in legacy mode. Since this is the only place that got broken because of it, I haven't come up with anything better then just adjusting in place :)

Copy link
Member

Choose a reason for hiding this comment

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

This test is so weird. Ideally we should not check the console log but this is a conversation for another day 🙂.

@@ -381,7 +381,7 @@ describe('action_type_form', () => {
jest.useFakeTimers({ legacyFakeTimers: true });
wrapper.find('[data-test-subj="action-group-error-icon"]').first().simulate('mouseOver');
// Run the timers so the EuiTooltip will be visible
jest.runAllTimers();
jest.runOnlyPendingTimers();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixing [job] [logs] Jest Tests #4 / ConnectorForm calls onChange when the form is invalid

I assumed that runAllTimers caused tooltip to close before we checked that it is present 🤷‍♂️

expect(screen.getByTestId('infraUnLinkDashboardMenu')).toBeDisabled();
expect(screen.getByTestId('infraUnLinkDashboardMenu')).toHaveTextContent('Unlink dashboard');

describe('UnlinkDashboard', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eokoneyo fixed [job] [logs] Jest Tests #2 / Custom Dashboards Actions should render the unlink dashboard action when the user can unlink a dashboard

[job] [logs] Jest Tests #2 / Custom Dashboards Actions should render the unlink dashboard action when the user cannot unlink a dashboard

Added this in to fix the issue here, the mocked hook here requires another hook that would have required a mock, hence the need to mock this.

@@ -110,14 +110,14 @@ describe('<FilterByAssigneesPopover />', () => {
const onUsersChangeMock = jest.fn();
const { getByTestId, getByText } = renderFilterByAssigneesPopover([], onUsersChangeMock);

getByTestId(FILTER_BY_ASSIGNEES_BUTTON).click();
fireEvent.click(getByTestId(FILTER_BY_ASSIGNEES_BUTTON));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixes [job] [logs] Jest Tests #8 / should call onUsersChange on closing the popover

Looks like with an update the last event can be missed before the assertion. By using fireEvent we wrap each event in act allowing tests to process pending events.

getByTestId(ASSIGNEES_ADD_BUTTON_TEST_ID).click();
getByText('User 1').click();
getByText('User 3').click();
fireEvent.click(getByTestId(ASSIGNEES_ADD_BUTTON_TEST_ID));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

[job] [logs] Jest Tests #9 / should call assignees update functionality with the right arguments

Looks like with an update the last event can be missed before the assertion. By using fireEvent we wrap each event in act allowing tests to process pending events.

@@ -21,7 +21,7 @@ describe('useOnExpandableFlyoutClose', () => {

window.removeEventListener = jest.fn().mockImplementationOnce((event, callback) => {});

renderHook(() => useOnExpandableFlyoutClose({ callback: callbackFct }));
const { unmount } = renderHook(() => useOnExpandableFlyoutClose({ callback: callbackFct }));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix [job] [logs] Jest Tests #9 / useOnExpandableFlyoutClose should run the callback function and remove the event listener from the window

I just assume this test wasn't testing what was intended. It might be that removeEventListener was called on the initial render during effect re-rerun and that is what the assertion was cathing.

@Dosant Dosant changed the title fix easy jest test with react@18 [React@18] fix outstanding easy unit tests Jan 16, 2025
@Dosant Dosant added release_note:skip Skip the PR/issue when compiling release notes Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) React@18 labels Jan 16, 2025
@Dosant Dosant marked this pull request as ready for review January 16, 2025 12:42
@Dosant Dosant requested review from a team as code owners January 16, 2025 12:42
@elasticmachine
Copy link
Contributor

Pinging @elastic/appex-sharedux (Team:SharedUX)

Copy link
Member

@dmlemeshko dmlemeshko left a comment

Choose a reason for hiding this comment

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

kbn-test changes lgtm

const componentErrors = consoleErrorSpy.mock.calls.filter(
(c) =>
!c[0]?.startsWith?.(
'Warning: ReactDOM.render'
Copy link
Member

Choose a reason for hiding this comment

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

This test is so weird. Ideally we should not check the console log but this is a conversation for another day 🙂.

Copy link
Contributor

@rmyz rmyz left a comment

Choose a reason for hiding this comment

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

LGTM

@Dosant
Copy link
Contributor Author

Dosant commented Jan 17, 2025

@elasticmachine merge upstream

@Dosant Dosant enabled auto-merge (squash) January 17, 2025 10:30
@Dosant Dosant merged commit 3ca02b3 into elastic:main Jan 17, 2025
8 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/12828554923

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Jan 17, 2025
## Summary

Extracted remaining easy backward-compatible unit test fixes that fail
with React@18 from elastic#206411

The idea is that the tests should pass for both React@17 and React@18

(cherry picked from commit 3ca02b3)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Jan 17, 2025
# Backport

This will backport the following commits from `main` to `8.x`:
- [[React@18] fix outstanding easy unit tests
(#206917)](#206917)

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

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

<!--BACKPORT [{"author":{"name":"Anton
Dosov","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-17T11:55:08Z","message":"[React@18]
fix outstanding easy unit tests (#206917)\n\n## Summary\r\n\r\nExtracted
remaining easy backward-compatible unit test fixes that fail\r\nwith
React@18 from https://github.com/elastic/kibana/pull/206411\r\n\r\nThe
idea is that the tests should pass for both React@17 and
React@18","sha":"3ca02b324051adfa54965cfc3985abdc2a93b8a3","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:SharedUX","backport:prev-minor","React@18"],"title":"[React@18]
fix outstanding easy unit
tests","number":206917,"url":"https://github.com/elastic/kibana/pull/206917","mergeCommit":{"message":"[React@18]
fix outstanding easy unit tests (#206917)\n\n## Summary\r\n\r\nExtracted
remaining easy backward-compatible unit test fixes that fail\r\nwith
React@18 from https://github.com/elastic/kibana/pull/206411\r\n\r\nThe
idea is that the tests should pass for both React@17 and
React@18","sha":"3ca02b324051adfa54965cfc3985abdc2a93b8a3"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/206917","number":206917,"mergeCommit":{"message":"[React@18]
fix outstanding easy unit tests (#206917)\n\n## Summary\r\n\r\nExtracted
remaining easy backward-compatible unit test fixes that fail\r\nwith
React@18 from https://github.com/elastic/kibana/pull/206411\r\n\r\nThe
idea is that the tests should pass for both React@17 and
React@18","sha":"3ca02b324051adfa54965cfc3985abdc2a93b8a3"}}]}]
BACKPORT-->

Co-authored-by: Anton Dosov <[email protected]>
cqliu1 pushed a commit to cqliu1/kibana that referenced this pull request Jan 17, 2025
## Summary

Extracted remaining easy backward-compatible unit test fixes that fail
with React@18 from elastic#206411

The idea is that the tests should pass for both React@17 and React@18
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:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants