-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
debug.jsx
210 lines (186 loc) · 6.24 KB
/
debug.jsx
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React from 'react';
import { expect } from 'chai';
import { debugNodes } from 'enzyme/build/Debug';
import {
describeIf,
itIf,
} from '../../_helpers';
import { is } from '../../_helpers/version';
import {
createClass,
memo,
useCallback,
} from '../../_helpers/react-compat';
export default function describeDebug({
Wrap,
WrapRendered,
isShallow,
}) {
describe('.debug()', () => {
context('passes through to the debugNodes function', () => {
it('with wrapping an HTML element', () => {
const wrapper = Wrap(<div />);
expect(wrapper.debug()).to.equal('<div />');
expect(wrapper.debug()).to.equal(debugNodes(wrapper.getNodesInternal()));
});
it('with wrapping a createClass component', () => {
const Foo = createClass({
displayName: 'Bar',
render() { return <div />; },
});
const wrapper = Wrap(<Foo />);
const expectedDebug = isShallow
? '<div />'
: `<Bar>
<div />
</Bar>`;
expect(wrapper.debug()).to.equal(expectedDebug);
expect(wrapper.debug()).to.equal(debugNodes(wrapper.getNodesInternal()));
});
it('with wrapping a class component', () => {
class Foo extends React.Component {
render() {
return <div />;
}
}
const wrapper = Wrap(<Foo />);
const expectedDebug = isShallow
? '<div />'
: `<Foo>
<div />
</Foo>`;
expect(wrapper.debug()).to.equal(expectedDebug);
expect(wrapper.debug()).to.equal(debugNodes(wrapper.getNodesInternal()));
});
itIf(is('> 0.13'), 'with wrapping a stateless function component (SFC)', () => {
const wrapper = Wrap(<div />);
expect(wrapper.debug()).to.equal('<div />');
expect(wrapper.debug()).to.equal(debugNodes(wrapper.getNodesInternal()));
});
});
describeIf(is('>= 16.6'), 'React.memo', () => {
describe('display names', () => {
function SFC() { return null; }
function SFCwithDisplayName() { return null; }
SFCwithDisplayName.displayName = 'SFC!';
const SFCMemo = memo && memo(SFC);
const SFCwithDisplayNameMemo = memo && memo(SFCwithDisplayName);
const SFCMemoWithDisplayName = memo && memo(SFC);
SFCMemoWithDisplayName.displayName = 'SFCMemoWithDisplayName!';
const SFCMemoWitDoubleDisplayName = memo && memo(SFCwithDisplayName);
SFCMemoWitDoubleDisplayName.displayName = 'SFCMemoWitDoubleDisplayName!';
it('displays the expected display names', () => {
const wrapper = Wrap((
<div>
<SFC />
<SFCwithDisplayName />
<SFCMemo />
<SFCwithDisplayNameMemo />
<SFCMemoWithDisplayName />
<SFCMemoWitDoubleDisplayName />
</div>
));
expect(wrapper.debug()).to.equal(`<div>
<SFC />
<SFC! />
<Memo(SFC) />
<Memo(SFC!) />
<SFCMemoWithDisplayName! />
<SFCMemoWitDoubleDisplayName! />
</div>`);
});
});
describe('defaultProps', () => {
function Add({ a, b, c }) {
return <div>{String(a)}|{String(b)}|{String(c)}</div>;
}
Add.defaultProps = {
b: 2,
c: 3,
};
const MemoAdd = memo && memo(Add);
it('applies defaultProps to the component', () => {
const wrapper = WrapRendered(<Add />);
expect(wrapper.debug()).to.equal(`<div>
undefined
|
2
|
3
</div>`);
});
it('applies defaultProps to the memoized component', () => {
const wrapper = WrapRendered(<MemoAdd />);
expect(wrapper.debug()).to.equal(`<div>
undefined
|
2
|
3
</div>`);
});
it('applies defaultProps to the memoized component and does not override real props', () => {
const wrapper = WrapRendered(<MemoAdd a={10} b={20} />);
expect(wrapper.debug()).to.equal(`<div>
10
|
20
|
3
</div>`);
});
describeIf(is('>= 16.8'), 'full tree', () => {
function TransitionGroup({ children }) { return children; }
function CSSTransition({ children }) { return children; }
function Body({ imageToShow, switchImage }) {
const handlerClick = useCallback(
() => {
if (imageToShow === 1) {
return switchImage(2);
}
return switchImage(1);
},
[imageToShow, switchImage],
);
return (
<div className="styles.body">
<button type="button" onClick={handlerClick} className="buttonsStyles.button">
<TransitionGroup className="body.animWrap">
<CSSTransition classNames="mainImage" timeout={500} key={imageToShow}>
<img className="bodyImg" src={`../assets/${imageToShow}.png`} alt="main_img" />
</CSSTransition>
</TransitionGroup>
</button>
</div>
);
}
const BodyMemo = memo && memo(Body);
it('shows everything when not memoized', () => {
const wrapper = WrapRendered(<Body imageToShow={1} switchImage={() => {}} />);
expect(wrapper.debug()).to.equal(`<div className="styles.body">
<button type="button" onClick={[Function]} className="buttonsStyles.button">
<TransitionGroup className="body.animWrap">
<CSSTransition classNames="mainImage" timeout={500}>
<img className="bodyImg" src="../assets/1.png" alt="main_img" />
</CSSTransition>
</TransitionGroup>
</button>
</div>`);
});
it('shows everything when memoized', () => {
const wrapper = WrapRendered(<BodyMemo imageToShow={1} switchImage={() => {}} />);
expect(wrapper.debug()).to.equal(`<div className="styles.body">
<button type="button" onClick={[Function]} className="buttonsStyles.button">
<TransitionGroup className="body.animWrap">
<CSSTransition classNames="mainImage" timeout={500}>
<img className="bodyImg" src="../assets/1.png" alt="main_img" />
</CSSTransition>
</TransitionGroup>
</button>
</div>`);
});
});
});
});
});
}