Skip to content
Snippets Groups Projects
Commit 015a176b authored by Oisin Coveney's avatar Oisin Coveney
Browse files

Just get the major release number and check that in actual places

parent 026adec1
Branches
Tags
No related merge requests found
import React from "react";
import { useSyncExternalStore } from "use-sync-external-store/shim";
import { isReact17OrEarlier } from "metabase/lib/react-compat";
import { getMajorReactVersion } from "metabase/lib/compat/check-version";
// Monkey-patches useSyncExternalStore if we are in React 17,
// where useSyncExternalStore is not available.
// This is used in react-redux v8, until they've dropped the shim in v9.
if (isReact17OrEarlier()) {
const reactVersion = getMajorReactVersion();
if (reactVersion <= 17) {
React.useSyncExternalStore = useSyncExternalStore;
}
export const isReactVersionLessThanOrEqualTo17 = (version: string) => {
const versionParts = version.split(".").map(Number);
return versionParts[0] < 17 || versionParts[0] === 17;
import React from "react";
export const getMajorReactVersion = () => {
const versionParts = React.version.split(".").map(Number);
return versionParts[0];
};
// isReactVersion.test.js
import { isReactVersionLessThanOrEqualTo17 } from "./check-version"; // Adjust the path as necessary
import React from "react";
describe("isReactVersionLessThanOrEqualTo17", () => {
import { getMajorReactVersion } from "./check-version";
describe("getMajorReactVersion", () => {
const versions = [
{ version: "0.14.0", expected: true },
{ version: "15.0.0", expected: true },
{ version: "16.0.0", expected: true },
{ version: "16.8.0", expected: true },
{ version: "17.0.0", expected: true },
{ version: "17.0.2", expected: true },
{ version: "18.0.0", expected: false },
{ version: "18.1.0", expected: false },
{ version: "0.14.0", expected: 0 },
{ version: "15.0.0", expected: 15 },
{ version: "16.0.0", expected: 16 },
{ version: "16.8.0", expected: 16 },
{ version: "17.0.0", expected: 17 },
{ version: "17.0.2", expected: 17 },
{ version: "18.0.0", expected: 18 },
{ version: "18.1.0", expected: 18 },
];
versions.forEach(({ version, expected }) => {
test(`returns ${expected} for React version ${version}`, () => {
// Call the function with the version string and check the output
expect(isReactVersionLessThanOrEqualTo17(version)).toBe(expected);
beforeEach(() => {
jest.resetModules();
});
it.each(versions)(
"should return $expected for React version $version",
({ version, expected }) => {
Object.defineProperty(React, "version", {
value: version,
writable: true,
});
expect(getMajorReactVersion()).toBe(expected);
},
);
it("should return NaN for an invalid version string", () => {
Object.defineProperty(React, "version", {
value: "invalid-version",
writable: true,
});
expect(getMajorReactVersion()).toBeNaN();
});
});
// Support React 17 backwards compatibility for the Embedding SDK
import React from "react";
import type React from "react";
import ReactDOM from "react-dom";
import { type Root, createRoot } from "react-dom/client";
import { isReactVersionLessThanOrEqualTo17 } from "./compat/check-version";
// React 18 and later has the useSyncExternalStore hook.
export const isReact17OrEarlier = () => !("useSyncExternalStore" in React);
import { getMajorReactVersion } from "./compat/check-version";
export function renderRoot(
content: React.JSX.Element,
element: Element,
): Root | undefined {
if (isReactVersionLessThanOrEqualTo17(React.version)) {
const reactVersion = getMajorReactVersion();
if (reactVersion <= 17) {
ReactDOM.render(content, element);
return;
}
......@@ -25,7 +23,9 @@ export function renderRoot(
}
export function unmountRoot(root?: Root, element?: Element) {
if (isReactVersionLessThanOrEqualTo17(React.version) && element) {
const reactVersion = getMajorReactVersion();
if (reactVersion <= 17 && element) {
ReactDOM.unmountComponentAtNode(element);
return;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment