question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing styled component with enzyme

See original GitHub issue

I got theme defined in the app and the child component contains style like color: ${props => props.theme.options.color};

if i use Enzyme to mount the parent component, it cause TypeError: Cannot read property of undefined

Is there any solution for this issue, i try to setContext(__styled-component__) in the test but it doesn’t work.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:33 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
three60fivecommented, Jun 9, 2017

When I use styled-components to make “building block” components defined in the same file as the “assembled” component, I am having a really difficult time targeting them in my tests using enzyme’s .find() method.

For example, with this styled-component:

import ImportedComponent from './ImportedComponent';
const Demo = styled.div`
`;
const Label = styled.span`
`;
const Thing = () =>
  <Demo>
    <ImportedComponent />
    <Label />
    <Label /> 
    <Label />
  </Demo>;

If I use an imported styled-component like <ImportedComponent>, I can easily find it like with enzyme like:

describe('<Thing />', () => {
  it('should contain an ImportedComponent', () => {
    const wrapper = shallow(<Thing />);
    const importedComponent = wrapper.find('ImportedComponent');
    expect(importedComponent).to.have.length(1);
  });
});

But I haven’t found an elegant way to find the “building block” components like <Label /> because they can’t be found by the component name as they appear more generically like “<styled.span>”. Is there a way for these “building block” components to appear with their component name like they are when imported? I don’t want to resort to targeting them by html or css as that is more brittle.

5reactions
chuanxiecommented, Mar 29, 2017

created a workaround but it might not be the best solution.

const CHANNEL = '__styled-components__';
const broadcast = createBroadcast(themes.dark);

const nodeWithThemeProp = node => {
  return React.cloneElement(node, { [CHANNEL]: broadcast.subscribe });
};

export function mountWithTheme(node, { context, childContextTypes } = {}) {
  return mount(
    nodeWithThemeProp(node),
    {
      context: Object.assign({}, context, {[CHANNEL]: broadcast.subscribe}),
      childContextTypes: Object.assign({}, {[CHANNEL]: createBroadcast(themes.dark).publish}, childContextTypes)
    }
  );
}

now i can get the wrapper component’s state() by using: const wrapper = mountWithTheme(<Component {...props} />);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test styled component with Jest-Enzyme - Stack Overflow
The test runner is Jest and I use the testing utilities library Enzyme. reactjs · unit-testing · jestjs · enzyme · styled-components ·...
Read more >
Testing React Styled Components with Jest & Enzyme | Neon
We have covered how to test the expected functionality of components by using the jest-styled-components library we installed earlier, we can ...
Read more >
Targeting styled components with enzyme's .find() #896 - GitHub
If I use an imported styled-component like <ImportedComponent> , I can easily find it like with enzyme like: describe('<Thing />', () => {...
Read more >
Test styled components in React efficiently using 'displayName'
This means that we are traversing the component tree using the actual component, rather than just the name(which is one of the benefits...
Read more >
Unit Testing Styled Components - Medium
Unit testing React components isn't always easy. In this short article i'll show some tricks to make our lives a little easier using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found