Skip to content

Instantly share code, notes, and snippets.

@MicheleBertoli
Created September 8, 2018 10:40
Show Gist options
  • Save MicheleBertoli/ea08aa9979e873666f3e3276c2f9c2ea to your computer and use it in GitHub Desktop.
Save MicheleBertoli/ea08aa9979e873666f3e3276c2f9c2ea to your computer and use it in GitHub Desktop.
Styled Component v4
import React from 'react'
import styled from 'styled-components'
import renderer from 'react-test-renderer'
import { shallow, mount } from 'enzyme'
import { render } from 'react-testing-library'
const Button = styled.button`
color: red;
`
test('it works', () => {
expect(renderer.create(<Button />).toJSON()).toMatchSnapshot(
'react-test-renderer'
)
expect(shallow(<Button />)).toMatchSnapshot('shallow')
expect(mount(<Button />)).toMatchSnapshot('mount')
expect(render(<Button />).container.firstChild).toMatchSnapshot(
'react-testing-library'
)
})
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`it works: mount 1`] = `
<ForwardRef>
<BaseStyledComponent
forwardedClass={
Object {
"$$typeof": Symbol(react.forward_ref),
"attrs": undefined,
"componentStyle": ComponentStyle {
"componentId": "sc-bdVaJa",
"isStatic": true,
"lastClassName": "rOCEJ",
"rules": Array [
"
color: red;
",
],
},
"displayName": "styled.button",
"render": [Function],
"styledComponentId": "sc-bdVaJa",
"target": "button",
"warnTooManyClasses": [Function],
"withComponent": [Function],
}
}
forwardedRef={null}
>
<button
className="sc-bdVaJa rOCEJ"
/>
</BaseStyledComponent>
</ForwardRef>
`;
exports[`it works: react-test-renderer 1`] = `
<button
className="sc-bdVaJa rOCEJ"
/>
`;
exports[`it works: react-testing-library 1`] = `
<button
class="sc-bdVaJa rOCEJ"
/>
`;
exports[`it works: shallow 1`] = `
<BaseStyledComponent
forwardedClass={
Object {
"$$typeof": Symbol(react.forward_ref),
"attrs": undefined,
"componentStyle": ComponentStyle {
"componentId": "sc-bdVaJa",
"isStatic": true,
"lastClassName": "rOCEJ",
"rules": Array [
"
color: red;
",
],
},
"displayName": "styled.button",
"render": [Function],
"styledComponentId": "sc-bdVaJa",
"target": "button",
"warnTooManyClasses": [Function],
"withComponent": [Function],
}
}
forwardedRef={null}
/>
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment