@@ -36,6 +36,28 @@ void main() {
3636 final stream = ChangeNotifier ().toStream ();
3737 _isSingleSubscriptionStream (stream);
3838 });
39+
40+ test ('Cancel' , () async {
41+ final changeNotifier = ChangeNotifier ();
42+ final stream = changeNotifier.toStream ();
43+
44+ final subscription = stream.listen (
45+ expectAsync1 (
46+ (e) => expect (e, changeNotifier),
47+ count: 3 ,
48+ ),
49+ );
50+
51+ changeNotifier.notifyListeners ();
52+ changeNotifier.notifyListeners ();
53+ changeNotifier.notifyListeners ();
54+
55+ await pumpEventQueue ();
56+ await subscription.cancel ();
57+
58+ changeNotifier.notifyListeners ();
59+ changeNotifier.notifyListeners ();
60+ });
3961 });
4062
4163 group ('ValueListenableToStream' , () {
@@ -81,5 +103,53 @@ void main() {
81103 _isSingleSubscriptionStream (stream);
82104 }
83105 });
106+
107+ test ('Cancel' , () async {
108+ {
109+ final valueNotifier = ValueNotifier (0 );
110+ final stream = valueNotifier.toValueStream ();
111+
112+ var i = 1 ;
113+ final subscription = stream.listen (
114+ expectAsync1 (
115+ (e) => expect (e, i++ ),
116+ count: 3 ,
117+ ),
118+ );
119+
120+ valueNotifier.value = 1 ;
121+ valueNotifier.value = 2 ;
122+ valueNotifier.value = 3 ;
123+
124+ await pumpEventQueue ();
125+ await subscription.cancel ();
126+
127+ valueNotifier.value = 4 ;
128+ valueNotifier.value = 5 ;
129+ }
130+
131+ {
132+ final valueNotifier = ValueNotifier (0 );
133+ final stream = valueNotifier.toValueStream (replayValue: true );
134+
135+ var i = 0 ;
136+ final subscription = stream.listen (
137+ expectAsync1 (
138+ (e) => expect (e, i++ ),
139+ count: 4 ,
140+ ),
141+ );
142+
143+ valueNotifier.value = 1 ;
144+ valueNotifier.value = 2 ;
145+ valueNotifier.value = 3 ;
146+
147+ await pumpEventQueue ();
148+ await subscription.cancel ();
149+
150+ valueNotifier.value = 4 ;
151+ valueNotifier.value = 5 ;
152+ }
153+ });
84154 });
85155}
0 commit comments