Skip to content

Commit

Permalink
actions test: Use a more realistic setup for logOutAccount
Browse files Browse the repository at this point in the history
This rewrite ended being a bit more substantial than just switching both
accounts initial routes to the result of MessageListPage.buildRoute().

We do not use popUtil because the navigator stack normally should not
become empty, so the HomePage route for account1 stays. Additionally,
because we are pushing a different page route, we no longer need to set
up different messages as the discriminator, further simplifying the
test.

Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 committed Jan 7, 2025
1 parent a7a6d6e commit 0da58df
Showing 1 changed file with 25 additions and 41 deletions.
66 changes: 25 additions & 41 deletions test/widgets/actions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import 'package:zulip/model/store.dart';
import 'package:zulip/notifications/receive.dart';
import 'package:zulip/widgets/actions.dart';
import 'package:zulip/widgets/app.dart';
import 'package:zulip/widgets/inbox.dart';
import 'package:zulip/widgets/home.dart';
import 'package:zulip/widgets/message_list.dart';
import 'package:zulip/widgets/page.dart';
import 'package:zulip/widgets/store.dart';

Expand All @@ -31,6 +32,7 @@ import '../model/unreads_checks.dart';
import '../stdlib_checks.dart';
import '../test_navigation.dart';
import 'dialog_checks.dart';
import 'page_checks.dart';
import 'test_app.dart';

void main() {
Expand Down Expand Up @@ -157,16 +159,6 @@ void main() {
});

testWidgets("logged-out account's routes removed from nav; other accounts' remain", (tester) async {
Future<void> makeUnreadTopicInInbox(int accountId, String topic) async {
final stream = eg.stream();
final message = eg.streamMessage(stream: stream, topic: topic);
final store = await testBinding.globalStore.perAccount(accountId);
await store.addStream(stream);
await store.addSubscription(eg.subscription(stream));
await store.addMessage(message);
await tester.pump();
}

addTearDown(testBinding.reset);

final account1 = eg.account(id: 1, user: eg.user());
Expand All @@ -175,50 +167,42 @@ void main() {
await testBinding.globalStore.add(account2, eg.initialSnapshot());

final testNavObserver = TestNavigatorObserver();
final pushedRoutes = <Route<void>>[];
testNavObserver.onPushed = (route, prevRoute) => pushedRoutes.add(route);

await tester.pumpWidget(ZulipApp(navigatorObservers: [testNavObserver]));
await tester.pump();
final account1Route = pushedRoutes.single;
check(account1Route).isA<WidgetRoute>().page.isA<HomePage>();

final account2Connection =
(await testBinding.globalStore.perAccount(account2.id)).connection as FakeApiConnection;
account2Connection.prepare(json: eg.newestGetMessagesResult(
foundOldest: true, messages: []).toJson());
final account2Route = MessageListPage.buildRoute(
accountId: account2.id, narrow: const CombinedFeedNarrow());
final navigator = await ZulipApp.navigator;
navigator.popUntil((_) => false); // clear starting routes
await tester.pumpAndSettle();

final pushedRoutes = <Route<dynamic>>[];
testNavObserver.onPushed = (route, prevRoute) => pushedRoutes.add(route);
// TODO: switch to a realistic setup:
// https://github.com/zulip/zulip-flutter/pull/1076#discussion_r1874124363
final account1Route = MaterialAccountWidgetRoute(
accountId: account1.id, page: const InboxPageBody());
final account2Route = MaterialAccountWidgetRoute(
accountId: account2.id, page: const InboxPageBody());
unawaited(navigator.push(account1Route));
unawaited(navigator.push(account2Route));
await tester.pumpAndSettle();
check(pushedRoutes).deepEquals([account1Route, account2Route]);

await makeUnreadTopicInInbox(account1.id, 'topic in account1');
final findAccount1PageContent = find.text('topic in account1', skipOffstage: false);

await makeUnreadTopicInInbox(account2.id, 'topic in account2');
final findAccount2PageContent = find.text('topic in account2', skipOffstage: false);
await tester.pump();

final findLoadingPage = find.byType(LoadingPlaceholderPage, skipOffstage: false);
final findAccount1PageContent = find.byType(HomePage, skipOffstage: false);
final findAccount2PageContent = find.byType(MessageListPage, skipOffstage: false);

check(pushedRoutes).deepEquals([account1Route, account2Route]);
check(findAccount1PageContent).findsOne();
check(findLoadingPage).findsNothing();
check(findAccount2PageContent).findsOne();

final removedRoutes = <Route<dynamic>>[];
final removedRoutes = <Route<void>>[];
testNavObserver.onRemoved = (route, prevRoute) => removedRoutes.add(route);

final context = tester.element(find.byType(MaterialApp));
final future = logOutAccount(GlobalStoreWidget.of(context), account1.id);
final future = logOutAccount(testBinding.globalStore, account1.id);
await tester.pump(TestGlobalStore.removeAccountDuration);
await future;
await tester.pumpAndSettle(); // wait for animations, if any
check(removedRoutes).single.identicalTo(account1Route);
check(testBinding.globalStore.takeDoRemoveAccountCalls())
.single.equals(account1.id);
check(findAccount1PageContent).findsNothing();
check(findLoadingPage).findsOne();

await tester.pump();
check(findAccount1PageContent).findsNothing();
check(findLoadingPage).findsNothing();
check(findAccount2PageContent).findsOne();
});
});
Expand Down

0 comments on commit 0da58df

Please sign in to comment.