score:0
we can use the themeProvider of styled-componnets to create a wrapper around the shallow
or mount
methods of enzyme, like below:
import { ThemeProvider } from 'styled-components';
const mountWithTheme = (children, theme={}, options = {}) => {
const wrapper = mount(<ThemeProvider theme={theme}>{children}</ThemeProvider>, options);
return wrapper.mount({});
};
test("header text is being passed through correctly", () => {
const wrapper = mountWithTheme(<Product items={data} headerText="Service" paginationSize="5" noItemMessage="No results found" />,
{ secondary: { blue: 'blue' } });
// we passed the theme we want as an argument to the mountWithTheme method
expect(wrapper.find(HeaderText).text()).toEqual("Service");
});
It will be better to pull out the utils mountWithTheme and ShallowWithTheme to a different file so that it can be reused, and also as a suggestion, you can create a mockTheme file which contains some default themes which can be passed to these wrappers instead of manually passing the theme each time we are consuming these utils.
More more information, visit below link: https://github.com/styled-components/styled-components/issues/1319
score:1
to get ride of add theme as props in your wrapper
<Product items={data} theme={{secondary: {blue: ''}}} headerText="Service" paginationSize="5" noItemMessage="No results found" />
or mock it
import { createSpy } from 'expect';
const theme = createSpy();
<Product items={data} theme={theme} headerText="Service" paginationSize="5" noItemMessage="No results found" />
Source: stackoverflow.com
Related Query
- How to test if a prop is rendered correctly in a Component using Jest and Enzyme in React
- How can I test if child React component was rendered based on a URL change using Jest and Enzyme
- How to test styles and media queries rendered by a React component with Jest and/or Enzyme
- How to test react functional component correctly using jest and enzyme?
- Test a component is rendered with React Router's render prop using Enzyme's shallow wrapper and Jest
- How to write a test case for a simple React component using Jest and Enzyme
- How to test if state of a component is changed by the function using jest and Enzyme in React TDD
- How to write test cases in enzyme and jest for a component using useDropZone?
- How to unit test useEffect cleanUp return function using Jest and Enzyme
- How to test conditional rendering of components using Jest and Enzyme
- How to test onClick props of a button using Jest and Enzyme
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- how to test OnSubmit in React using Jest and Enzyme
- How to test if a component is rendered or not by jest and enzyme?
- How to test onChange for ui-material TextField using Jest and Enzyme
- Trying to unit test a React component using Jest and Enzyme
- How to test redux saga using jest and enzyme
- How to mock component method using Jest and Enzyme
- How can I test a function of a function component that uses React.useState() using jest enzyme testing?
- How to test React component with children using jest and enzyme?
- how to test component parameters in constructor with Jest and Enzyme
- How to write test cases for a function using jest and enzyme
- How to test the anchor tag in react class component using jest and enzyme?
- how do I test toggle theme button using jest and enzyme
- How to test onClick() funct and useState hooks using jest and enzyme
- In React functional Component , test internal functional using jest and enzyme
- How to test the style inside the CSS class for React component using Jest and Enzyme?
- How to write test case coverage in Jest and Enzyme for indexOf used inside React Component
- How to test conditionally rendered string using React testing library and jest
- How to cover test cases for Svg react component using Jest + Enzyme
More Query from same tag
- React, writing handleInputChange function more elegant
- Assigining value to the Select tag's value property in React js
- React Router switch does not mount same component for different routes
- How can i target a specific item from a list of array on Hover in React
- When should I put state in parent?
- Cannot read property 'history' of undefined (useHistory hook of React Router 5)
- How to set a variable in a parent component from a child component
- Next.JS "Rendered more hooks than during the previous render"
- Mock axios create in test file. React, typescript
- Height depends on the text in React
- How to make 3 buttons show and hide content when click on them?
- Hide Layout on certain routes
- How can I pull various claims out of the access token?
- how to filter all columns of html table using react js?
- React handleOnSubmit() need to be clicked twice
- How do I render this data in react?
- React Formik : how to use custom onChange and onBlur
- Why is the content removed after page refresh?
- useEffect for componentDidMount dependency
- How to check if provided properties belongs to generics object
- How to map through an array of objects and add value to each other in React?
- How to extend a render DOM in react
- Typescript in React: Props type check is not working
- Can't find bundle.js
- Delete space between components
- React JS - Countdown timer using Moment.js
- Pivotting a material UI dialog in the center of the screen when it's height may vary
- Component not rendering with react-router-dom
- Is there a way to add a cancel button with an input field in Sweetalert, Reactjs
- Is it possible to make fields required on material-table