-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathlifecycle-1.js
171 lines (138 loc) · 4.22 KB
/
lifecycle-1.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
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
'use strict';
const Root = document.getElementById('root');
function assertEq(...args) {
var name, actual, expect = null
if (args.length == 2)
[actual, expect] = args
else if (args.length == 3)
[name, actual, expect] = args
else
throw `What? assertEq(${args})`
const a = JSON.stringify(actual)
const e = JSON.stringify(expect)
if (a !== e) {
console.log("Name: ", name)
console.log("Actual (val): ", actual)
console.log("Expect (val): ", expect)
console.log("Actual (cmp): ", a)
console.log("Expect (cmp): ", e)
throw `AssertionFailure${name && ` in ${name}`}: ${a} ≠ ${e}`
}
}
function assertOuterHTML(mounted, expect) {
const dom = ReactDOM.findDOMNode(mounted)
assertEq(dom.outerHTML, expect)
}
// ===================================================================================================
function Props(a, b, c) {
function minus(that) {
return Props(a - that.a, b - that.b, c - that.c)
}
return { a, b, c, minus }
}
var mountCountA = 0
var mountCountB = 0
var mountCountBeforeMountA = 0
var mountCountBeforeMountB = 0
var willMountCountA = 0
var willMountCountB = 0
function assertMountCount(expect) {
assertEq("mountCountA", mountCountA, expect)
assertEq("mountCountB", mountCountB, expect)
assertEq("willMountCountA", willMountCountA, expect)
assertEq("willMountCountB", willMountCountB, expect)
assertEq("mountCountBeforeMountA", mountCountBeforeMountA, 0)
assertEq("mountCountBeforeMountB", mountCountBeforeMountB, 0)
}
var didUpdates = []
var willUpdates = []
function assertUpdates(...expectedProps) {
assertEq("willUpdates", willUpdates, expectedProps)
assertEq("didUpdates", didUpdates, expectedProps)
}
var recievedPropDeltas = []
var willUnmountCount = 0
var err,info = null
class Inner extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
const p = this.props;
return React.createElement("div", null, `${p.a} ${p.b} ${p.c}`)
}
shouldComponentUpdate(next) {
const cur = this.props;
return (cur.a != next.a) || (cur.b != next.b)
}
componentWillMount() {
mountCountBeforeMountA += mountCountA
willMountCountA += 1
mountCountBeforeMountB += mountCountB
willMountCountB += 1
}
componentDidMount() {
mountCountA += 1
mountCountB += 1
}
componentWillUpdate(next) {
const cur = this.props;
willUpdates = willUpdates.concat(next.minus(cur))
}
componentDidUpdate(prev) {
const cur = this.props;
didUpdates = didUpdates.concat(cur.minus(prev))
}
componentWillUnmount() {
willUnmountCount += 1
}
componentWillReceiveProps(next) {
const cur = this.props;
console.log(`receive *** = ${recievedPropDeltas} | ${JSON.stringify(next)} | ${JSON.stringify(cur)}`)
recievedPropDeltas = recievedPropDeltas.concat(next.minus(cur))
}
}
class Comp extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
render() {
if (this.state.hasError)
return React.createElement("div", null, `Error: ${this.state.error}`)
else
return React.createElement(Inner, this.props)
}
componentDidCatch(e, i) {
err = e
info = i
console.log("Error: ", err)
console.log("Error msg: ", e.message)
console.log("Info: ", info)
this.setState({error: e.message, hasError: true})
}
}
// ===========================================================================================================================
function renderComp(props) {
return ReactDOM.render(React.createElement(Comp, props), Root)
}
assertMountCount(0)
var mounted = renderComp(Props(1, 2, 3))
assertMountCount(1)
assertOuterHTML(mounted, "<div>1 2 3</div>")
assertUpdates()
mounted = renderComp(Props(1, 2, 8))
assertOuterHTML(mounted, "<div>1 2 3</div>")
assertUpdates()
mounted = renderComp(Props(1, 5, 8))
assertOuterHTML(mounted, "<div>1 5 8</div>")
assertUpdates(Props(0, 3, 0))
assertEq("willUnmountCount", willUnmountCount, 0)
console.log("OMG 1")
const x = React.createElement(Comp, {a:"e"})
console.log("OMG 2")
mounted = ReactDOM.render(x, Root)
console.log("OMG 3")
assertOuterHTML(mounted, "<div>Error: next.minus is not a function</div>")
assertEq("willUnmountCount", willUnmountCount, 1)