-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
create-reducer.spec.ts
258 lines (231 loc) · 6.87 KB
/
create-reducer.spec.ts
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import * as T from './type-helpers'; // type-tests global
import { ActionType } from './type-helpers';
import { createAction } from './create-action';
import { createReducer } from './create-reducer';
import { getType } from './get-type';
const add = createAction('ADD')<number>();
const increment = createAction('INCREMENT')();
const decrement = createAction('DECREMENT')();
const actions = {
add,
increment,
decrement,
};
declare module './type-helpers' {
interface Types {
RootAction: ActionType<typeof actions>;
}
}
const initialState = 0;
// @dts-jest:group With Action Creators
{
const emptyReducer = createReducer(initialState);
const counterReducer1 = emptyReducer.handleAction(
[add, increment],
(state, action) => state + (action.type === 'ADD' ? action.payload : 1)
);
// @dts-jest:pass:snap
counterReducer1.handlers;
// @dts-jest:pass
Object.keys({ ...counterReducer1.handlers }); // => ["ADD", "INCREMENT"]
// @dts-jest:pass
Object.keys({ ...emptyReducer.handlers }); // => []
const counterReducer2 = emptyReducer
.handleAction([add], (state, action) => state + action.payload)
.handleAction([increment], (state, _) => state + 1);
// @dts-jest:pass:snap
counterReducer2.handlers;
// @dts-jest:pass
Object.keys({ ...counterReducer2.handlers }); // => ["ADD", "INCREMENT"]
// @dts-jest:pass
Object.keys({ ...emptyReducer.handlers }); // => []
const counterReducer3 = emptyReducer.handleAction(
add,
(state, action) => state + action.payload
);
// @dts-jest:pass:snap
counterReducer3.handlers;
// @dts-jest:pass
Object.keys({ ...counterReducer3.handlers }); // => [ "ADD"]
// @dts-jest:pass
Object.keys({ ...emptyReducer.handlers }); // => []
const counterReducer4 = emptyReducer.handleAction(
increment,
(state, _) => state + 1
);
// @dts-jest:pass:snap
counterReducer4.handlers;
// @dts-jest:pass
Object.keys({ ...counterReducer4.handlers }); // => [ "INCREMENT"]
// @dts-jest:pass
Object.keys({ ...emptyReducer.handlers }); // => []
{
[
counterReducer1,
counterReducer2,
createReducer(initialState, {
[getType(add)]: counterReducer3.handlers.ADD,
[getType(increment)]: counterReducer4.handlers.INCREMENT,
}),
// has to be typed as any because of typescript bug: https://github.com/microsoft/TypeScript/issues/29718
createReducer(initialState, {
[getType(add)]: (state: any, action: any) => state + action.payload,
[getType(increment)]: (state: any, _: any) => state + 1,
}),
createReducer<number, ActionType<typeof actions>>(initialState, {
[getType(add)]: (state: any, action: any) => state + action.payload,
[getType(increment)]: (state: any, _: any) => state + 1,
}),
].forEach(fn => {
// @dts-jest:pass
fn(0, {} as any); // => 0
// @dts-jest:pass
fn(0, increment()); // => 1
// @dts-jest:pass
fn(0, add(4)); // => 4
});
}
}
// @dts-jest:group Type refinement checks
{
type State = {
foo: string | null;
};
const defaultState: State = {
foo: null,
};
const actions2 = {
foo1: createAction('foo1')<string>(),
foo2: createAction('foo2')<string>(),
foo3: createAction('foo3')(),
foo4: createAction('foo4')(),
};
type Action = ActionType<typeof actions2>;
const reducer = createReducer<State, Action>(defaultState)
.handleAction(actions2.foo1, (state, action) => {
// @dts-jest:pass:snap
state;
// @dts-jest:pass:snap
action;
return {
...state,
foo: action.payload,
};
})
.handleAction(actions2.foo2, (state, action) => {
// @dts-jest:pass:snap
state;
// @dts-jest:pass:snap
action;
return {
...state,
foo: action.payload,
};
})
.handleAction(actions2.foo3, (state, action) => {
// @dts-jest:pass:snap
state;
// @dts-jest:pass:snap
action;
return {
...state,
foo: 'empty',
};
})
.handleAction(actions2.foo4, (state, action) => {
// @dts-jest:pass:snap
state;
// @dts-jest:pass:snap
action;
return {
...state,
foo: 'empty',
};
});
// @dts-jest:pass:snap
reducer.handlers;
{
[
reducer(defaultState, actions2.foo1('check')),
reducer(defaultState, actions2.foo2('check')),
].forEach(reducerResult => {
// @dts-jest:pass:snap
reducerResult; // => { foo: "check" }
});
[
reducer(defaultState, actions2.foo3()),
reducer(defaultState, actions2.foo4()),
].forEach(reducerResult => {
// @dts-jest:pass:snap
reducerResult; // => { foo: "empty" }
});
}
}
// @dts-jest:group With Action Types
{
const reducerTest = createReducer(initialState);
const counterReducer1 = reducerTest.handleType(
['ADD', 'INCREMENT'],
(state, action) => state + (action.type === 'ADD' ? action.payload : 1)
);
// @dts-jest:pass:snap
counterReducer1.handlers;
// @dts-jest:pass
Object.keys({ ...counterReducer1.handlers }); // => ["ADD", "INCREMENT"]
// @dts-jest:pass
Object.keys({ ...reducerTest.handlers }); // => []
const counterReducer2 = reducerTest
.handleType(['ADD'], (state, action) => state + action.payload)
.handleType(['INCREMENT'], (state, _) => state + 1);
// @dts-jest:pass:snap
counterReducer2.handlers;
// @dts-jest:pass
Object.keys({ ...counterReducer2.handlers }); // => ["ADD", "INCREMENT"]
// @dts-jest:pass
Object.keys({ ...reducerTest.handlers }); // => []
const counterReducer3 = reducerTest.handleType(
'ADD',
(state, action) => state + action.payload
);
// @dts-jest:pass:snap
counterReducer3.handlers;
// @dts-jest:pass
Object.keys({ ...counterReducer3.handlers }); // => ["ADD"]
// @dts-jest:pass
Object.keys({ ...reducerTest.handlers }); // => []
const counterReducer4 = reducerTest.handleType(
'INCREMENT',
(state, _) => state + 1
);
// @dts-jest:pass:snap
counterReducer4.handlers;
// @dts-jest:pass
Object.keys({ ...counterReducer4.handlers }); // => [ "INCREMENT"]
// @dts-jest:pass
Object.keys({ ...reducerTest.handlers }); // => []
{
[
counterReducer1,
counterReducer2,
createReducer(initialState, {
ADD: counterReducer3.handlers.ADD,
INCREMENT: counterReducer4.handlers.INCREMENT,
}),
createReducer(initialState, {
ADD: (state, action) => state + action.payload,
INCREMENT: (state, action) => state + 1,
}),
createReducer<number, ActionType<typeof actions>>(initialState, {
ADD: (state, action) => state + action.payload,
INCREMENT: (state, action) => state + 1,
}),
].forEach(fn => {
// @dts-jest:pass
fn(0, {} as any); // => 0
// @dts-jest:pass
fn(0, increment()); // => 1
// @dts-jest:pass
fn(0, add(4)); // => 4
});
}
}