@@ -92,10 +92,20 @@ void main() {
92
92
return stream;
93
93
}
94
94
95
+ Future <Subscription > prepareSingleSubscription (WidgetTester tester) async {
96
+ final stream = eg.subscription (eg.stream ());
97
+ await setupChannelListPage (tester, streams: [stream], subscriptions: [stream]);
98
+ return stream;
99
+ }
100
+
95
101
Future <void > tapSubscribeButton (WidgetTester tester) async {
96
102
await tester.tap (find.byIcon (Icons .add));
97
103
}
98
104
105
+ Future <void > tapUnsubscribeButton (WidgetTester tester) async {
106
+ await tester.tap (find.byIcon (Icons .check));
107
+ }
108
+
99
109
Future <void > waitAndCheckSnackbarIsShown (WidgetTester tester, String message, String channelName) async {
100
110
await tester.pump (Duration .zero);
101
111
await tester.pumpAndSettle ();
@@ -118,17 +128,20 @@ void main() {
118
128
alreadySubscribed: {}).toJson ());
119
129
120
130
check (find.byIcon (Icons .add).evaluate ()).isNotEmpty ();
131
+ check (find.byIcon (Icons .check).evaluate ()).isEmpty ();
121
132
122
133
await store.handleEvent (SubscriptionAddEvent (id: 1 ,
123
134
subscriptions: [eg.subscription (stream)]));
124
135
await tester.pumpAndSettle ();
125
136
126
137
check (find.byIcon (Icons .add).evaluate ()).isEmpty ();
138
+ check (find.byIcon (Icons .check).evaluate ()).isNotEmpty ();
127
139
128
140
await store.handleEvent (SubscriptionRemoveEvent (id: 2 , streamIds: [stream.streamId]));
129
141
await tester.pumpAndSettle ();
130
142
131
143
check (find.byIcon (Icons .add).evaluate ()).isNotEmpty ();
144
+ check (find.byIcon (Icons .check).evaluate ()).isEmpty ();
132
145
});
133
146
134
147
testWidgets ('is disabled while loading' , (WidgetTester tester) async {
@@ -236,5 +249,65 @@ void main() {
236
249
zulipLocalizations.errorFailedToSubscribeToChannel, stream.name);
237
250
});
238
251
});
252
+
253
+ group ('unsubscribe' , () {
254
+ testWidgets ('is shown only for streams that user is subscribed to' , (tester) async {
255
+ final streams = [eg.stream (), eg.stream (), eg.subscription (eg.stream ())];
256
+ final subscriptions = [streams[2 ] as Subscription ];
257
+ await setupChannelListPage (tester, streams: streams, subscriptions: subscriptions);
258
+
259
+ check (find.byIcon (Icons .check).evaluate ().length).equals (1 );
260
+ });
261
+
262
+ testWidgets ('smoke api' , (tester) async {
263
+ final stream = await prepareSingleSubscription (tester);
264
+ connection.prepare (json: UnsubscribeFromChannelsResult (
265
+ removed: [stream.name],
266
+ notRemoved: []).toJson ());
267
+ await tapUnsubscribeButton (tester);
268
+
269
+ await tester.pump (Duration .zero);
270
+ await tester.pumpAndSettle ();
271
+ check (connection.lastRequest).isA< http.Request > ()
272
+ ..method.equals ('DELETE' )
273
+ ..url.path.equals ('/api/v1/users/me/subscriptions' )
274
+ ..bodyFields.deepEquals ({
275
+ 'subscriptions' : jsonEncode ([stream.name])
276
+ });
277
+ });
278
+
279
+ testWidgets ('shows a snackbar when subscription passes' , (WidgetTester tester) async {
280
+ final stream = await prepareSingleSubscription (tester);
281
+ connection.prepare (json: UnsubscribeFromChannelsResult (
282
+ removed: [stream.name],
283
+ notRemoved: []).toJson ());
284
+ await tapUnsubscribeButton (tester);
285
+
286
+ await waitAndCheckSnackbarIsShown (tester,
287
+ zulipLocalizations.messageUnsubscribedFromChannel, stream.name);
288
+ });
289
+
290
+ testWidgets ('shows a snackbar when subscription fails' , (WidgetTester tester) async {
291
+ final stream = await prepareSingleSubscription (tester);
292
+ connection.prepare (json: UnsubscribeFromChannelsResult (
293
+ removed: [],
294
+ notRemoved: [stream.name]).toJson ());
295
+ await tapUnsubscribeButton (tester);
296
+
297
+ await waitAndCheckSnackbarIsShown (tester,
298
+ zulipLocalizations.errorFailedToUnsubscribeFromChannel, stream.name);
299
+ });
300
+
301
+ testWidgets ('catch-all api errors' , (WidgetTester tester) async {
302
+ final stream = await prepareSingleSubscription (tester);
303
+ connection.prepare (exception: http.ClientException ('Oops' ));
304
+ await tapUnsubscribeButton (tester);
305
+ await tester.pump (Duration .zero);
306
+ await tester.pumpAndSettle ();
307
+
308
+ await waitAndCheckSnackbarIsShown (tester,
309
+ zulipLocalizations.errorFailedToUnsubscribeFromChannel, stream.name);
310
+ });
311
+ });
239
312
});
240
313
}
0 commit comments