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