Skip to content

Commit 4aa933f

Browse files
committed
added styled-components, fixed jest setup (unresolved imports)
1 parent b0ecb72 commit 4aa933f

18 files changed

+662
-61
lines changed

.storybook/config.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ function loadStories() {
1212

1313
addDecorator(
1414
withOptions({
15-
name: 'Storybook',
15+
name: '@natterstefan',
16+
url: 'https://twitter.com/natterstefan',
17+
addonPanelInRight: true,
1618
goFullScreen: false,
17-
showStoriesPanel: true,
1819
showAddonPanel: true,
1920
showSearchBox: false,
20-
addonPanelInRight: true,
2121
sortStoriesByKind: true,
22+
showStoriesPanel: true,
2223
}),
2324
)
2425

.storybook/manager-head.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
document.title = '@natterstefan/react-component-library-lerna';
3+
</script>

.stylelintrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"processors": [
3+
"stylelint-processor-styled-components"
4+
],
5+
"extends": [
6+
"stylelint-config-recommended",
7+
"stylelint-config-styled-components"
8+
]
9+
}

CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
1313
- local verdaccio setup (with docker)
1414
- jest test suite setup
1515
- ESLint and prettier setup
16-
- babel setup
16+
- babel setup
17+
- styled-components setup

babel.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
module.exports = {
22
presets: ['@babel/preset-env', '@babel/preset-react'],
3+
plugins: [
4+
[
5+
'babel-plugin-styled-components',
6+
{
7+
ssr: true,
8+
displayName: true,
9+
fileName: false,
10+
},
11+
],
12+
],
313
}

jest.config.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
// partially inspired by https://github.com/facebook/jest/issues/3112
1+
/**
2+
* Notes
3+
*
4+
* - I had issues running tests in packages depending on other packages (symlinked
5+
* with lerna bootstrap) at first. Errors during tests was the result. After
6+
* reading this discussion, adding a `pretest` script resolved the issue.
7+
* See: https://github.com/lerna/lerna/issues/1367#issuecomment-380187436
8+
*
9+
* - Parts of this configuration are inspired by: https://github.com/facebook/jest/issues/3112#issuecomment-398581705
10+
*/
211
module.exports = {
312
collectCoverageFrom: [
413
'packages/**/*.js',
@@ -7,12 +16,17 @@ module.exports = {
716
'!packages/**/prop-types.js',
817
'!packages/**/*.stories.js',
918
'!packages/**/*.test.js',
19+
'!packages/**/jest.*.js',
1020
'!packages/__tests__/**/*.js',
1121
'!packages/__mocks__/**/*.js',
1222
],
1323
collectCoverage: true,
24+
modulePaths: ['<rootDir>'],
1425
notify: true,
1526
notifyMode: 'failure-change',
27+
projects: ['<rootDir>/packages/*'],
28+
resetMocks: true,
29+
resetModules: true,
1630
roots: ['<rootDir>/packages/'],
1731
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
1832
snapshotSerializers: ['enzyme-to-json/serializer'],
@@ -24,5 +38,5 @@ module.exports = {
2438
'.+\\.js$': 'babel-jest',
2539
},
2640
testEnvironment: 'jest-environment-jsdom-global',
27-
verbose: true,
41+
verbose: false, // if you want to see all tests for each suite, change to true
2842
}

package.json

+17-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
"build-storybook": "build-storybook -c .storybook -o dist/storybook",
1515
"clean": "lerna clean",
1616
"clean-dist": "lerna run prebuild --stream --parallel",
17-
"lint": "lerna run lint --stream --parallel",
17+
"lint": "yarn lint-css && yarn lint-js",
18+
"lint-css": "stylelint 'packages/**/*.js'",
19+
"lint-js": "eslint ./packages",
20+
"lint-lerna": "lerna run lint --stream --parallel",
1821
"prepublishOnly": "yarn clean && yarn && yarn build",
1922
"publish": "lerna publish",
20-
"setup": "yarn clean && yarn && yarn bootstrap",
21-
"start": "start-storybook -p 9001 -c .storybook --ci",
22-
"test": "jest",
23-
"watch-test": "jest --watch"
23+
"setup": "yarn && yarn clean && yarn bootstrap",
24+
"start": "yarn clean-dist && start-storybook -p 9001 -c .storybook --ci",
25+
"pretest": "yarn build",
26+
"test": "jest --detectOpenHandles",
27+
"posttest": "yarn clean-dist"
2428
},
2529
"publishConfig": {
2630
"access": "public",
@@ -36,7 +40,7 @@
3640
"prettier --write",
3741
"yarn lint",
3842
"git update-index --again",
39-
"jest --findRelatedTests"
43+
"yarn test --findRelatedTests"
4044
]
4145
},
4246
"repository": {
@@ -70,6 +74,7 @@
7074
"babel-eslint": "^10.0.1",
7175
"babel-jest": "24.1.0",
7276
"babel-loader": "^8.0.5",
77+
"babel-plugin-styled-components": "1.10.0",
7378
"enzyme": "3.8.0",
7479
"enzyme-adapter-react-16": "1.9.1",
7580
"enzyme-to-json": "3.3.5",
@@ -85,13 +90,18 @@
8590
"jest": "24.1.0",
8691
"jest-environment-jsdom": "24.0.0",
8792
"jest-environment-jsdom-global": "1.1.0",
93+
"jest-styled-components": "6.3.1",
8894
"lerna": "^3.11.0",
8995
"lint-staged": "^8.1.3",
9096
"prettier": "^1.16.4",
9197
"react-scripts": "^2.1.3",
9298
"rimraf": "^2.6.3",
9399
"storybook-addon-jsx": "^6.0.0",
94-
"storybook-readme": "^4.0.5"
100+
"storybook-readme": "^4.0.5",
101+
"stylelint": "9.10.1",
102+
"stylelint-config-recommended": "2.1.0",
103+
"stylelint-config-styled-components": "0.1.1",
104+
"stylelint-processor-styled-components": "1.5.2"
95105
},
96106
"dependencies": {
97107
"react": "^16.8.1",

packages/button/.stylelintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"../../.stylelintrc"
4+
]
5+
}

packages/button/jest.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const baseConfig = require('../../jest.config.js')
2+
3+
const pack = require('./package')
4+
5+
module.exports = {
6+
...baseConfig,
7+
displayName: pack.name,
8+
modulePaths: ['<rootDir>'],
9+
name: pack.name,
10+
rootDir: '../..',
11+
setupFilesAfterEnv: ['./jest.setup.js'],
12+
testMatch: ['/**/*.test.js'],
13+
}

packages/button/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"scripts": {
1313
"prebuild": "rimraf dist",
1414
"build": "babel --root-mode upward src --ignore */*.test.js,**/*.test.js,*/*.stories.js,**/stories.js --out-dir dist",
15+
"pretest": "yarn build",
1516
"lint": "eslint src",
1617
"test": "jest"
1718
},
@@ -22,6 +23,7 @@
2223
"author": "Stefan Natter",
2324
"license": "MIT",
2425
"dependencies": {
25-
"react": "^16.8.1"
26+
"react": "^16.8.1",
27+
"styled-components": "^4.1.3"
2628
}
2729
}

packages/button/src/index.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
import React from 'react'
2+
import styled from 'styled-components'
23

3-
const Button = ({ onClick, children }) => (
4-
<button type="button" onClick={onClick}>
4+
export const StyledButton = styled.button`
5+
border: 1px solid #000;
6+
padding: 10px;
7+
8+
:hover {
9+
border: 2px solid blue;
10+
cursor: pointer;
11+
font-weight: 700;
12+
}
13+
14+
:active,
15+
:focus,
16+
:visited {
17+
outline: none;
18+
}
19+
`
20+
21+
export const Button = ({ onClick, children }) => (
22+
<StyledButton type="button" onClick={onClick}>
523
{children}
6-
</button>
24+
</StyledButton>
725
)
826

927
export default Button

packages/button/src/index.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import { shallow } from 'enzyme'
2+
import { mount, shallow } from 'enzyme'
33

44
import Button from '.'
55

@@ -11,7 +11,7 @@ describe('Button', () => {
1111

1212
it('invokes onClick when clicked', () => {
1313
const onClick = jest.fn()
14-
const wrapper = shallow(<Button onClick={onClick}>Hello Button</Button>)
14+
const wrapper = mount(<Button onClick={onClick}>Hello Button</Button>)
1515
wrapper.find('button').simulate('click')
1616
expect(onClick).toHaveBeenCalledTimes(1)
1717
})

packages/card/.stylelintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"../../.stylelintrc"
4+
]
5+
}

packages/card/jest.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const baseConfig = require('../../jest.config.js')
2+
3+
const pack = require('./package')
4+
5+
module.exports = {
6+
...baseConfig,
7+
displayName: pack.name,
8+
name: pack.name,
9+
rootDir: '../..',
10+
setupFilesAfterEnv: ['./jest.setup.js'],
11+
testMatch: ['/**/*.test.js'],
12+
}

packages/card/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "Example card component",
55
"main": "./dist/index.js",
66
"module": "./src/index.js",
7-
"files": ["dist"],
7+
"files": [
8+
"dist"
9+
],
810
"directories": {
911
"lib": "src"
1012
},
@@ -23,6 +25,7 @@
2325
"license": "MIT",
2426
"dependencies": {
2527
"@natterstefan/ns-button-js": "^0.1.0",
26-
"react": "^16.8.1"
28+
"react": "^16.8.1",
29+
"styled-components": "^4.1.3"
2730
}
2831
}

packages/card/src/index.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import React from 'react'
2+
import styled from 'styled-components'
23
import Button from '@natterstefan/ns-button-js'
34

4-
const Card = ({ onClick, buttonText, title }) => (
5-
<div
6-
style={{
7-
border: '1px solid black',
8-
padding: '5px 20px 20px',
9-
textAlign: 'center',
10-
}}
11-
>
5+
export const StyledCard = styled.div`
6+
border: 1px solid black;
7+
padding: 0 10px 15px;
8+
text-align: center;
9+
`
10+
11+
export const Card = ({ onClick, buttonText, title }) => (
12+
<StyledCard>
1213
<h1>{title}</h1>
1314
<Button onClick={onClick}>{buttonText}</Button>
14-
</div>
15+
</StyledCard>
1516
)
1617

1718
export default Card

packages/card/src/index.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react'
22
import { mount, shallow } from 'enzyme'
3-
4-
import Button from '../../button'
3+
import Button from '@natterstefan/ns-button-js'
54

65
import Card from '.'
76

0 commit comments

Comments
 (0)