GithubHelp home page GithubHelp logo

Comments (6)

mxstbr avatar mxstbr commented on May 19, 2024 1

Promote the component to its own module and do snapshot testing on it there.

This is definitely the way to go! You could also just partially export it if you don't want to add another file:

export const Title = // ...

export default class FullComponent extends React.Component { /* ... */ }

Then you can import both in your tests and test the separately:

import FullComponent, { Title } from '../FullComponent';

describe('<FullComponent />', () => { /* ...shallow render <FullComponent />... */ })

describe('<Title />', () => { /* ...shallow render <Titlte /> ... */ })

from jest-styled-components.

MicheleBertoli avatar MicheleBertoli commented on May 19, 2024

Hello Tyler, thanks for opening the issue.
I understand why this might be confusing but that's how shallow rendering works.
Shallow rendering renders the components only one level deep, and that's the reason why the Title is shown as styled.h1 and it doesn't have any class names.

I don't believe this is an issue or something to solve, but I'll be happy to discuss further in case you do. I'm going to close this but anyone should feel free to reopen it in case we want to address the problem.

from jest-styled-components.

tylerferguson avatar tylerferguson commented on May 19, 2024

That does make sense. I think it was a mistake in how I was viewing the Title component. I was seeing it as: <Title className="2rQth">Hello World</Title>, when it's actually more like: Title =(props) => <h1 className="2rQth">{props.children}</h1>. The className is not passed as a prop toTitle.

In that case, when it comes to testing examples like this (where we have two components that we would like to test, but we don't want to do a deep render because the contents have a lot of stuff in the tree that we are testing elsewhere), is there a decent way of snapshotting the styles? The options I can think of are:

  1. Promote the component to its own module and do snapshot testing on it there.
  2. Deep render with manual mocks on the components that you don't want to render.

Thanks for your help on this!

from jest-styled-components.

tylerferguson avatar tylerferguson commented on May 19, 2024

Perfect. Thanks for the advice @mxstbr. I'll get to it!

from jest-styled-components.

melounek avatar melounek commented on May 19, 2024

I fell into the same issue and finally I realized to use your second option of deep rendering with mocking (ignoring) the nested components defining empty functions.

jest.mock('../Inner', () => 'inner')

const snapshot = mount(<Wrapper />)

I documented it here, so I hope it will be helpful for some others... https://medium.com/@melounek/jest-snapshots-with-nested-components-c08ceef34108

from jest-styled-components.

MicheleBertoli avatar MicheleBertoli commented on May 19, 2024

Thank you very much, @melounek.

from jest-styled-components.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.