score:5
Why are you calling console.log inside of shallow
? I've never seen this before...
Try to wrap your <App />
component in <Route />
, and search for some tag inside it.
For example. Assuming you have this simple component:
<App>
<h1>Hi there!</h1>
</App>
Test can look like that:
describe('<App />', () => {
it('renders a static text', () => {
const wrapper = shallow(
<MemoryRouter initialEntries={['/']} initialIndex={0}>
<Route path="/" render={() => <App />} />
</MemoryRouter>
);
console.log(wrapper.find(App).html());
});
});
UPDATE It happens that react-boilerplate adds way too much wrappers and dependencies. So for successfull test we need to overcome them in different ways...
This is how I got it done(full content of my react-boilerplate/app/tests/simple.test.js
):
import React from 'react';
import { mount } from 'enzyme';
import { MemoryRouter, Route, browserHistory } from 'react-router-dom';
import { IntlProvider } from 'react-intl';
import { Provider } from 'react-redux';
import configureStore from '../configureStore';
import App from '../containers/App';
import { HomePage } from '../containers/HomePage';
describe('test of initial load', () => {
it('test', () => {
const store = configureStore({}, browserHistory);
const wrapper = mount(
<MemoryRouter initialEntries={['/']} initialIndex={0}>
<IntlProvider locale="en"> // to add Intl support
<Provider store={store} > // to provide store for numeric connected components
<Route path="/" render={() => <App />} />
</Provider>
</IntlProvider>
</MemoryRouter>
);
process.nextTick(() => { // to wait for loadible component will be imported
console.log(wrapper.find(HomePage).html());
});
});
});
I think that this is rather integration and not unit test...
Output of console.log
:
console.log app/tests/simple.test.js:24
<article><!-- react-empty: 38 --><div><section class="sc-bxivhb jwizic"><h2 class="H2__H2-dhGylN kpbCLA"><span>Start your next react project in seconds</span></h2><p><span>A highly scalable, offline-first foundation with the best DX and a focus on performance and best practices</span></p></section><section class="Section__Section-jEbZoY eivjmS"><h2 class="H2__H2-dhGylN kpbCLA"><span>Try me!</span></h2><form class="Form__Form-PASBS cEpjmP"><label for="username"><span>Show Github repositories by</span><span class="AtPrefix__AtPrefix-ivHByA dZcxpA"><span>@</span></span><input type="text" class="Input__Input-ixjKAz jCPlXI" id="username" placeholder="mxstbr" value=""></label></form><!-- react-empty: 54 --></section></div></article>
score:-1
Super simple way to do this without needing all the <MemoryRouter>
or <Route>
tags
In your App component, add the export
on to your class.
export class App extends Component {
render() {
return (
<div>Hi, there</div>
);
}
}
Then in your spec.js
file, simply import your component to test as follows:
import React from 'react';
import {shallow} from 'enzyme';
import { App } from './App';
describe ('App component', () => {
it('renders App', () => {
const wrapper = shallow(<App optInValue={true} authenticated={true}/>);
expect(wrapper.length).toEqual(1);
});
});
Source: stackoverflow.com
Related Query
- Testing react router v4 with jest enzyme
- Testing React Router with Jest and Enzyme
- Testing debounced function in React component with Jest and Enzyme
- testing click behavior on React Router Links with Enzyme
- React testing with Jest and Enzyme (in Symfony) got "Syntax Error: Unexpected token import"
- React testing state of component with Jest and Enzyme
- Getting started testing React components with Enzyme and Jest
- Testing components that contain react router Link with Enzyme
- Testing React component with Enzyme Jest finding HTML element
- testing custom react methods with jest and enzyme
- Testing a React Hooks component with Jest / Enzyme & Axios
- Testing react app with jest and enzyme token problem
- React testing with Jest and Enzyme @react-google-maps/api returns TypeError: Cannot read property 'maps' of undefined
- Testing a React component with Jest / Enzyme & Axios
- Testing component function in React with Enzyme and Jest
- Unable to get props while testing with jest Enzyme React
- Faced error during testing with Jest and Enzyme in React
- How to test a className with the Jest and React testing library
- How to mock React component methods with jest and enzyme
- Testing React Functional Component with Hooks using Jest
- How to mock history.push with the new React Router Hooks using Jest
- Nested components testing with Enzyme inside of React & Redux
- Accessing the State of a Functional Component with React Hooks when Testing with Enzyme
- Testing React portals with enzyme
- How to mock out sub-components when unit testing a React component with Jest
- Testing with React's Jest and Enzyme when simulated clicks call a function that calls a promise
- Testing with React's Jest and Enzyme when async componentDidMount
- Jest testing react component with react-intersection-observer
- Testing functions inside stateless React component with Enzyme
- React Enzyme Jest test component with className
More Query from same tag
- Testing className with materialUI
- React: load images in advance
- Can I type React Children using Typescript?
- How to update state that has dependency on other state with React Hooks
- Unable to change y-axis value dynamically based on data for Stacked bar chart in reactjs
- Changing two states sequentially throws an error
- React - Display Chart when button is clicked
- how to use context api in react in counter example?
- React is not fetching env variables
- react-native-swiper does not show dynamic images
- How to get the value of input tag onSubmit without using onChange in React js/Typescript?
- Force Webpack to use ES6 syntax in react-app bundle
- Can't fetch express API from React frontend
- react-redux store mapping issue between components
- Saving Datepicker input to state is causing error
- Dashed Circular Bar with Percentage
- React basics - setState() not updating counter in the state
- How do I get my JavaScript file to execute at the end of my component?
- React Redux - Loading state too slow - how to solve it
- Auth0 authentication with Cypress
- react_dom_client__WEBPACK_IMPORTED_MODULE_1__.render is not a function show in the console of localhost:3000
- Why auth0's parseHash return undefined?
- Cannot empty array in ReastJS useEffect hook
- How to change the color of radio button with font awesome icon in react, styled components
- ReactRouter work not correct, only change url not view
- Extracting styles in React + material-ui
- react using a dynamic import name and path
- ReactJS - Route works but doesn't display the page
- How to post data with asp.net core/react
- How I can get an index value of a array inside a another array