-
Notifications
You must be signed in to change notification settings - Fork 17
/
ReComponent.js
229 lines (207 loc) · 6.1 KB
/
ReComponent.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* @flow */
import * as React from "react";
import {
ReComponent,
Update,
NoUpdate,
SideEffects,
UpdateWithSideEffects
} from "../../";
type Action =
| {| type: "A" |}
| {| type: "B" |}
| {| type: "C" |}
| {| type: "D" |};
class StateMismatch extends ReComponent<{}, { count: number }, Action> {
// $ExpectError
state = { invalid: "state" };
static reducer(action, state) {
switch (action.type) {
case "A":
return Update({});
case "B":
return Update({ count: 1 });
case "C":
// $ExpectError - `count` should be `number`
return Update({ count: "1" });
default:
// $ExpectError - `invalid` is missing in State
return Update({ invalid: "state" });
}
}
}
class UpdateTypes extends ReComponent<{}, { count: number }, Action> {
// Used to test the callback property of SideEffects
someClassProperty: number;
static reducer(action, state) {
switch (action.type) {
case "A":
return NoUpdate();
case "B":
return Update({ count: 1 });
case "C":
return SideEffects((instance: UpdateTypes) => {
instance.someClassProperty = 1;
// $ExpectError - `instance.someClassProperty` has to be number
instance.someClassProperty = "1";
});
default:
return UpdateWithSideEffects({ count: 1 }, (instance: UpdateTypes) => {
instance.someClassProperty = 1;
// $ExpectError - `instance.someClassProperty` has to be number
instance.someClassProperty = "1";
});
}
}
}
class TypedActionTypes extends ReComponent<
{},
{ count: number },
{| type: "CLICK" |}
> {
handleClick = () => this.send({ type: "CLICK" });
static reducer(action, state) {
switch (action.type) {
case "CLICK":
return NoUpdate();
default:
return NoUpdate();
}
}
}
const typedActionTypes = new TypedActionTypes();
typedActionTypes.send({ type: "CLICK" });
// $ExpectError - "CLACK" is invalid action type
typedActionTypes.send({ type: "CLACK" });
// $ExpectError - invalid action
typedActionTypes.send({});
typedActionTypes.handleClick();
// $ExpectError - `handleClick` expects no arguments
typedActionTypes.handleClick({});
// $ExpectError - `handleClick` expects no arguments
typedActionTypes.handleClick(1);
// Flow can verify that we've handled every defined action type for us through
// what is called [exhaustiveness testing].
//
// This can be done by using the special type `empty` and casting to it in the
// `default` or `else` branch. This will fail once Flow determines it can be
// reached.
//
// [exhaustiveness testing]: https://blog.jez.io/flow-exhaustiveness/
const absurd = <T>(x: empty): T => {
throw new Error("absurd");
};
class ExhaustivelyTypedFailingActionTypes extends ReComponent<
{},
{ count: number },
{| type: "CLICK" |} | {| type: "CLACK" |}
> {
static reducer(action, state) {
switch (action.type) {
case "CLICK":
return NoUpdate();
default: {
// $ExpectError - should be unreachable
absurd(action.type);
return NoUpdate();
}
}
}
}
class ExhaustivelyTypedPassingActionTypes extends ReComponent<
{},
{ count: number },
{ type: "CLICK" } | { type: "CLACK" }
> {
static reducer(action, state) {
switch (action.type) {
case "CLICK":
return NoUpdate();
case "CLACK":
return NoUpdate();
default: {
absurd(action.type);
return NoUpdate();
}
}
}
}
class FailingPayloadType extends ReComponent<
{},
{ count: number, awesome: boolean },
{ type: "CLICK", payload: number } | { type: "CLACK", payload: boolean }
> {
// $ExpectError - `clicks` should be `number`
handleClick = (clicks: boolean) =>
this.send({ type: "CLICK", payload: clicks });
// $ExpectError - `awesome` should be `boolean`
handleClack = (awesome: number) =>
this.send({ type: "CLACK", payload: awesome });
static reducer(action, state) {
switch (action.type) {
case "CLICK":
// $ExpectError - `awesome` should be `boolean`, but received `number`
return Update({ awesome: action.payload });
case "CLACK":
// $ExpectError - `count` should be `number`, but received `boolean`
return Update({ count: action.payload });
default: {
absurd(action.type);
return NoUpdate();
}
}
}
}
class PassingPayloadType extends ReComponent<
{},
{ count: number, awesome: boolean },
{ type: "CLICK", payload: number } | { type: "CLACK", payload: boolean }
> {
handleClick = (clicks: number) =>
this.send({ type: "CLICK", payload: clicks });
handleClack = (awesome: boolean) =>
this.send({ type: "CLACK", payload: awesome });
static reducer(action, state) {
switch (action.type) {
case "CLICK":
return Update({ count: action.payload });
case "CLACK":
return Update({ awesome: action.payload });
default: {
absurd(action.type);
return NoUpdate();
}
}
}
}
class CreateSenderTest extends ReComponent<
{},
{ count: number },
{| type: "CLICK" |} | {| type: "CLACK", payload: number |}
> {
handleClick = this.createSender("CLICK");
handleClack = this.createSender("CLACK");
// $ExpectError - "INVALID" is invalid action type
handleFoo = this.createSender("INVALID");
// $ExpectError - invalid action type
handleBar = this.createSender();
static reducer(action, state) {
return NoUpdate();
}
}
const createSenderTest = new CreateSenderTest();
createSenderTest.send({ type: "CLICK" });
createSenderTest.send({ type: "CLACK", payload: 0 });
// $ExpectError - "INVALID" is invalid action type
createSenderTest.send({ type: "INVALID" });
// $ExpectError - invalid action
createSenderTest.send({});
// $ExpectError - invalid payload
createSenderTest.send({ type: "CLACK", payload: "CLACK" });
// @TODO: Find out how we can assert the payload when using createSender
createSenderTest.handleClick();
createSenderTest.handleClick({});
createSenderTest.handleClick(1);
createSenderTest.handleClack(3);
createSenderTest.handleClack();
createSenderTest.handleClick("sda");