/
store_test.dart
68 lines (57 loc) · 1.94 KB
/
store_test.dart
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
import 'package:redux/redux.dart';
import 'package:test/test.dart';
import 'test_data.dart';
void main() {
group('Store', () {
test('calls the reducer when an action is fired', () {
final store = new Store<String>(stringReducer, initialState: 'Hello');
final action = 'test';
store.dispatch(action);
expect(store.state, equals(action));
});
test('canceled subscriber should not be notified', () {
var subscriber1Called = false;
var subscriber2Called = false;
final store = new Store<String>(
stringReducer,
initialState: 'hello',
syncStream: true,
);
final subscription = store.onChange.listen((String state) {
subscriber2Called = true;
});
store.onChange.listen((String state) {
subscriber1Called = true;
});
subscription.cancel();
store.dispatch('action');
expect(subscriber1Called, isTrue);
expect(subscriber2Called, isFalse);
});
test('store emits current state to subscribers', () {
final action = 'test';
final states = <String>[];
final store = new Store<String>(
stringReducer,
initialState: 'hello',
syncStream: true,
);
store.onChange.listen((state) => states.add(state));
// Dispatch two actions. Both should be emitted by default.
store.dispatch(action);
store.dispatch(action);
expect(states, <String>[action, action]);
});
test('store does not emit an onChange if distinct', () {
final action = 'test';
final states = <String>[];
final store = new Store<String>(stringReducer,
initialState: 'hello', syncStream: true, distinct: true);
store.onChange.listen((state) => states.add(state));
// Dispatch two actions. Only one should be emitted b/c distinct is true
store.dispatch(action);
store.dispatch(action);
expect(states, <String>[action]);
});
});
}