Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jest-emotion for preact #731

Merged
merged 1 commit into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/jest-emotion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"babel-cli": "^6.24.1",
"dtslint": "^0.3.0",
"npm-run-all": "^4.0.2",
"preact": "^8.2.9",
"preact-render-to-json": "^3.6.6",
"pretty-format": "^22.4.3",
"rimraf": "^2.6.1"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-emotion/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ function getClassNames(selectors, classes) {
return classes ? selectors.concat(classes.split(' ')) : selectors
}

function getClassNamesFromTestRenderer(selectors, node) {
const props = node.props
function getClassNamesFromTestRenderer(selectors, { props = {} }) {
return getClassNames(selectors, props.className || props.class)
}

Expand Down
21 changes: 21 additions & 0 deletions packages/jest-emotion/test/__snapshots__/preact.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jest-emotion with preact handles elements with no props 1`] = `"<div />"`;

exports[`jest-emotion with preact replaces class names and inserts styles into preact test component snapshots 1`] = `
".emotion-1 {
color: red;
}

.emotion-0 {
width: 100%;
}

<div
class=\\"emotion-1\\"
>
<svg
class=\\"emotion-0\\"
/>
</div>"
`;
44 changes: 44 additions & 0 deletions packages/jest-emotion/test/preact.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable react/no-unknown-property */
import { h } from 'preact'
import render from 'preact-render-to-json'
import prettyFormat from 'pretty-format'
import * as emotion from 'emotion'
import { createSerializer } from '../src'
const { ReactElement, ReactTestComponent, DOMElement } = prettyFormat.plugins

/** @jsx h */

describe('jest-emotion with preact', () => {
const emotionPlugin = createSerializer(emotion)

const divStyle = emotion.css`
color: red;
`
const svgStyle = emotion.css`
width: 100%;
`

it('replaces class names and inserts styles into preact test component snapshots', () => {
const tree = render(
<div class={divStyle}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is preact accepting class instead of className?

There is a linting problem found in this file:

Unknown property 'class' found, use 'className' instead react/no-unknown-property

If class is correct in preact you can just disable this rule for this file (with top file comment or smth)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed. And yes, preact takes all the html style instead of js style (class and for, for example)

<svg class={svgStyle} />
</div>
)

const output = prettyFormat(tree, {
plugins: [emotionPlugin, ReactElement, ReactTestComponent, DOMElement]
})

expect(output).toMatchSnapshot()
})

it('handles elements with no props', () => {
const tree = render(<div />)

const output = prettyFormat(tree, {
plugins: [emotionPlugin, ReactElement, ReactTestComponent, DOMElement]
})

expect(output).toMatchSnapshot()
})
})
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11241,6 +11241,10 @@ potrace@^2.1.1:
dependencies:
jimp "^0.2.24"

preact-render-to-json@^3.6.6:
version "3.6.6"
resolved "https://registry.yarnpkg.com/preact-render-to-json/-/preact-render-to-json-3.6.6.tgz#f67f48581912ac53fc9f4873bc6d7ce342f71c20"

preact@^8.2.9:
version "8.2.9"
resolved "https://registry.yarnpkg.com/preact/-/preact-8.2.9.tgz#813ba9dd45e5d97c5ea0d6c86d375b3be711cc40"
Expand Down