Skip to content

Commit 318f0a3

Browse files
Backport 1.28.1 => main (#2432)
DES-6853 React 17 compatibility (as patch to 1.28) (#2431) * fix: fixes import in renderReactInWebComponent * test: fixes unit tests for renderReactInWebComponent --------- Co-authored-by: Kevin Ghadyani <kevin.ghadyani@okta.com> build: bump versions for 1.28.1 docs: update CHANGELOG for 1.28.1 chore(odyssey-react-mui): trigger build
1 parent b8486f7 commit 318f0a3

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

packages/odyssey-react-mui/src/ui-shell/UiShell/renderUiShell.test.tsx

+21-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* See the License for the specific language governing permissions and limitations under the License.
1111
*/
1212

13-
import { act } from "@testing-library/react";
13+
import { act, waitFor } from "@testing-library/react";
1414

1515
import { renderUiShell } from "./renderUiShell";
1616
import {
@@ -126,9 +126,11 @@ describe("renderUiShell", () => {
126126
});
127127
});
128128

129-
expect(
130-
rootElement.querySelector(reactWebComponentElementName)!.shadowRoot,
131-
).toHaveTextContent(appName);
129+
await waitFor(() => {
130+
expect(
131+
rootElement.querySelector(reactWebComponentElementName)!.shadowRoot,
132+
).toHaveTextContent(appName);
133+
});
132134
});
133135

134136
test("renders `UiShell` with immediately updated props", async () => {
@@ -153,9 +155,11 @@ describe("renderUiShell", () => {
153155
});
154156
});
155157

156-
expect(
157-
rootElement.querySelector(reactWebComponentElementName)!.shadowRoot,
158-
).toHaveTextContent(appName);
158+
await waitFor(() => {
159+
expect(
160+
rootElement.querySelector(reactWebComponentElementName)!.shadowRoot,
161+
).toHaveTextContent(appName);
162+
});
159163
});
160164

161165
test("renders `<slot>` in the event of an error", async () => {
@@ -184,14 +188,16 @@ describe("renderUiShell", () => {
184188
);
185189
});
186190

187-
consoleErrorSpy.mockRestore();
191+
await waitFor(() => {
192+
expect(onError).toHaveBeenCalledTimes(1);
193+
expect(consoleError).toHaveBeenCalledTimes(1);
194+
expect(
195+
rootElement
196+
.querySelector(reactWebComponentElementName)!
197+
.shadowRoot?.querySelector("slot"),
198+
).toBeInstanceOf(HTMLSlotElement);
199+
});
188200

189-
expect(onError).toHaveBeenCalledTimes(1);
190-
expect(consoleError).toHaveBeenCalledTimes(1);
191-
expect(
192-
rootElement
193-
.querySelector(reactWebComponentElementName)!
194-
.shadowRoot?.querySelector("slot"),
195-
).toBeInstanceOf(HTMLSlotElement);
201+
consoleErrorSpy.mockRestore();
196202
});
197203
});

packages/odyssey-react-mui/src/ui-shell/renderReactInWebComponent.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export type GetReactComponentInWebComponent = (
2525

2626
export class ReactInWebComponentElement extends HTMLElement {
2727
getReactComponent: GetReactComponentInWebComponent;
28-
reactRoot: Root;
2928
reactRootElements: ReactRootElements;
29+
reactRootPromise: Promise<Root>;
3030

3131
constructor(getReactComponent: GetReactComponentInWebComponent) {
3232
super();
@@ -50,15 +50,20 @@ export class ReactInWebComponentElement extends HTMLElement {
5050
shadowRoot.appendChild(this.reactRootElements.stylesRootElement);
5151
shadowRoot.appendChild(this.reactRootElements.appRootElement);
5252

53-
this.reactRoot = createRoot(this.reactRootElements.appRootElement);
53+
// If we want to support React v17 in the future, we can use a try-catch on the import to grab the old `ReactDOM.render` function if `react-dom/client` errors. --Kevin Ghadyani
54+
this.reactRootPromise = import("react-dom/client").then(({ createRoot }) =>
55+
createRoot(this.reactRootElements.appRootElement),
56+
);
5457
}
5558

5659
connectedCallback() {
57-
this.reactRoot.render(this.getReactComponent(this.reactRootElements));
60+
this.reactRootPromise.then((reactRoot) =>
61+
reactRoot.render(this.getReactComponent(this.reactRootElements)),
62+
);
5863
}
5964

6065
disconnectedCallback() {
61-
this.reactRoot.unmount();
66+
this.reactRootPromise.then((reactRoot) => reactRoot.unmount());
6267
}
6368
}
6469

0 commit comments

Comments
 (0)