0

I am using NextJS with React Testing Library for component testing. I have a tinymce editor that loads from a CDN. When I tried to render my component which contains the tinymce component with React Testing Library, it failed. This is my Editor.jsx component:

"use client";

import { useRef } from "react";
import { Editor as TinymceEditor } from "@tinymce/tinymce-react";
import { init } from "./tinymceUtils";
import { Box } from "@mui/material";

function Editor() {

  return (
    <Box height="95vh">
      <TinymceEditor
        apiKey="my-api-key"
        onInit={(_evt, editor) => (editorRef.current = editor)}
        initialValue="<p>This is the initial content of the editor.</p>"
        init={init}
      />
    </Box>
  );
}

export default Editor;

And this is my test:

it("Should render <Editor /> with initial text", async function () {
  render(<Editor />);
  const editorTextarea = await screen.findByTestId(
    "tiny-react-editor",
    undefined,
    { timeout: 5000 }
  );

  const iframe = await waitFor(() => editorTextarea.querySelector("iframe"), {
    timeout: 10000,
  });

  console.log(iframe); //It logs null
});

So my test passes but the iframe is null and I cant get it. When I inspect the DOM on my browser, I see that there is a div with role="application" but can't load when I render my component through React Testing Library.

1
  • You should IMO mock "@tinymce/tinymce-react" module and don't bother with retesting it's implementation. In your mock you can expose the params you've passed and render it for test. Commented Dec 17, 2024 at 14:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.