This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
Remove React 17 compatability from react-testing #2820
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/react-testing': major | ||
--- | ||
|
||
Remove React 17 compatability |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,5 @@ | ||
import ReactDOM from 'react-dom'; | ||
import React from 'react'; | ||
import type {Root as ReactRoot} from 'react-dom/client'; | ||
import {act as oldAct} from 'react-dom/test-utils'; | ||
|
||
import type {ReactInstance, Fiber} from './types'; | ||
|
||
export function getInternals(instance: ReactInstance): Fiber { | ||
// In React 17+ _reactInternalFiber was renamed to _reactInternals. As such we need to handle both APIs to maintain support. | ||
|
||
if ('_reactInternalFiber' in instance) { | ||
return instance._reactInternalFiber; | ||
} | ||
|
||
return instance._reactInternals; | ||
} | ||
|
||
/** Shim to provide createRoot backwards compatibility for React < 18 */ | ||
function createRootShim(element: HTMLElement): ReactRoot { | ||
/* eslint-disable react/no-deprecated */ | ||
return { | ||
render(children: React.ReactChild | Iterable<React.ReactNode>): void { | ||
ReactDOM.render(children as any, element as any); | ||
}, | ||
unmount(): void { | ||
ReactDOM.unmountComponentAtNode(element as any); | ||
}, | ||
}; | ||
/* eslint-enable react/no-deprecated */ | ||
} | ||
|
||
/** Uses React >= 18 createRoot if available or falls back to shim*/ | ||
export function createRoot(element: HTMLElement): ReactRoot { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
return require('react-dom/client').createRoot(element); | ||
} catch { | ||
return createRootShim(element); | ||
} | ||
} | ||
|
||
export const isLegacyReact = parseInt(React.version, 10) < 18; | ||
|
||
export const act: typeof oldAct = (() => { | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving https://github.com/facebook/react/blob/main/CHANGELOG.md#1831-april-26-2024 |
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
|
@@ -50,30 +9,3 @@ export const act: typeof oldAct = (() => { | |
return oldAct; | ||
} | ||
})(); | ||
|
||
// https://github.com/facebook/react/blob/12adaffef7105e2714f82651ea51936c563fe15c/packages/shared/enqueueTask.js#L13 | ||
let enqueueTaskImpl: any = null; | ||
|
||
export function enqueueTask(task: (v: any) => void) { | ||
if (enqueueTaskImpl === null) { | ||
try { | ||
// read require off the module object to get around the bundlers. | ||
// we don't want them to detect a require and bundle a Node polyfill. | ||
const requireString = `require${Math.random()}`.slice(0, 7); | ||
const nodeRequire = module && module[requireString]; | ||
// assuming we're in node, let's try to get node's | ||
// version of setImmediate, bypassing fake timers if any. | ||
enqueueTaskImpl = nodeRequire.call(module, 'timers').setImmediate; | ||
} catch (_err) { | ||
// we're in a browser | ||
// we can't use regular timers because they may still be faked | ||
// so we try MessageChannel+postMessage instead | ||
enqueueTaskImpl = function (callback: () => void) { | ||
const channel = new MessageChannel(); | ||
channel.port1.onmessage = callback; | ||
channel.port2.postMessage(undefined); | ||
}; | ||
} | ||
} | ||
return enqueueTaskImpl!(task); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I flagged this change as a major as it deletes backwards compatibility