I am trying to write tests for one of our rich text components which was implemented with slate js editor in react js. So when writing tests, I am retrieveing the element div[contenteditable='true'], but not able to simulate events like change, blur, focus. The handlers attached to editor component are not getting called. I tried multiple combinations, but no luck. Can someone please help on this? Is it possible to simulate events for contenteditable element using testing library (contenteditable is implemented using slatejs)?
How to test `contenteditable` events with React Testing Library
2.7k views Asked by Rekha At
1
There are 1 answers
Related Questions in REACTJS
- ussd reader in Recket Native module
- Teams tab application returns SSO error in mobile Outlook
- Github Pages Deployment deploys a blank page
- Is there any way to glow this bulb image like a real light bulb
- Optimize LCP ReactJs
- Page in React only renders elements after refreshing
- Unable to Post Form Data to MongoDB because of picturepath
- MERN Stack App - User Avatar Upload - 500 Error After Deployment on Render
- Hooks are not supported inside an async component error in nextjs project using useQuery
- How to change the Font Weight of a SelectValue component in React when a SelectItem is selected?
- On the server side, it returns undefined but on the client side, logs the values no problem
- Multilevel dropdown with checkboxes in Select component
- TypeScript Error only on big type only when assigned to a variable
- Deployment through app engine, cloud sql database, problem connecting with server code, doesn't connect
- Data is not filtering in props. Showing passdata.map is not a function
Related Questions in CONTENTEDITABLE
- contentEditable - how to force to write after tag, not inside
- append react custom component to div with contentEditable
- How to get caret position on Twitter editable div?
- Why does contenteditable="false" not work?
- Configure HTML element to behave like text character
- Insert new line in contenteditable on Ctrl+Enter
- How to set Caretposition via mouse click in content editable within a draggable in Firefox
- getSelection().collapseToEnd() does not work
- CkEditor5 - Making div inside UIElement as contenteditable is not working
- Transform text to html based on regex and show it in input or ContentEditable
- React/Next js contentEditable Security with onBlur
- Issue with getBoundingClientRect in contenteditable div when caret is inside an empty child div
- How can I fix this content editable section in a Next app where content is RTL though set to LTR?
- Simulating Keypress in Div Element for Instagram DMs with JavaScript
- How to get content from WebView in jetpack compose
Related Questions in REACT-TESTING-LIBRARY
- react test with useEffect, axios and useParams
- How to use React-Testing-Library (RTL) to unit test a component with internal hook that uses ajax and state?
- `UserEvent` doesn't fire on 1 element from getAll* in vitest & testing-library/react
- How to test openFileInNewWindow behaviour and pass the sonarqube
- How to write jest unit test for NextJs layout which includes server component
- What am I doing wrong (testing Picker component in React Native app)?
- Test redux-toolkit component with custom renderWithProviders failing
- React GraphQL mutation is not leaving the loading state
- How to test conditional rendering component with useQuery result data?
- Find image after onLoad event with react-testing-library
- React Context not getting cleared after running each test in Jest
- MUI Autocomplete tests not working properly
- React testing Library form validation onChange not working as expected
- Delaying API response in Jest
- Not able to dispatch action in Jest
Related Questions in SLATEJS
- Slate.js - How to get Absolute Text Selection
- Slate JS not support Mobile Keypad voice support feature
- Side by side editing of richtext content in a webapp with quilljs or slatejs
- SlateJS image styling
- Can't pass selection in Slate.js (React) as single child
- How to automatically set focus to a SlateJS editor inside a react-hook-form
- how to limit text length in a Slate editor?
- How to migrate slate json text to tiptap based prosemirror format? Are there any converters/bindings for this?
- How to make SlateJS editor look like a MUI TextField?
- How to goto next line when focus the Image block?
- Manipulating text in a rich text editor using chrome extension
- SlateJS: All void elements are deleted instead of the selected one
- Create a temporary visual effect when insertNode on on react-slate
- How to align text in Slate.js and React?
- TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Node'
Related Questions in TESTING-LIBRARY
- Jest spyOn does not mock external function used in source file
- `Cannot use 'in' operator to search for '_leaflet_id' in null` in testing
- How to instantiate Ionic element with test-library
- Svelte Kit using Testing Library not recognising href value
- Testing library `within` return type in typescript
- How to test addEventListener with jest and testing library?
- Expect.toBeEqual is failing when an attribute is a function
- Why user-event click() promise resolves only by timeout when injectGlobals: false in Jest config?
- Flaky unit tests (React Testing Library) in CI/CD (teamcity)
- Vitest and custom hook that utilizes useNavigate() & useParams() in React
- Unable to trigger click/onChange when interaction-testing a MUI-RadioButtonGroup
- Invalid Chai property in Vitest
- How can I effectively test SvelteKit components, including monitoring reactive properties and props passed to child components?
- How to test if the rows are filtered in a table with testing library
- replace an API during tests in the Jest environment for React Native for axios AxiosInstance
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Like you've discovered,
contenteditableisn't supported by JSDOM. React Testing Library (RTL) is built on top of JSDOM, so it's not possible to test the Slate editor properly with RTL until JSDOM implements support forcontenteditable.Use a browser automation library together with Testing Library
Your options are then to use a tool that creates a real browser context. Testing Library have integrations with many tools that do exactly that: TestCafe, Cypress, Nightwatch, Puppeteer.
You can also use the above tools on their own, without Testing Library.
I've solved this using Puppeteer, and there are two approaches:
localhost:3000page.setContent(htmlString)(1) is the most common, and you'll find many guides for this since it's a common approach for end-to-end testing (google search).
(2) is a little trickier because you will have to transform and bundle your source for each test, and then inject it as HTML into the browser page. I prefer this approach because the testing experience is much more similar to using RTL. I've created a repository with an example of this setup with the Slate editor here: https://github.com/marcusstenbeck/puppeteer-react-testing-template/