Skip to content

Commit 878afe0

Browse files
author
Robin Frischmann
committed
add tests & 1.0.2 release
1 parent bfd90cc commit 878afe0

9 files changed

+286
-18
lines changed

.babelrc

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
{
22
"presets": [
3-
"react",
3+
[
4+
"es2015",
5+
{
6+
"modules": false
7+
}
8+
],
49
"stage-0",
5-
"es2015"
6-
]
7-
}
10+
"react"
11+
],
12+
"plugins": [
13+
"transform-class-properties"
14+
],
15+
"env": {
16+
"commonjs": {
17+
"plugins": [
18+
"transform-es2015-modules-commonjs"
19+
]
20+
}
21+
}
22+
}

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ script:
55
- npm run check
66
addons:
77
code_climate:
8-
repo_token: 7b36993e94e8f045924171a92cb833c7097d5a49c98e1905a13964792c25192e
8+
repo_token: b9b1d75d35a4a0abd6cefa9934564d22166b2ff346af0730d20dc70c92af5da8
99
before_script:
1010
- npm run setup
1111
after_script:

package.json

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-css-component",
3-
"version": "1.0.1",
4-
"description": "Inject CSS with a Component",
3+
"version": "1.0.2",
4+
"description": "Injecting CSS via React Components",
55
"main": "lib/index.js",
66
"module": "es/index.js",
77
"jsnext:main": "es/index.js",
@@ -21,9 +21,9 @@
2121
"scoped-style"
2222
],
2323
"scripts": {
24-
"build": "babel src/ --out-dir lib --ignore __tests__ && BABEL_ENV=commonjs babel src/ --out-dir lib --ignore __tests__",
24+
"build": "babel src -d es --ignore __tests__ && BABEL_ENV=commonjs babel src -d lib --ignore __tests__",
2525
"clean": "rimraf es lib coverage",
26-
"check": "yarn format && yarn lint",
26+
"check": "yarn format && yarn lint && yarn test",
2727
"format": "prettier --write \"src/**/*.js\"",
2828
"lint": "eslint src/**/*.js",
2929
"release": "git pull --rebase && yarn setup && yarn run check && npm publish",
@@ -32,6 +32,9 @@
3232
"watch": "yarn test -- --watch",
3333
"setup": "yarn run clean && yarn build"
3434
},
35+
"jest": {
36+
"testEnvironment": "node"
37+
},
3538
"repository": "https://github.com/rofrischmann/react-css-component.git",
3639
"author": "Robin Frischmann <[email protected]>",
3740
"license": "MIT",
@@ -40,9 +43,11 @@
4043
"babel-core": "^6.26.0",
4144
"babel-eslint": "^8.2.2",
4245
"babel-jest": "^22.4.1",
43-
"babel-preset-es2015": "^6.24.1",
44-
"babel-preset-react": "^6.24.1",
45-
"babel-preset-stage-0": "^6.24.1",
46+
"babel-plugin-transform-class-properties": "^6.9.1",
47+
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
48+
"babel-preset-es2015": "^6.6.0",
49+
"babel-preset-react": "^6.5.0",
50+
"babel-preset-stage-0": "^6.5.0",
4651
"codeclimate-test-reporter": "^0.3.1",
4752
"cross-env": "^5.1.3",
4853
"eslint": "^4.18.1",
@@ -52,9 +57,11 @@
5257
"eslint-plugin-jsx-a11y": "^6.0.3",
5358
"eslint-plugin-react": "^7.7.0",
5459
"jest": "^22.4.2",
60+
"jsdom": "^11.6.2",
5561
"prettier": "^1.11.1",
5662
"prop-types": "^15.6.1",
5763
"react": "^16.2.0",
64+
"react-dom": "^16.2.0",
5865
"rimraf": "^2.6.2"
5966
},
6067
"peerDependencies": {

src/components/StyleCacheProvider.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default class StyleCacheProvider extends Component {
1919
return this.props.children
2020
}
2121
}
22+
2223
StyleCacheProvider.childContextTypes = {
2324
[CONTEXT_NAMESPACE]: PropTypes.object,
2425
}

src/components/UniversalStyle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ export default class UniversalStyle extends Component {
5454
}
5555
}
5656

57-
Style.contextTypes = {
57+
UniversalStyle.contextTypes = {
5858
[CONTEXT_NAMESPACE]: PropTypes.object,
5959
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
import { render } from 'react-dom'
3+
import { renderToString } from 'react-dom/server'
4+
import { JSDOM } from 'jsdom'
5+
6+
import Style from '../ClientStyle'
7+
8+
afterEach(() => {
9+
global.window = undefined
10+
global.document = undefined
11+
})
12+
13+
describe('ClientStyle Component', () => {
14+
it('should render nothing, but apply a style node to the document.head', () => {
15+
const { window } = new JSDOM('<html><body></body></html>')
16+
17+
global.window = window
18+
global.document = window.document
19+
20+
const div = document.createElement('div')
21+
const css = '.class1 { color: red } .class2 { color: blue }'
22+
23+
render(<Style css={css} />, div)
24+
25+
expect(
26+
document.getElementById('__react_css_component_id-0').textContent
27+
).toBe(css)
28+
expect(div.innerHTML).toBe('')
29+
})
30+
31+
it('should not throw on the server', () => {
32+
const css = '.class1 { color: red } .class2 { color: blue }'
33+
34+
const markup = renderToString(<Style css={css} />)
35+
expect(markup).toBe('')
36+
})
37+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import React, { Component } from 'react'
2+
import { render } from 'react-dom'
3+
import { renderToString } from 'react-dom/server'
4+
import { JSDOM } from 'jsdom'
5+
6+
import Style from '../UniversalStyle'
7+
8+
afterEach(() => {
9+
global.window = undefined
10+
global.document = undefined
11+
})
12+
13+
describe('UniversalComponent Component', () => {
14+
it('should render a style element on the server', () => {
15+
const css = '.class1 { color: red } .class2 { color: blue }'
16+
17+
const markup = renderToString(<Style css={css} />)
18+
expect(markup).toMatchSnapshot()
19+
})
20+
21+
it('should render a style element on the client', () => {
22+
const { window } = new JSDOM('<html><body></body></html>')
23+
24+
global.window = window
25+
global.document = window.document
26+
27+
const div = document.createElement('div')
28+
const css = '.class1 { color: red } .class2 { color: blue }'
29+
30+
render(<Style css={css} />, div)
31+
32+
expect(div.innerHTML).toMatchSnapshot()
33+
expect(document.head.childNodes.length).toBe(0)
34+
})
35+
36+
it('should move the style to the document.head if the component unmounts', () => {
37+
const { window } = new JSDOM('<html><body></body></html>')
38+
39+
global.window = window
40+
global.document = window.document
41+
42+
const div = document.createElement('div')
43+
const css = '.class1 { color: red } .class2 { color: blue }'
44+
45+
class Wrapper extends Component {
46+
constructor(props, context) {
47+
super(props, context)
48+
49+
this.state = {
50+
isVisible: true,
51+
}
52+
53+
window.hide = () => {
54+
this.setState({ isVisible: false })
55+
}
56+
}
57+
58+
render() {
59+
if (this.state.isVisible) {
60+
return <Style css={css} />
61+
}
62+
63+
return null
64+
}
65+
}
66+
67+
render(<Wrapper />, div)
68+
69+
expect(div.innerHTML).toMatchSnapshot()
70+
expect(document.head.childNodes.length).toBe(0)
71+
72+
window.hide()
73+
74+
expect(div.innerHTML).toBe('')
75+
expect(document.head.childNodes.length).toBe(1)
76+
expect(
77+
document.getElementById('__react_css_component_id-0').textContent
78+
).toBe(css)
79+
})
80+
81+
it('should move each different style to the document.head if the component unmounts', () => {
82+
const { window } = new JSDOM('<html><body></body></html>')
83+
84+
global.window = window
85+
global.document = window.document
86+
87+
const div = document.createElement('div')
88+
const css1 = '.class1 { color: red } .class2 { color: blue }'
89+
const css2 = '.class3 { color: green }'
90+
91+
class Wrapper extends Component {
92+
constructor(props, context) {
93+
super(props, context)
94+
95+
this.state = {
96+
isVisible: true,
97+
}
98+
99+
window.hide = () => {
100+
this.setState({ isVisible: false })
101+
}
102+
}
103+
104+
render() {
105+
if (this.state.isVisible) {
106+
return [
107+
<Style key="style-1" css={css1} />,
108+
<Style key="style-2" css={css2} />,
109+
]
110+
}
111+
112+
return null
113+
}
114+
}
115+
116+
render(<Wrapper />, div)
117+
118+
expect(div.innerHTML).toMatchSnapshot()
119+
expect(document.head.childNodes.length).toBe(0)
120+
121+
window.hide()
122+
123+
expect(div.innerHTML).toBe('')
124+
expect(document.head.childNodes.length).toBe(2)
125+
expect(
126+
document.getElementById('__react_css_component_id-0').textContent
127+
).toBe(css1)
128+
expect(
129+
document.getElementById('__react_css_component_id-1').textContent
130+
).toBe(css2)
131+
})
132+
133+
it('should only move the style once to the document.head', () => {
134+
const { window } = new JSDOM('<html><body></body></html>')
135+
136+
global.window = window
137+
global.document = window.document
138+
139+
const div = document.createElement('div')
140+
const css = '.class1 { color: red } .class2 { color: blue }'
141+
142+
class Wrapper extends Component {
143+
constructor(props, context) {
144+
super(props, context)
145+
146+
this.state = {
147+
isVisible: true,
148+
}
149+
150+
window.hide = () => {
151+
this.setState({ isVisible: false })
152+
}
153+
}
154+
155+
render() {
156+
if (this.state.isVisible) {
157+
return [
158+
<Style key="style-1" css={css} />,
159+
<Style key="style-2" css={css} />,
160+
]
161+
}
162+
163+
return null
164+
}
165+
}
166+
167+
render(<Wrapper />, div)
168+
169+
expect(div.innerHTML).toMatchSnapshot()
170+
expect(document.head.childNodes.length).toBe(0)
171+
172+
window.hide()
173+
174+
expect(div.innerHTML).toBe('')
175+
expect(document.head.childNodes.length).toBe(1)
176+
expect(
177+
document.getElementById('__react_css_component_id-0').textContent
178+
).toBe(css)
179+
})
180+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`UniversalComponent Component should move each different style to the document.head if the component unmounts 1`] = `"<style>.class1 { color: red } .class2 { color: blue }</style><style>.class3 { color: green }</style>"`;
4+
5+
exports[`UniversalComponent Component should move the style to the document.head if the component unmounts 1`] = `"<style>.class1 { color: red } .class2 { color: blue }</style>"`;
6+
7+
exports[`UniversalComponent Component should only move the style once to the document.head 1`] = `"<style>.class1 { color: red } .class2 { color: blue }</style><style>.class1 { color: red } .class2 { color: blue }</style>"`;
8+
9+
exports[`UniversalComponent Component should render a style element on the client 1`] = `"<style>.class1 { color: red } .class2 { color: blue }</style>"`;
10+
11+
exports[`UniversalComponent Component should render a style element on the server 1`] = `"<style data-reactroot=\\"\\">.class1 { color: red } .class2 { color: blue }</style>"`;

0 commit comments

Comments
 (0)