score:2
I guess it's about how react-dom
and jsdom
(which used by testing too) works.
(Please correct me or let me know if guessing is unacceptable! I will remove this. )
Button type return by
queryByText
is HtmlElmentreact
orreact-dom
is just minimize the task for dom manipulation, eventuallyjsdom
would still do the text changing and button removing works.Since the testing tool is using
jsdom
, I check theremoveChild
part of code, and believe they are following W3C specification. I assume in the level of dom manipulation,jsdom
would works like browsers. Thus I simulated your code like this:
const btn = document.getElementById('btn');
const btnContainer = document.getElementById('btnContainer');
btn.textContent = '1';
console.log('btn after click 1: ' + btn);
btn.textContent = '2';
console.log('btn after click 2: ' + btn);
btnContainer.removeChild(btn);
console.log('btn after removed: '+ btn);
<div id='btnContainer'><button id='btn'>0<button></div>
- As you can see, after removing from
div
, thebtn
variable is still[object HTMLButtonElement]
Maybe It's similar to usesplice
to an array which contained a defined object, the initial obj still remains the same?
const obj = { 'a': 1};
const arr = [obj];
arr[0]['a'] = 2;
arr.splice(0, -1);
console.log(obj)
Therefore, I think it's more about how garbage collection works in javascript.
In addition, maybe not to reuse the query result, and always query again in this situation, the result will be more expectable.
P.S. I found there is a method call toBeInTheDocument
!
Source: stackoverflow.com
Related Query
- How to test a className with the Jest and React testing library
- How to test material ui autocomplete with react testing library
- How to test Material UI Select Component with React Testing Library
- How to test components internal functions with React testing library
- How to test HTML content with React testing library
- Test input Search box with React testing library
- How to test Material-UI's Responsive UI (e.g. Hidden, Grid, Breakpoints) in React with Enzyme or React Testing Library
- How to test redux state update with react testing library and jest
- How to test `contenteditable` events with React Testing Library
- Unit test form submission with data using react testing library
- Having an issue while up and running unit test with react redux testing library
- How to test select option logic with React Testing Library
- Next seo test with react testing library
- How to test a function inside React functional component using jest.spyOn with React Testing Library
- Can't test mui accordion with react testing library
- How to Test Responsive React Components with React Testing Library
- Access and modify react context from jest test with react testing library
- How to test this component with react testing library - just a starter with this new testing library
- How to test CSS properties defined inside a class with react testing library
- How to Test Custom Hook with react testing library
- unit test custom hook with jest and react testing library
- An update to Provider inside a test was not wrapped in act(...) with Jest + React Testing Library
- Test with react testing library the document object
- How to Test Anchor Hrefs in an antd Menu with React Testing Library
- Test document listener with React Testing Library
- Create unit test with Jest and React testing library for helper which generate a domain url
- How to test Reach UI Menu Button with react testing library
- Test input without label with React Testing Library
- How to test react component with onClick function Jest/React testing library
- How to test a required input field with React Testing Library and Jest?
More Query from same tag
- Is it possible to type a React function component with TypeScript without using arrow functions?
- Delete a <div> onClick in React
- How to parse hashtags and mentions of a string with custom components
- Onclick won't bind value of variable at point of rendering
- How to create a proxy to access an external API?
- In JS/React, how to store array properties into new array to display new array values?
- useEffect show a warning if I call a function in throw props?
- next js make dynamic human readble urls
- Configuring antdesign using webpack
- Funtions with two parameter not working in React
- Webpack 5 - Uncaught ReferenceError: process is not defined
- Not able to transpile jsx to js using babel
- I'm using a useDispatch custom hook and getting this error in my test: Actions must be plain objects. Use custom middleware for async actions
- How to center a component in MUI and make it responsive?
- Lifecycle issue on axios call to an external API
- How can I use react-router to trigger state changes?
- Why is it not filling the useState whin i get the data?
- In a React/Jest unit test, how do you simulate an event prop being called by a mocked component?
- Query all documents created by a particular user
- State isn't updated on handler call from child to parent
- ReferenceError: React not defined in tests
- How to push multiple json (response ) in single array?
- Requiring React components in my html files
- Error: useLocation() may be used only in the context of a <Router> component
- Flow error - property 'contains' not found in EventTarget
- React Hooks - How to use a make a state dependant on another state?
- Reactjs how to manage multiple interactions between component
- React-select shows keyboard when is toggled
- Preventing rerenders when using object as state in useState
- In Redux, is there the concept of Data Provider?