@@ -9,12 +9,29 @@ class TestZulipApp extends StatelessWidget {
9
9
const TestZulipApp ({
10
10
super .key,
11
11
this .accountId,
12
+ this .skipAssertAccountExists = false ,
12
13
this .navigatorObservers,
13
14
this .child = const Placeholder (),
14
- });
15
+ }) : assert ( ! skipAssertAccountExists || accountId != null ) ;
15
16
16
17
final int ? accountId;
17
18
19
+ /// Whether to proceed if [accountId] doesn't have an [Account] in the store.
20
+ ///
21
+ /// If this widget's [GlobalStoreWidget] loads
22
+ /// with no [Account] for [accountId] ,
23
+ /// [build] will error unless this param is true.
24
+ ///
25
+ /// Usually, this case is just a mistake;
26
+ /// the caller either forgot to add the account to the store
27
+ /// or they didn't want to simulate a per-account context in the first place.
28
+ ///
29
+ /// Sometimes we want to simulate an account's UI
30
+ /// just after the account is logged out (so is absent in the store)
31
+ /// but before we tear down that UI.
32
+ /// Pass true to silence the assertion in that case.
33
+ final bool skipAssertAccountExists;
34
+
18
35
/// A list to pass through to [MaterialApp.navigatorObservers] .
19
36
final List <NavigatorObserver >? navigatorObservers;
20
37
@@ -27,8 +44,31 @@ class TestZulipApp extends StatelessWidget {
27
44
28
45
@override
29
46
Widget build (BuildContext context) {
30
- return GlobalStoreWidget (
31
- child: MaterialApp (
47
+ return GlobalStoreWidget (child: Builder (builder: (context) {
48
+ assert (() {
49
+ if (accountId != null && ! skipAssertAccountExists) {
50
+ final account = GlobalStoreWidget .of (context).getAccount (accountId! );
51
+ if (account == null ) {
52
+ throw FlutterError .fromParts ([
53
+ ErrorSummary (
54
+ 'TestZulipApp() was called with [accountId] but a corresponding '
55
+ 'Account was not found in the GlobalStore.' ),
56
+ ErrorHint (
57
+ 'If [child] needs per-account data, consider calling '
58
+ '`testBinding.globalStore.add` before pumping `TestZulipApp`.' ),
59
+ ErrorHint (
60
+ 'If [child] is not specific to an account, omit [accountId].' ),
61
+ ErrorHint (
62
+ 'If you are testing behavior when an account is logged out, '
63
+ 'consider building ZulipApp instead of TestZulipApp, '
64
+ 'or pass `skipAssertAccountExists: true`.' ),
65
+ ]);
66
+ }
67
+ }
68
+ return true ;
69
+ }());
70
+
71
+ return MaterialApp (
32
72
title: 'Zulip' ,
33
73
localizationsDelegates: ZulipLocalizations .localizationsDelegates,
34
74
supportedLocales: ZulipLocalizations .supportedLocales,
@@ -39,6 +79,7 @@ class TestZulipApp extends StatelessWidget {
39
79
home: accountId != null
40
80
? PerAccountStoreWidget (accountId: accountId! , child: child)
41
81
: child,
42
- ));
82
+ );
83
+ }));
43
84
}
44
85
}
0 commit comments