Skip to content

Commit 2347aee

Browse files
committed
actions test [nfc]: Move a store-related test to a better home
This is more about testing the implementation of PerAccountStoreWidget to handle routeToRemoveOnLogout, instead of logOutAccount. Signed-off-by: Zixuan James Li <[email protected]>
1 parent a1c50e6 commit 2347aee

File tree

2 files changed

+74
-71
lines changed

2 files changed

+74
-71
lines changed

test/widgets/actions_test.dart

-71
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import 'dart:async';
21
import 'dart:convert';
32

43
import 'package:checks/checks.dart';
54
import 'package:flutter/foundation.dart';
65
import 'package:flutter/material.dart';
7-
import 'package:flutter_checks/flutter_checks.dart';
86
import 'package:flutter_test/flutter_test.dart';
97
import 'package:http/http.dart' as http;
108
import 'package:zulip/api/exception.dart';
@@ -17,9 +15,6 @@ import 'package:zulip/model/narrow.dart';
1715
import 'package:zulip/model/store.dart';
1816
import 'package:zulip/notifications/receive.dart';
1917
import 'package:zulip/widgets/actions.dart';
20-
import 'package:zulip/widgets/app.dart';
21-
import 'package:zulip/widgets/inbox.dart';
22-
import 'package:zulip/widgets/page.dart';
2318

2419
import '../api/fake_api.dart';
2520
import '../example_data.dart' as eg;
@@ -28,7 +23,6 @@ import '../model/store_checks.dart';
2823
import '../model/test_store.dart';
2924
import '../model/unreads_checks.dart';
3025
import '../stdlib_checks.dart';
31-
import '../test_navigation.dart';
3226
import 'dialog_checks.dart';
3327
import 'test_app.dart';
3428

@@ -157,71 +151,6 @@ void main() {
157151
await tester.pump(unregisterDelay - TestGlobalStore.removeAccountDuration);
158152
check(newConnection.isOpen).isFalse();
159153
});
160-
161-
testWidgets("logged-out account's routes removed from nav; other accounts' remain", (tester) async {
162-
Future<void> makeUnreadTopicInInbox(int accountId, String topic) async {
163-
final stream = eg.stream();
164-
final message = eg.streamMessage(stream: stream, topic: topic);
165-
final store = await testBinding.globalStore.perAccount(accountId);
166-
await store.addStream(stream);
167-
await store.addSubscription(eg.subscription(stream));
168-
await store.addMessage(message);
169-
await tester.pump();
170-
}
171-
172-
addTearDown(testBinding.reset);
173-
174-
final account1 = eg.account(id: 1, user: eg.user());
175-
final account2 = eg.account(id: 2, user: eg.user());
176-
await testBinding.globalStore.add(account1, eg.initialSnapshot());
177-
await testBinding.globalStore.add(account2, eg.initialSnapshot());
178-
179-
final testNavObserver = TestNavigatorObserver();
180-
await tester.pumpWidget(ZulipApp(navigatorObservers: [testNavObserver]));
181-
await tester.pump();
182-
final navigator = await ZulipApp.navigator;
183-
navigator.popUntil((_) => false); // clear starting routes
184-
await tester.pumpAndSettle();
185-
186-
final pushedRoutes = <Route<dynamic>>[];
187-
testNavObserver.onPushed = (route, prevRoute) => pushedRoutes.add(route);
188-
// TODO(#737): switch to a realistic setup:
189-
// https://github.com/zulip/zulip-flutter/pull/1076#discussion_r1874124363
190-
final account1Route = MaterialAccountWidgetRoute(
191-
accountId: account1.id, page: const InboxPageBody());
192-
final account2Route = MaterialAccountWidgetRoute(
193-
accountId: account2.id, page: const InboxPageBody());
194-
unawaited(navigator.push(account1Route));
195-
unawaited(navigator.push(account2Route));
196-
await tester.pumpAndSettle();
197-
check(pushedRoutes).deepEquals([account1Route, account2Route]);
198-
199-
await makeUnreadTopicInInbox(account1.id, 'topic in account1');
200-
final findAccount1PageContent = find.text('topic in account1', skipOffstage: false);
201-
202-
await makeUnreadTopicInInbox(account2.id, 'topic in account2');
203-
final findAccount2PageContent = find.text('topic in account2', skipOffstage: false);
204-
205-
final findLoadingPage = find.byType(LoadingPlaceholderPage, skipOffstage: false);
206-
207-
check(findAccount1PageContent).findsOne();
208-
check(findLoadingPage).findsNothing();
209-
210-
final removedRoutes = <Route<dynamic>>[];
211-
testNavObserver.onRemoved = (route, prevRoute) => removedRoutes.add(route);
212-
213-
final future = logOutAccount(testBinding.globalStore, account1.id);
214-
await tester.pump(TestGlobalStore.removeAccountDuration);
215-
await future;
216-
check(removedRoutes).single.identicalTo(account1Route);
217-
check(findAccount1PageContent).findsNothing();
218-
check(findLoadingPage).findsOne();
219-
220-
await tester.pump();
221-
check(findAccount1PageContent).findsNothing();
222-
check(findLoadingPage).findsNothing();
223-
check(findAccount2PageContent).findsOne();
224-
});
225154
});
226155

227156
group('unregisterToken', () {

test/widgets/store_test.dart

+74
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
import 'dart:async';
2+
13
import 'package:checks/checks.dart';
24
import 'package:flutter/material.dart';
5+
import 'package:flutter_checks/flutter_checks.dart';
36
import 'package:flutter_test/flutter_test.dart';
47
import 'package:zulip/model/store.dart';
8+
import 'package:zulip/widgets/actions.dart';
9+
import 'package:zulip/widgets/app.dart';
10+
import 'package:zulip/widgets/inbox.dart';
11+
import 'package:zulip/widgets/page.dart';
512
import 'package:zulip/widgets/store.dart';
613

714
import '../flutter_checks.dart';
815
import '../model/binding.dart';
916
import '../example_data.dart' as eg;
1017
import '../model/store_checks.dart';
18+
import '../model/test_store.dart';
19+
import '../test_navigation.dart';
1120

1221
/// A widget whose state uses [PerAccountStoreAwareStateMixin].
1322
class MyWidgetWithMixin extends StatefulWidget {
@@ -167,6 +176,71 @@ void main() {
167176
tester.widget(find.text('found store, account: ${eg.selfAccount.id}'));
168177
});
169178

179+
testWidgets("PerAccountStoreWidget.routeToRemoveOnLogout logged-out account's routes removed from nav; other accounts' remain", (tester) async {
180+
Future<void> makeUnreadTopicInInbox(int accountId, String topic) async {
181+
final stream = eg.stream();
182+
final message = eg.streamMessage(stream: stream, topic: topic);
183+
final store = await testBinding.globalStore.perAccount(accountId);
184+
await store.addStream(stream);
185+
await store.addSubscription(eg.subscription(stream));
186+
await store.addMessage(message);
187+
await tester.pump();
188+
}
189+
190+
addTearDown(testBinding.reset);
191+
192+
final account1 = eg.account(id: 1, user: eg.user());
193+
final account2 = eg.account(id: 2, user: eg.user());
194+
await testBinding.globalStore.add(account1, eg.initialSnapshot());
195+
await testBinding.globalStore.add(account2, eg.initialSnapshot());
196+
197+
final testNavObserver = TestNavigatorObserver();
198+
await tester.pumpWidget(ZulipApp(navigatorObservers: [testNavObserver]));
199+
await tester.pump();
200+
final navigator = await ZulipApp.navigator;
201+
navigator.popUntil((_) => false); // clear starting routes
202+
await tester.pumpAndSettle();
203+
204+
final pushedRoutes = <Route<dynamic>>[];
205+
testNavObserver.onPushed = (route, prevRoute) => pushedRoutes.add(route);
206+
// TODO(#737): switch to a realistic setup:
207+
// https://github.com/zulip/zulip-flutter/pull/1076#discussion_r1874124363
208+
final account1Route = MaterialAccountWidgetRoute(
209+
accountId: account1.id, page: const InboxPageBody());
210+
final account2Route = MaterialAccountWidgetRoute(
211+
accountId: account2.id, page: const InboxPageBody());
212+
unawaited(navigator.push(account1Route));
213+
unawaited(navigator.push(account2Route));
214+
await tester.pumpAndSettle();
215+
check(pushedRoutes).deepEquals([account1Route, account2Route]);
216+
217+
await makeUnreadTopicInInbox(account1.id, 'topic in account1');
218+
final findAccount1PageContent = find.text('topic in account1', skipOffstage: false);
219+
220+
await makeUnreadTopicInInbox(account2.id, 'topic in account2');
221+
final findAccount2PageContent = find.text('topic in account2', skipOffstage: false);
222+
223+
final findLoadingPage = find.byType(LoadingPlaceholderPage, skipOffstage: false);
224+
225+
check(findAccount1PageContent).findsOne();
226+
check(findLoadingPage).findsNothing();
227+
228+
final removedRoutes = <Route<dynamic>>[];
229+
testNavObserver.onRemoved = (route, prevRoute) => removedRoutes.add(route);
230+
231+
final future = logOutAccount(testBinding.globalStore, account1.id);
232+
await tester.pump(TestGlobalStore.removeAccountDuration);
233+
await future;
234+
check(removedRoutes).single.identicalTo(account1Route);
235+
check(findAccount1PageContent).findsNothing();
236+
check(findLoadingPage).findsOne();
237+
238+
await tester.pump();
239+
check(findAccount1PageContent).findsNothing();
240+
check(findLoadingPage).findsNothing();
241+
check(findAccount2PageContent).findsOne();
242+
});
243+
170244
testWidgets('PerAccountStoreAwareStateMixin', (tester) async {
171245
final widgetWithMixinKey = GlobalKey<MyWidgetWithMixinState>();
172246
final accountId = eg.selfAccount.id;

0 commit comments

Comments
 (0)