@@ -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 {
@@ -238,5 +251,66 @@ void main() {
238
251
expectedMessage: 'NetworkException: Oops (ClientException: Oops)' );
239
252
});
240
253
});
254
+
255
+ group ('unsubscribe' , () {
256
+ testWidgets ('is shown only for streams that user is subscribed to' , (tester) async {
257
+ final streams = [eg.stream (), eg.stream (), eg.subscription (eg.stream ())];
258
+ final subscriptions = [streams[2 ] as Subscription ];
259
+ await setupChannelListPage (tester, streams: streams, subscriptions: subscriptions);
260
+
261
+ check (find.byIcon (Icons .check).evaluate ().length).equals (1 );
262
+ });
263
+
264
+ testWidgets ('smoke api' , (tester) async {
265
+ final stream = await prepareSingleSubscription (tester);
266
+ connection.prepare (json: UnsubscribeFromChannelsResult (
267
+ removed: [stream.name],
268
+ notRemoved: []).toJson ());
269
+ await tapUnsubscribeButton (tester);
270
+
271
+ await tester.pump (Duration .zero);
272
+ await tester.pumpAndSettle ();
273
+ check (connection.lastRequest).isA< http.Request > ()
274
+ ..method.equals ('DELETE' )
275
+ ..url.path.equals ('/api/v1/users/me/subscriptions' )
276
+ ..bodyFields.deepEquals ({
277
+ 'subscriptions' : jsonEncode ([stream.name])
278
+ });
279
+ });
280
+
281
+ testWidgets ('shows a snackbar when subscription passes' , (WidgetTester tester) async {
282
+ final stream = await prepareSingleSubscription (tester);
283
+ connection.prepare (json: UnsubscribeFromChannelsResult (
284
+ removed: [stream.name],
285
+ notRemoved: []).toJson ());
286
+ await tapUnsubscribeButton (tester);
287
+
288
+ await waitAndCheckSnackbarIsShown (tester,
289
+ zulipLocalizations.messageUnsubscribedFromChannel, stream.name);
290
+ });
291
+
292
+ testWidgets ('shows a snackbar when subscription fails' , (WidgetTester tester) async {
293
+ final stream = await prepareSingleSubscription (tester);
294
+ connection.prepare (json: UnsubscribeFromChannelsResult (
295
+ removed: [],
296
+ notRemoved: [stream.name]).toJson ());
297
+ await tapUnsubscribeButton (tester);
298
+
299
+ await waitAndCheckSnackbarIsShown (tester,
300
+ zulipLocalizations.errorFailedToUnsubscribedFromChannel, stream.name);
301
+ });
302
+
303
+ testWidgets ('catch-all api errors' , (WidgetTester tester) async {
304
+ await prepareSingleSubscription (tester);
305
+ connection.prepare (exception: http.ClientException ('Oops' ));
306
+ await tapUnsubscribeButton (tester);
307
+ await tester.pump (Duration .zero);
308
+ await tester.pumpAndSettle ();
309
+
310
+ checkErrorDialog (tester,
311
+ expectedTitle: zulipLocalizations.errorFailedToUnsubscribedFromChannel,
312
+ expectedMessage: 'NetworkException: Oops (ClientException: Oops)' );
313
+ });
314
+ });
241
315
});
242
316
}
0 commit comments