Skip to content

Commit bc464c4

Browse files
committed
fix: tests for enum actions
1 parent 0fa9d5b commit bc464c4

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ Initial Version of the library.
1212
* Added docs to the example
1313
* Migrated the example to functional widgets using [`functional_widget`](https://github.com/rrousselGit/functional_widget)
1414
* Fixed code styling and setup pedantic
15+
16+
## 0.1.2
17+
18+
* Added support for `localhost` in `remote-devtools` url.
19+
* Added example for enum actions.
20+
* Added tests for `configure_store`.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@ final reducer = createReducer<AppState>(
164164
);
165165
```
166166

167+
#### Enum actions
168+
169+
Since enum values are not types it is not possible to add a case for these but you can still use a matcher like the following:
170+
171+
```dart
172+
enum Actions {
173+
increment,
174+
decrement
175+
}
176+
177+
final reducer = createReducer<int>(0, (builder) {
178+
builder
179+
.addMatcher((action) => action == Actions.increment,
180+
(state, action) => state + 1)
181+
.addMatcher((action) => action == Actions.decrement,
182+
(state, action) => state - 1);
183+
});
184+
```
185+
186+
`Actions.increment` would be serialized as `{ "type": "increment" }` for `remote-devtools`.
187+
167188
#### API Reference
168189

169190
```dart

test/fakes/test_actions.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ class TestPayloadAction extends PayloadAction {
3636
const TestPayloadAction({dynamic payload, dynamic meta, dynamic error})
3737
: super(payload: payload, meta: meta, error: error);
3838
}
39+
40+
enum CounterActions {
41+
Increment,
42+
Decrement,
43+
}

test/tests/create_reducer_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void main() {
1717
Reducer<int> listReducer;
1818
Reducer<bool> statusReducer;
1919
Reducer<int> resetReducer;
20+
Reducer<int> enumReducer;
2021

2122
setUp(() {
2223
testStateReducer = createReducer<TestState>(initialState, (builder) {
@@ -51,6 +52,14 @@ void main() {
5152
resetReducer = createReducer<int>(5, (builder) {
5253
builder.addDefaultCase((state) => 0);
5354
});
55+
56+
enumReducer = createReducer<int>(0, (builder) {
57+
builder
58+
.addMatcher((action) => action == CounterActions.Increment,
59+
(state, action) => state + 1)
60+
.addMatcher((action) => action == CounterActions.Decrement,
61+
(state, action) => state - 1);
62+
});
5463
});
5564

5665
test('should yield the initialState', () {
@@ -161,5 +170,15 @@ void main() {
161170
final result = resetReducer(5, SomeUnhandledAction());
162171
expect(result, equals(0));
163172
});
173+
174+
test('should yield 4 as the next state', () {
175+
final result = enumReducer(3, CounterActions.Increment);
176+
expect(result, equals(4));
177+
});
178+
179+
test('should yield 24 as the next state', () {
180+
final result = enumReducer(25, CounterActions.Decrement);
181+
expect(result, equals(24));
182+
});
164183
});
165184
}

0 commit comments

Comments
 (0)