Skip to content

Commit 8131cdc

Browse files
BenLorantfyCédric Delpoux
authored and
Cédric Delpoux
committed
Added fade animation and snapshot testing (#5)
Added fade animation props and snapshot testing
1 parent e8e5f79 commit 8131cdc

File tree

8 files changed

+391
-10
lines changed

8 files changed

+391
-10
lines changed

jest.config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
},
77
"moduleNameMapper": {
88
"\\.(css)$": "<rootDir>/node_modules/jest-css-modules"
9-
}
9+
},
10+
"snapshotSerializers": ["enzyme-to-json/serializer"]
1011
}

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"babel-preset-es2015": "^6.24.1",
3939
"babel-preset-react": "^6.24.1",
4040
"enzyme": "^2.9.1",
41+
"enzyme-to-json": "^2.0.1",
4142
"eslint": "^4.5.0",
4243
"eslint-config-prettier": "^2.3.0",
4344
"eslint-loader": "^1.9.0",
@@ -49,6 +50,7 @@
4950
"husky": "^0.14.3",
5051
"jest": "^20.0.4",
5152
"jest-css-modules": "^1.1.0",
53+
"jest-styled-components": "^4.10.0",
5254
"lint-staged": "^4.0.2",
5355
"markdown-loader": "^2.0.1",
5456
"nwb": "^0.18.0",
@@ -57,7 +59,8 @@
5759
"react": "^15.6.1",
5860
"react-demo-page": "^0.2.2",
5961
"react-dom": "^15.6.1",
60-
"react-test-renderer": "^15.6.1"
62+
"react-test-renderer": "^15.6.1",
63+
"sinon": "^4.2.2"
6164
},
6265
"peerDependencies": {
6366
"react": "^15.0.0 || ^16.0.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Tooltip renders 1`] = `
4+
.c0 {
5+
position: absolute;
6+
z-index: 8;
7+
top: 100%;
8+
left: 50%;
9+
-webkit-transform: translateX(-50%);
10+
-ms-transform: translateX(-50%);
11+
transform: translateX(-50%);
12+
margin-top: 8px;
13+
}
14+
15+
<Tooltip
16+
offset={8}
17+
open={true}
18+
placement="bottom"
19+
zIndex={8}
20+
>
21+
<styled.div
22+
offset={8}
23+
open={true}
24+
zIndex={8}
25+
>
26+
<div
27+
className="c0"
28+
offset={8}
29+
open={true}
30+
>
31+
😎
32+
</div>
33+
</styled.div>
34+
</Tooltip>
35+
`;
36+
37+
exports[`Tooltip renders 2`] = `
38+
.c0 {
39+
position: absolute;
40+
z-index: 8;
41+
bottom: 100%;
42+
left: 50%;
43+
-webkit-transform: translateX(-50%);
44+
-ms-transform: translateX(-50%);
45+
transform: translateX(-50%);
46+
margin-bottom: 8px;
47+
}
48+
49+
<Tooltip
50+
offset={8}
51+
open={true}
52+
placement="top"
53+
zIndex={8}
54+
>
55+
<styled.div
56+
offset={8}
57+
open={true}
58+
zIndex={8}
59+
>
60+
<div
61+
className="c0"
62+
offset={8}
63+
open={true}
64+
>
65+
😎
66+
</div>
67+
</styled.div>
68+
</Tooltip>
69+
`;
70+
71+
exports[`Tooltip renders 3`] = `
72+
.c0 {
73+
position: absolute;
74+
z-index: 8;
75+
left: 100%;
76+
top: 50%;
77+
-webkit-transform: translateY(-50%);
78+
-ms-transform: translateY(-50%);
79+
transform: translateY(-50%);
80+
margin-left: 8px;
81+
}
82+
83+
<Tooltip
84+
offset={8}
85+
open={true}
86+
placement="right"
87+
zIndex={8}
88+
>
89+
<styled.div
90+
offset={8}
91+
open={true}
92+
zIndex={8}
93+
>
94+
<div
95+
className="c0"
96+
offset={8}
97+
open={true}
98+
>
99+
😎
100+
</div>
101+
</styled.div>
102+
</Tooltip>
103+
`;
104+
105+
exports[`Tooltip renders 4`] = `
106+
.c0 {
107+
position: absolute;
108+
z-index: 8;
109+
right: 100%;
110+
top: 50%;
111+
-webkit-transform: translateY(-50%);
112+
-ms-transform: translateY(-50%);
113+
transform: translateY(-50%);
114+
margin-right: 8px;
115+
}
116+
117+
<Tooltip
118+
offset={8}
119+
open={true}
120+
placement="left"
121+
zIndex={8}
122+
>
123+
<styled.div
124+
offset={8}
125+
open={true}
126+
zIndex={8}
127+
>
128+
<div
129+
className="c0"
130+
offset={8}
131+
open={true}
132+
>
133+
😎
134+
</div>
135+
</styled.div>
136+
</Tooltip>
137+
`;
138+
139+
exports[`Tooltip should create a Tooltip with an animation 1`] = `
140+
.c0 {
141+
position: absolute;
142+
-webkit-animation: 5s ease-out 0s 1 bcCCNc;
143+
animation: 5s ease-out 0s 1 bcCCNc;
144+
z-index: 8;
145+
bottom: 100%;
146+
left: 50%;
147+
-webkit-transform: translateX(-50%);
148+
-ms-transform: translateX(-50%);
149+
transform: translateX(-50%);
150+
margin-bottom: 8px;
151+
}
152+
153+
<Tooltip
154+
fadeDuration={5}
155+
fadeEasing="ease-out"
156+
offset={8}
157+
open={true}
158+
zIndex={8}
159+
>
160+
<styled.div
161+
fadeDuration={5}
162+
fadeEasing="ease-out"
163+
offset={8}
164+
open={true}
165+
zIndex={8}
166+
>
167+
<div
168+
className="c0"
169+
offset={8}
170+
open={true}
171+
>
172+
😎
173+
</div>
174+
</styled.div>
175+
</Tooltip>
176+
`;

src/components/Tooltip/index.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
import React from "react"
22
import PropTypes from "prop-types"
3-
import styled from "styled-components"
3+
import styled, {keyframes} from "styled-components"
4+
import {easingPropType} from "../../utils/propTypes"
45

6+
const fadeAnimation = keyframes`
7+
0% {
8+
opacity: 0;
9+
}
10+
100% {
11+
opacity: 1;
12+
}
13+
`
14+
15+
const createAnimation = props => {
16+
return `animation:
17+
${props.fadeDuration}s
18+
${props.fadeEasing}
19+
0s
20+
1
21+
${fadeAnimation}
22+
`
23+
}
24+
25+
// prettier-ignore
526
const Base = styled.div`
627
position: absolute;
28+
${props => props.fadeDuration && `${createAnimation(props)};`}
729
${props => props.zIndex && `z-index: ${props.zIndex};`};
830
`
931

@@ -42,11 +64,25 @@ const tooltips = {
4264
bottom: Bottom,
4365
}
4466

45-
const Tooltip = ({children, offset, open, placement, zIndex}) => {
67+
const Tooltip = ({
68+
children,
69+
offset,
70+
open,
71+
placement,
72+
zIndex,
73+
fadeDuration,
74+
fadeEasing,
75+
}) => {
4676
const Component = tooltips[placement] || tooltips.top
4777
return (
4878
open && (
49-
<Component offset={offset} open={open} zIndex={zIndex}>
79+
<Component
80+
offset={offset}
81+
open={open}
82+
zIndex={zIndex}
83+
fadeDuration={fadeDuration}
84+
fadeEasing={fadeEasing}
85+
>
5086
{children}
5187
</Component>
5288
)
@@ -59,6 +95,8 @@ Tooltip.propTypes = {
5995
open: PropTypes.bool,
6096
placement: PropTypes.string,
6197
zIndex: PropTypes.number,
98+
fadeEasing: easingPropType,
99+
fadeDuration: PropTypes.number,
62100
}
63101

64102
export default Tooltip

src/components/Tooltip/index.test.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {mount} from "enzyme"
22
import React from "react"
33
import Tooltip from "./index"
4+
import "jest-styled-components"
45

56
const tooltipProps = {
67
offset: 8,
@@ -16,13 +17,32 @@ const NoTooltipFixture = <Tooltip {...tooltipProps} open={false} />
1617

1718
describe("Tooltip", () => {
1819
it("renders", () => {
19-
mount(TooltipUpFixture)
20-
mount(TooltipBottomFixture)
21-
mount(TooltipLeftFixture)
22-
mount(TooltipRightFixture)
20+
const wrappers = [
21+
mount(TooltipUpFixture),
22+
mount(TooltipBottomFixture),
23+
mount(TooltipLeftFixture),
24+
mount(TooltipRightFixture),
25+
]
26+
27+
wrappers.forEach(wrapper => {
28+
expect(wrapper).not.toHaveStyleRule("animation", /.*/)
29+
expect(wrapper).toMatchSnapshot()
30+
})
2331
})
2432

2533
it("do not render", () => {
26-
mount(NoTooltipFixture)
34+
expect(
35+
mount(NoTooltipFixture)
36+
.children()
37+
.get(0)
38+
).toBeFalsy()
39+
})
40+
41+
it("should create a Tooltip with an animation", () => {
42+
const wrapper = mount(
43+
<Tooltip {...tooltipProps} fadeDuration={5} fadeEasing={"ease-out"} />
44+
)
45+
expect(wrapper).toHaveStyleRule("animation", /5s ease-out 0s 1 \w+/)
46+
expect(wrapper).toMatchSnapshot()
2747
})
2848
})

src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import styled from "styled-components"
55
import Arrow from "./components/Arrow"
66
import Tooltip from "./components/Tooltip"
77
import Bubble from "./components/Bubble"
8+
import {easingPropType} from "./utils/propTypes"
89

910
const Container = styled.div`
1011
position: relative;
@@ -47,6 +48,8 @@ class Wrapper extends React.Component {
4748
placement,
4849
radius,
4950
zIndex,
51+
fadeEasing,
52+
fadeDuration,
5053
...props
5154
} = this.props
5255
const hasTrigger = children !== undefined && children !== null
@@ -56,6 +59,8 @@ class Wrapper extends React.Component {
5659
placement={placement}
5760
offset={arrow}
5861
zIndex={zIndex}
62+
fadeEasing={fadeEasing}
63+
fadeDuration={fadeDuration}
5964
>
6065
<Bubble
6166
background={background}
@@ -106,6 +111,8 @@ Wrapper.propTypes = {
106111
placement: PropTypes.oneOf(["left", "top", "right", "bottom"]),
107112
radius: PropTypes.number,
108113
zIndex: PropTypes.number,
114+
fadeEasing: easingPropType,
115+
fadeDuration: PropTypes.number,
109116
}
110117

111118
Wrapper.defaultProps = {
@@ -121,6 +128,8 @@ Wrapper.defaultProps = {
121128
placement: "top",
122129
radius: 0,
123130
zIndex: 1,
131+
fadeEasing: "linear",
132+
fadeDuration: 0,
124133
}
125134

126135
Wrapper.displayName = "Tooltip.Wrapper"

0 commit comments

Comments
 (0)