@@ -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) async {
100
110
await tester.pump (Duration .zero);
101
111
await tester.pumpAndSettle ();
@@ -109,17 +119,20 @@ void main() {
109
119
alreadySubscribed: {}).toJson ());
110
120
111
121
check (find.byIcon (Icons .add).evaluate ()).isNotEmpty ();
122
+ check (find.byIcon (Icons .check).evaluate ()).isEmpty ();
112
123
113
124
await store.handleEvent (SubscriptionAddEvent (id: 1 ,
114
125
subscriptions: [eg.subscription (stream)]));
115
126
await tester.pumpAndSettle ();
116
127
117
128
check (find.byIcon (Icons .add).evaluate ()).isEmpty ();
129
+ check (find.byIcon (Icons .check).evaluate ()).isNotEmpty ();
118
130
119
131
await store.handleEvent (SubscriptionRemoveEvent (id: 2 , streamIds: [stream.streamId]));
120
132
await tester.pumpAndSettle ();
121
133
122
134
check (find.byIcon (Icons .add).evaluate ()).isNotEmpty ();
135
+ check (find.byIcon (Icons .check).evaluate ()).isEmpty ();
123
136
});
124
137
125
138
testWidgets ('is disabled while loading' , (WidgetTester tester) async {
@@ -228,5 +241,66 @@ void main() {
228
241
expectedMessage: 'NetworkException: Oops (ClientException: Oops)' );
229
242
});
230
243
});
244
+
245
+ group ('unsubscribe' , () {
246
+ testWidgets ('is shown only for streams that user is subscribed to' , (tester) async {
247
+ final streams = [eg.stream (), eg.stream (), eg.subscription (eg.stream ())];
248
+ final subscriptions = [streams[2 ] as Subscription ];
249
+ await setupChannelListPage (tester, streams: streams, subscriptions: subscriptions);
250
+
251
+ check (find.byIcon (Icons .check).evaluate ().length).equals (1 );
252
+ });
253
+
254
+ testWidgets ('smoke api' , (tester) async {
255
+ final stream = await prepareSingleSubscription (tester);
256
+ connection.prepare (json: UnsubscribeFromChannelsResult (
257
+ removed: [stream.name],
258
+ notRemoved: []).toJson ());
259
+ await tapUnsubscribeButton (tester);
260
+
261
+ await tester.pump (Duration .zero);
262
+ await tester.pumpAndSettle ();
263
+ check (connection.lastRequest).isA< http.Request > ()
264
+ ..method.equals ('DELETE' )
265
+ ..url.path.equals ('/api/v1/users/me/subscriptions' )
266
+ ..bodyFields.deepEquals ({
267
+ 'subscriptions' : jsonEncode ([stream.name])
268
+ });
269
+ });
270
+
271
+ testWidgets ('shows a snackbar when subscription passes' , (WidgetTester tester) async {
272
+ final stream = await prepareSingleSubscription (tester);
273
+ connection.prepare (json: UnsubscribeFromChannelsResult (
274
+ removed: [stream.name],
275
+ notRemoved: []).toJson ());
276
+ await tapUnsubscribeButton (tester);
277
+
278
+ await waitAndCheckSnackbarIsShown (tester,
279
+ zulipLocalizations.messageUnsubscribedFromChannel (stream.name));
280
+ });
281
+
282
+ testWidgets ('shows a snackbar when subscription fails' , (WidgetTester tester) async {
283
+ final stream = await prepareSingleSubscription (tester);
284
+ connection.prepare (json: UnsubscribeFromChannelsResult (
285
+ removed: [],
286
+ notRemoved: [stream.name]).toJson ());
287
+ await tapUnsubscribeButton (tester);
288
+
289
+ await waitAndCheckSnackbarIsShown (tester,
290
+ zulipLocalizations.errorFailedToUnsubscribedFromChannel (stream.name));
291
+ });
292
+
293
+ testWidgets ('catch-all api errors' , (WidgetTester tester) async {
294
+ final stream = await prepareSingleSubscription (tester);
295
+ connection.prepare (exception: http.ClientException ('Oops' ));
296
+ await tapUnsubscribeButton (tester);
297
+ await tester.pump (Duration .zero);
298
+ await tester.pumpAndSettle ();
299
+
300
+ checkErrorDialog (tester,
301
+ expectedTitle: zulipLocalizations.errorFailedToUnsubscribedFromChannel (stream.name),
302
+ expectedMessage: 'NetworkException: Oops (ClientException: Oops)' );
303
+ });
304
+ });
231
305
});
232
306
}
0 commit comments