Skip to content

Commit

Permalink
feat(serializer): Use placeholder classnames (#12) (#13)
Browse files Browse the repository at this point in the history
Closes #11

This replaces the glamor generated classnames in the output with placeholder "glamor-*" classnames. This improves diffs as they are not polluted by the change in classnames.

BREAKING CHANGE: Your snapshots will all break and will need to be updated. That's all though 😄 and they'll be much easier to read!
  • Loading branch information
Kent C. Dodds authored Jul 23, 2017
1 parent ee8a9c1 commit eafaec0
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 104 deletions.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
"doc",
"test"
]
},
{
"login": "jhurley23",
"name": "jhurley23",
"avatar_url": "https://avatars2.githubusercontent.com/u/11878516?v=3",
"profile": "https://github.com/jhurley23",
"contributions": [
"code",
"test",
"doc"
]
}
]
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Jest utilities for Glamor and React
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -208,8 +208,8 @@ I'm unaware of other solutions. Please file a PR if you know of any!
Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars1.githubusercontent.com/u/1308971?v=3" width="100px;"/><br /><sub>Michele Bertoli</sub>](http://michele.berto.li)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli) [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli) [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli) | [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds) 🚇 [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds) | [<img src="https://avatars2.githubusercontent.com/u/11481355?v=3" width="100px;"/><br /><sub>Mitchell Hamilton</sub>](https://hamil.town)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton) [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton) [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton) |
| :---: | :---: | :---: |
| [<img src="https://avatars1.githubusercontent.com/u/1308971?v=3" width="100px;"/><br /><sub>Michele Bertoli</sub>](http://michele.berto.li)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli "Code") [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli "Documentation") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli "Tests") | [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds "Tests") | [<img src="https://avatars2.githubusercontent.com/u/11481355?v=3" width="100px;"/><br /><sub>Mitchell Hamilton</sub>](https://hamil.town)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton "Code") [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton "Documentation") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton "Tests") | [<img src="https://avatars2.githubusercontent.com/u/11878516?v=3" width="100px;"/><br /><sub>jhurley23</sub>](https://github.com/jhurley23)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=jhurley23 "Code") |
| :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
Expand Down
Binary file modified other/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 24 additions & 24 deletions src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,26 @@ exports[`4. general tests: pseudo states - {":hover":{"backgroundColor":"blue"}}
exports[`doesn't mess up stuff that does't have styles 1`] = `<div />`;

exports[`enzyme.mount 1`] = `
.css-1otybxc,
[data-css-1otybxc] {
.glamor-1,
[data-glamor-1] {
padding: 4em;
background: papayawhip;
}
.css-1tnuino,
[data-css-1tnuino] {
.glamor-0,
[data-glamor-0] {
font-size: 1.5em;
text-align: center;
color: palevioletred;
}
<Wrapper>
<section
className="css-1otybxc"
className="glamor-1"
>
<Title>
<h1
className="css-1tnuino"
className="glamor-0"
>
Hello World, this is my first glamor styled component!
</h1>
Expand All @@ -86,39 +86,39 @@ exports[`enzyme.mount 1`] = `
`;

exports[`enzyme.render 1`] = `
.css-1otybxc,
[data-css-1otybxc] {
.glamor-1,
[data-glamor-1] {
padding: 4em;
background: papayawhip;
}
.css-1tnuino,
[data-css-1tnuino] {
.glamor-0,
[data-glamor-0] {
font-size: 1.5em;
text-align: center;
color: palevioletred;
}
<section
class="css-1otybxc"
class="glamor-1"
>
<h1
class="css-1tnuino"
class="glamor-0"
>
Hello World, this is my first glamor styled component!
</h1>
</section>
`;

exports[`enzyme.shallow 1`] = `
.css-1otybxc,
[data-css-1otybxc] {
.glamor-0,
[data-glamor-0] {
padding: 4em;
background: papayawhip;
}
<section
className="css-1otybxc"
className="glamor-0"
>
<Title>
Hello World, this is my first glamor styled component!
Expand All @@ -127,40 +127,40 @@ exports[`enzyme.shallow 1`] = `
`;

exports[`react-test-renderer 1`] = `
.css-1otybxc,
[data-css-1otybxc] {
.glamor-1,
[data-glamor-1] {
padding: 4em;
background: papayawhip;
}
.css-1tnuino,
[data-css-1tnuino] {
.glamor-0,
[data-glamor-0] {
font-size: 1.5em;
text-align: center;
color: palevioletred;
}
<section
className="css-1otybxc"
className="glamor-1"
>
<h1
className="css-1tnuino"
className="glamor-0"
>
Hello World, this is my first glamor styled component!
</h1>
</section>
`;

exports[`works when the root element does not have styles 1`] = `
.css-1otybxc,
[data-css-1otybxc] {
.glamor-0,
[data-glamor-0] {
padding: 4em;
background: papayawhip;
}
<div>
<section
className="css-1otybxc"
className="glamor-0"
/>
</div>
`;
32 changes: 16 additions & 16 deletions src/__snapshots__/matcher.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ exports[`formats a message 1`] = `
+ Received
 
[37m- .css-1otybxc,[39m
[37m- [data-css-1otybxc] {[39m
[37m+ .css-i77z7r,[39m
[37m+ [data-css-i77z7r] {[39m
[32m- .css-1otybxc,[39m
[32m- [data-css-1otybxc] {[39m
[31m+ .css-i77z7r,[39m
[31m+ [data-css-i77z7r] {[39m
 padding: 4em;
- background: papayawhip;
+ background: blue;
 }
 
[37m- .css-1tnuino,[39m
[37m- [data-css-1tnuino] {[39m
[37m+ .css-1252hns,[39m
[37m+ [data-css-1252hns] {[39m
[32m- .css-1tnuino,[39m
[32m- [data-css-1tnuino] {[39m
[31m+ .css-1252hns,[39m
[31m+ [data-css-1252hns] {[39m
 font-size: 1.5em;
- text-align: center;
+ text-align: left;
 color: palevioletred;
 }
 
 <section
[37m- className=\\"some-other-class css-1otybxc\\"[39m
[37m+ className=\\"some-other-class css-i77z7r\\"[39m
[32m- className=\\"some-other-class css-1otybxc\\"[39m
[31m+ className=\\"some-other-class css-i77z7r\\"[39m
 >
 <h1
- className=\\"changed-class css-1tnuino\\"
Expand All @@ -43,18 +43,18 @@ exports[`formats a message with data- attributes 1`] = `
+ Received
 
[37m- .css-12gg9yz,[39m
[37m- [data-css-12gg9yz] {[39m
[32m- .css-12gg9yz,[39m
[32m- [data-css-12gg9yz] {[39m
- background-color: rebeccapurple;
[37m+ .css-1rdlmfe,[39m
[37m+ [data-css-1rdlmfe] {[39m
[31m+ .css-1rdlmfe,[39m
[31m+ [data-css-1rdlmfe] {[39m
+ background-color: rebeccapurples;
 margin: 2px;
 }
 
 <div
[37m- data-css-12gg9yz=\\"\\"[39m
[37m+ data-css-1rdlmfe=\\"\\"[39m
[32m- data-css-12gg9yz=\\"\\"[39m
[31m+ data-css-1rdlmfe=\\"\\"[39m
 />
 "
`;
39 changes: 1 addition & 38 deletions src/matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,11 @@ const isAddition = line => /^\+/.test(line)

const isDeletion = line => /^-/.test(line)

const removeGlamorClassNames = line =>
line.replace(/css-.+?( |")/, '').slice(1).trim()
const isClassNameAttribute = (
line,
previous = '',
/* istanbul ignore next */ next = '',
) => {
const containsGlamorClassName = /class(Name)?=".*css-.*"/.test(line)
if (containsGlamorClassName) {
// just because the line contains a glamor class name doesn't mean that
// it doesn't also have some other change, so we'll remove everything
// except the glamor class name and see if it's the same as the previous
// or the next line. It's not really perfect, but it's less likely that
// we'll show something in white that shouldn't be :)
const lineRemoved = removeGlamorClassNames(line)
const previousRemoved = removeGlamorClassNames(previous)
const nextRemoved = removeGlamorClassNames(next)
return lineRemoved === previousRemoved || lineRemoved === nextRemoved
}
return containsGlamorClassName
}
const isDataAttribute = line => /data-css-.*/.test(line)
const isClassNameSelector = line => /\.css-.*,/.test(line)
const isDataSelector = line => /\[data-css-.*\] {/.test(line)

const isClassName = (line, previous, next) =>
(isAddition(line) || isDeletion(line)) &&
(isClassNameAttribute(line, previous, next) ||
isDataAttribute(line) ||
isClassNameSelector(line) ||
isDataSelector(line))

const colorize = message => {
const messageLines = message.split('\n')

return messageLines
.map((line, index) => {
const previous = messageLines[index - 1]
const next = messageLines[index + 1]
if (isClassName(line, previous, next)) {
return chalk.white(line)
}
.map(line => {
if (isAddition(line)) {
return chalk.red(line)
}
Expand Down
15 changes: 15 additions & 0 deletions src/replace-class-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const replaceClassNames = (selectors, styles, code) => {
let index = 0
return selectors.reduce((acc, className) => {
if (className.indexOf('.css-') === 0) {
const escapedRegex = new RegExp(
className.replace('.', '').replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'),
'g',
)
return acc.replace(escapedRegex, `glamor-${index++}`)
}
return acc
}, `${styles}\n\n${code}`)
}

module.exports = {replaceClassNames}
98 changes: 98 additions & 0 deletions src/replace-class-names.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {replaceClassNames} from './replace-class-names'

test('Replaces a single class', () => {
const selectors = ['.css-12345']
const styles = `
.css-12345,
[data-css-12345] {
font-size: 1.5em;
text-align: center;
color: palevioletred;
}
`
const code = `
<h1
className="changed-class css-12345"
>
Hello World, this is my first glamor styled component!
</h1>
`

expect(replaceClassNames(selectors, styles, code)).toMatch(/(glamor-0)/)
expect(replaceClassNames(selectors, styles, code)).not.toMatch(/(css-12345)/)
})

test('Replaces multiple glamor classes', () => {
const selectors = ['.css-12345', '.css-67890']
const styles = `
.css-12345,
[data-css-12345] {
font-size: 1.5em;
text-align: center;
color: palevioletred;
}
.css-67890,
[data-css-67890] {
font-size: 1.5em;
text-align: center;
color: palevioletred;
}
`
const code = `
<section
className="some-other-class css-12345"
>
<h1
className="changed-class css-67890"
>
Hello World, this is my first glamor styled component!
</h1>
</section>
`

expect(replaceClassNames(selectors, styles, code)).toMatch(/(glamor-0)/)
expect(replaceClassNames(selectors, styles, code)).toMatch(/(glamor-1)/)
})

test('does not replace non-glamor classes', () => {
const selectors = ['.p-4em']
const styles = `
.p-4em,
[data-p-4em] {
padding: 4em;
}
`
const code = `
<section
className="p-4em"
>
Hello World
</section>
`

expect(replaceClassNames(selectors, styles, code)).not.toMatch(/(glamor-0)/)
expect(replaceClassNames(selectors, styles, code)).toMatch(/(p-4em)/)
})

test('only replaces classes beginning with "css-"', () => {
const selectors = ['.not-glamor-css-1234']
const styles = `
.not-glamor-css-1234,
[data-not-glamor-css-1234] {
padding: 4em;
}
`
const code = `
<section
className="not-glamor-css-1234"
>
Hello World
</section>
`

expect(replaceClassNames(selectors, styles, code)).not.toMatch(/(glamor-0)/)
expect(replaceClassNames(selectors, styles, code)).toMatch(
/(not-glamor-css-1234)/,
)
})
Loading

0 comments on commit eafaec0

Please sign in to comment.