This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathenhancer-test.js
133 lines (109 loc) · 3.35 KB
/
enhancer-test.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
const React = require('react');
const Enhancer = require('../lib/enhancer').default;
const createEsClass = require('../lib/test-helpers').createEsClass;
const render = require('./utils').render;
// Full assertion wrapper
const assertValidEnhancedComponent = Composed => {
const Enhanced = Enhancer(Composed);
const rendered = render(Enhanced);
expect(render(Composed)).to.not.equal(rendered);
expect(rendered)
.to.contain('data-radium="true"')
.and.to.contain('style="background:red;color:white"');
};
describe('Enhancer', () => {
let testElement;
beforeEach(() => {
testElement = React.createElement('div', {
style: [{background: 'red'}, {color: 'white'}]
});
});
it('handles arrow functions', () => {
const Composed = () => testElement;
assertValidEnhancedComponent(Composed);
});
it('handles real functions', () => {
const Composed = function() {
return testElement;
};
assertValidEnhancedComponent(Composed);
});
it('handles babel-ified ES classes', () => {
const Composed = createEsClass(() => testElement);
assertValidEnhancedComponent(Composed);
});
// Regression test - `Radium wrapping not compatible with native classes?`
// https://github.com/FormidableLabs/radium/issues/576
it('handles native ES classes', () => {
class Composed extends React.Component {
render() {
return testElement;
}
}
assertValidEnhancedComponent(Composed);
});
it('handles native ES classes with bound methods', () => {
class TestComponent extends React.Component {
constructor() {
super(...arguments);
this.checkProp = this.checkProp.bind(this);
this.checkState = this.checkState.bind(this);
this.state = {v: 1};
}
componentWillReceiveProps() {
this.setState(state => ({v: state.v + 1}));
}
checkProp() {
return this.props.flag;
}
checkState() {
return this.state.v;
}
touchState() {
this.state.v += 1;
}
render() {
return React.createElement('p', {}, [
String(this.props.flag),
' == ',
String(this.checkProp()),
', ',
String(this.state.v),
' == ',
String(this.checkState())
]);
}
}
const cleanup = require('jsdom-global')();
try {
const Enhanced = Enhancer(TestComponent);
const elt = React.createElement(Enhanced, {flag: true});
const {configure, mount} = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
configure({adapter: new Adapter()});
const wrapper = mount(elt);
expect(wrapper.html()).is.equal(
'<p data-radium="true">true == true, 1 == 1</p>'
);
wrapper.setProps({flag: false});
expect(wrapper.html()).is.equal(
'<p data-radium="true">false == false, 2 == 2</p>'
);
wrapper.setProps({flag: true});
expect(wrapper.html()).is.equal(
'<p data-radium="true">true == true, 3 == 3</p>'
);
} finally {
cleanup();
}
}).timeout(10000);
it('works with hooks', () => {
const Composed = function() {
// eslint-disable-next-line no-unused-vars
const [counter, setCount] = React.useState(0);
return testElement;
};
assertValidEnhancedComponent(Composed);
});
});