-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathCSSMotionList.spec.js
96 lines (83 loc) · 2.7 KB
/
CSSMotionList.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint react/no-render-return-value:0, react/prefer-stateless-function:0, react/no-multi-comp:0 */
import React from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import TestUtils from 'react-dom/test-utils';
import expect from 'expect.js';
import { genCSSMotionList } from '../src/CSSMotionList';
import { genCSSMotion } from '../src/CSSMotion';
import './CSSMotion.spec.css';
describe('motion list', () => {
let div;
beforeEach(() => {
div = document.createElement('div');
document.body.appendChild(div);
});
afterEach(() => {
try {
ReactDOM.unmountComponentAtNode(div);
document.body.removeChild(div);
} catch (e) {
// Do nothing
}
});
describe('diff should work', () => {
function testMotion(CSSMotionList, done, injectLeave) {
let leaveCalled = 0;
function onLeaveEnd() {
leaveCalled += 1;
}
class Demo extends React.Component {
state = {
keys: [ 'a', 'b' ],
};
render() {
const { keys } = this.state;
return (
<CSSMotionList motionName="transition" keys={keys} onLeaveEnd={onLeaveEnd}>
{({ key, style, className }) => (
<div key={key} style={style} className={classNames('motion-box', className)}>
{key}
</div>
)}
</CSSMotionList>
);
}
}
ReactDOM.render(<Demo />, div, function init() {
const instance = this;
function checkKeys(targetKeys) {
const nodeList = TestUtils.scryRenderedDOMComponentsWithClass(instance, 'motion-box');
const keys = nodeList.map((node) => node.innerHTML);
expect(keys).to.eql(targetKeys);
}
checkKeys([ 'a', 'b' ]);
instance.setState({ keys: [ 'c', 'd' ] });
if (injectLeave) {
injectLeave(instance);
}
setTimeout(() => {
checkKeys([ 'c', 'd' ]);
if (injectLeave) {
expect(leaveCalled).to.be(2);
}
done();
}, 100);
});
}
it('with motion support', (done) => {
const CSSMotion = genCSSMotion({ transitionSupport: true, forwardRef: false });
const CSSMotionList = genCSSMotionList(true, CSSMotion);
testMotion(CSSMotionList, done, (instance) => {
const motionList = TestUtils.scryRenderedComponentsWithType(instance, CSSMotion);
motionList.slice(0, 2).forEach((cssMotion) => {
cssMotion.props.onLeaveEnd();
});
});
});
it('without motion support', (done) => {
const CSSMotionList = genCSSMotionList(false);
testMotion(CSSMotionList, done);
});
});
});