Say you're testing for existence via selector as in your example and need to use a method that returns
ShallowWrapper, then yes you need to use
.length. But you don't necessarily need separate statements for it:
const wrapper = shallow(<MYReactComponent />); expect(wrapper.find('some-css-selector').length).to.equal(1);
For that matter you could consolidate the whole thing in a case like this if you want:
expect( shallow(<MYReactComponent />) .find('some-css-selector').length ).to.equal(1);
If you're testing based on component type / props then you may be able to use
contains(), which returns boolean:
const wrapper = shallow(<MYReactComponent />); expect(wrapper.contains( <div class="something"> <span>content</span> </div> )).to.be.true;
Can skip find by directly using
Also works with other React Components nicely
import SomeOtherReactElement from 'path/to/SomeOtherReactElement'; ... ... expect(wrapper.exists(SomeOtherReactElement)).to.equal(true)
- What is the simplest way of testing that a React Component contains a html node with enzyme?
- What is the best way to render a search bar component that takes in a props in another component using react v6
- What is the correct way to type a React component class that has no props?
- How to test button that on click renders html element within the same component in React testing library, Jest?
- What is the correct way of adding a dependency to react in your package.json for a react component
- Is there a right way to pass data into a React component from the HTML page?
- what is the proper way to publish a React component module in NPM without external dependencies inside?
- What is the simplest way to share react components between projects?
- What is the 'proper' way to update a react component after an interval with hooks?
- How do I find the React DOM node that represents its actual html element?
- What is the most comfortable/conventional way to modify a specific item in an array, in a React component state?
- What is the simplest way to pass state while using React Router?
- What is the proper way to handle prop-types for a component that uses object destructing and object rest...spread to collect props
- What is the most effective/conventional way to remove an item from a React component state?
- Add/inject React component into pure HTML that came from the back-end
- What is the preferred way to pass data from a Rails view to a react component in Rails 6?
- What is the best way to create component library for both React and Preact?
- Error when testing a React Parent that contains a Child React Redux Component using Enzyme Mount
- React Testing Library: How to test a component that requires the useState hook from its parent?
- What is the idiomatic way to create styles for a Reason-React component that depend on props?
- What is the correct way to call updateCachedData on a click event in a component that uses the RTKQ query?
- What is the best way to test methods that traverse the react components
- Is there a way in React to render a component into HTML that is not its initial state?
- If I render text that includes HTML tags in my React app, is there a way to display the HTML?
- What is the correct way to pass an argument to parent component in React from onClick event handler
- What is the best way to connect React js app with node js server?
- Guess the number with React - what is the proper way of using state in that case
- What is the correct way to setup an independent Route that is not a part of App.js in React Router Dom?
- What is the simplest way to have form input set function variable in React JS?
- What are the separations of concerns for testing a component that consumes additional components
More Query from same tag
- history.push on form submit
- Delete item from MongoDB using React, Redux and Express
- How to execute a function only after updating the state of two variables?
- React Router Dom Relative Path
- Update language using react-intl and mobx state tree
- React - Trying to render property of object, though it stays "undefined"
- Why does my useEffect react function run when the page loads although I am giving it a second value array
- Check if my React array contains value if yes return certain component
- How can i write "setTimeOut" function in react hooks?
- React-slick: how sync 3 sliders
- React Webpack Dev Server main.js not found
- Modelling Dynamic Forms as React Component
- Infinite loop in ComponentDidUpdate
- Render multiple independent redux-form
- Note able to sort data in ascending/descending based on key of object in angular /typescript
- Encountered two children with the same key Error
- Assign namespaces to src library files
- How to change the styling of a selected row in MUI5?
- ES lint not working after trying to install it to MERN stack project
- Reducer not receiving action in redux or trouble with the action being called
- React - Typescript interfaces - errors when get props from union types
- Whats the best way to add an SVG to ReactJS
- sort immutable js data based on the two properties
- Using leaflet and need to update component. How can I update the values in my constructor?
- Image and text side by side in div, want to shrink image on resize
- CORS issue - Response to preflight request doesn't pass access control check:
- typescript string interpolation with lodash
- How can I prevent the MaterialUI Switch from animating?
- How to use DOMRect with useEffect in React?
- ReactJS: How can I play an html5 video with an onClick event correctly with react?