@@ -6,7 +6,7 @@ import 'test_locations.dart';
66
77void main () {
88 TestWidgetsFlutterBinding .ensureInitialized ();
9- final router = BeamerDelegate (
9+ final delegate = BeamerDelegate (
1010 locationBuilder: (state) {
1111 if (state.uri.pathSegments.contains ('l1' )) {
1212 return Location1 (state);
@@ -20,21 +20,21 @@ void main() {
2020 return NotFound (path: state.uri.toString ());
2121 },
2222 );
23- router .setNewRoutePath (BeamState .fromUri (Uri .parse ('/l1' )));
23+ delegate .setNewRoutePath (BeamState .fromUri (Uri .parse ('/l1' )));
2424
2525 group ('initialization & beaming' , () {
2626 test ('initialLocation is set' , () {
27- expect (router .currentBeamLocation, isA <Location1 >());
27+ expect (delegate .currentBeamLocation, isA <Location1 >());
2828 });
2929
3030 test ('beamTo changes locations' , () {
31- router .beamTo (Location2 (BeamState .fromUri (Uri .parse ('/l2' ))));
32- expect (router .currentBeamLocation, isA <Location2 >());
31+ delegate .beamTo (Location2 (BeamState .fromUri (Uri .parse ('/l2' ))));
32+ expect (delegate .currentBeamLocation, isA <Location2 >());
3333 });
3434
3535 test ('beamToNamed updates locations with correct parameters' , () {
36- router .beamToNamed ('/l2/2?q=t' , data: {'x' : 'y' });
37- final location = router .currentBeamLocation;
36+ delegate .beamToNamed ('/l2/2?q=t' , data: {'x' : 'y' });
37+ final location = delegate .currentBeamLocation;
3838 expect (location, isA <Location2 >());
3939 expect (location.state.pathParameters.containsKey ('id' ), true );
4040 expect (location.state.pathParameters['id' ], '2' );
@@ -46,10 +46,10 @@ void main() {
4646 test (
4747 'beaming to the same location type will not add it to history but will update current location' ,
4848 () {
49- final historyLength = router .beamLocationHistory.length;
50- router .beamToNamed ('/l2/2?q=t&r=s' , data: {'x' : 'z' });
51- final location = router .currentBeamLocation;
52- expect (router .beamLocationHistory.length, historyLength);
49+ final historyLength = delegate .beamLocationHistory.length;
50+ delegate .beamToNamed ('/l2/2?q=t&r=s' , data: {'x' : 'z' });
51+ final location = delegate .currentBeamLocation;
52+ expect (delegate .beamLocationHistory.length, historyLength);
5353 expect (location.state.pathParameters.containsKey ('id' ), true );
5454 expect (location.state.pathParameters['id' ], '2' );
5555 expect (location.state.queryParameters.containsKey ('q' ), true );
@@ -62,38 +62,38 @@ void main() {
6262 test (
6363 'popBeamLocation leads to previous location and all helpers are correct' ,
6464 () {
65- expect (router .canPopBeamLocation, true );
66- expect (router .popBeamLocation (), true );
67- expect (router .currentBeamLocation, isA <Location1 >());
65+ expect (delegate .canPopBeamLocation, true );
66+ expect (delegate .popBeamLocation (), true );
67+ expect (delegate .currentBeamLocation, isA <Location1 >());
6868
69- expect (router .canPopBeamLocation, false );
70- expect (router .popBeamLocation (), false );
71- expect (router .currentBeamLocation, isA <Location1 >());
69+ expect (delegate .canPopBeamLocation, false );
70+ expect (delegate .popBeamLocation (), false );
71+ expect (delegate .currentBeamLocation, isA <Location1 >());
7272 });
7373
7474 test ('duplicate locations are removed from history' , () {
75- expect (router .beamLocationHistory.length, 1 );
76- expect (router .beamLocationHistory[0 ], isA <Location1 >());
77- router .beamToNamed ('/l2' );
78- expect (router .beamLocationHistory.length, 2 );
79- expect (router .beamLocationHistory[0 ], isA <Location1 >());
80- router .beamToNamed ('/l1' );
81- expect (router .beamLocationHistory.length, 2 );
82- expect (router .beamLocationHistory[0 ], isA <Location2 >());
75+ expect (delegate .beamLocationHistory.length, 1 );
76+ expect (delegate .beamLocationHistory[0 ], isA <Location1 >());
77+ delegate .beamToNamed ('/l2' );
78+ expect (delegate .beamLocationHistory.length, 2 );
79+ expect (delegate .beamLocationHistory[0 ], isA <Location1 >());
80+ delegate .beamToNamed ('/l1' );
81+ expect (delegate .beamLocationHistory.length, 2 );
82+ expect (delegate .beamLocationHistory[0 ], isA <Location2 >());
8383 });
8484
8585 test (
8686 'beamTo replaceCurrent removes previous history state before appending new' ,
8787 () {
88- expect (router .beamLocationHistory.length, 2 );
89- expect (router .beamLocationHistory[0 ], isA <Location2 >());
90- expect (router .currentBeamLocation, isA <Location1 >());
91- router .beamTo (
88+ expect (delegate .beamLocationHistory.length, 2 );
89+ expect (delegate .beamLocationHistory[0 ], isA <Location2 >());
90+ expect (delegate .currentBeamLocation, isA <Location1 >());
91+ delegate .beamTo (
9292 Location2 (BeamState .fromUri (Uri .parse ('/l2' ))),
9393 replaceCurrent: true ,
9494 );
95- expect (router .beamLocationHistory.length, 1 );
96- expect (router .currentBeamLocation, isA <Location2 >());
95+ expect (delegate .beamLocationHistory.length, 1 );
96+ expect (delegate .currentBeamLocation, isA <Location2 >());
9797 });
9898 });
9999
@@ -102,96 +102,98 @@ void main() {
102102 await tester.pumpWidget (
103103 MaterialApp .router (
104104 routeInformationParser: BeamerParser (),
105- routerDelegate: router ,
105+ routerDelegate: delegate ,
106106 ),
107107 );
108- router .beamToNamed ('/l1/one' , stacked: false );
108+ delegate .beamToNamed ('/l1/one' , stacked: false );
109109 await tester.pump ();
110- expect (router .currentPages.length, 1 );
110+ expect (delegate .currentPages.length, 1 );
111111 });
112112
113113 test ('custom state can be updated' , () {
114- router.beamToNamed ('/custom' );
115- expect ((router.currentBeamLocation as CustomStateLocation ).state.customVar,
114+ delegate.beamToNamed ('/custom' );
115+ expect (
116+ (delegate.currentBeamLocation as CustomStateLocation ).state.customVar,
116117 'test' );
117- (router .currentBeamLocation as CustomStateLocation )
118+ (delegate .currentBeamLocation as CustomStateLocation )
118119 .update ((state) => state.copyWith (customVar: 'test-ok' ));
119- expect ((router.currentBeamLocation as CustomStateLocation ).state.customVar,
120+ expect (
121+ (delegate.currentBeamLocation as CustomStateLocation ).state.customVar,
120122 'test-ok' );
121123 });
122124
123125 test ('beamTo works without setting the BeamState explicitly' , () {
124- router .beamTo (NoStateLocation ());
125- expect (router .currentBeamLocation.state, isNotNull);
126- router .beamBack ();
126+ delegate .beamTo (NoStateLocation ());
127+ expect (delegate .currentBeamLocation.state, isNotNull);
128+ delegate .beamBack ();
127129 });
128130
129131 test ('clearHistory removes all but last entry (current location)' , () {
130- final currentBeamLocation = router .currentBeamLocation;
131- expect (router .beamLocationHistory.length, greaterThan (1 ));
132- router .clearBeamLocationHistory ();
133- expect (router .beamLocationHistory.length, equals (1 ));
134- expect (router .currentBeamLocation, currentBeamLocation);
132+ final currentBeamLocation = delegate .currentBeamLocation;
133+ expect (delegate .beamLocationHistory.length, greaterThan (1 ));
134+ delegate .clearBeamLocationHistory ();
135+ expect (delegate .beamLocationHistory.length, equals (1 ));
136+ expect (delegate .currentBeamLocation, currentBeamLocation);
135137 });
136138
137139 testWidgets ('popToNamed forces pop to specified location' , (tester) async {
138140 await tester.pumpWidget (
139141 MaterialApp .router (
140142 routeInformationParser: BeamerParser (),
141- routerDelegate: router ,
143+ routerDelegate: delegate ,
142144 ),
143145 );
144- router .beamToNamed ('/l1/one' , popToNamed: '/l2' );
146+ delegate .beamToNamed ('/l1/one' , popToNamed: '/l2' );
145147 await tester.pump ();
146- final historyLength = router .beamLocationHistory.length;
147- expect (router .currentBeamLocation, isA <Location1 >());
148- await router .popRoute ();
148+ final historyLength = delegate .beamLocationHistory.length;
149+ expect (delegate .currentBeamLocation, isA <Location1 >());
150+ await delegate .popRoute ();
149151 await tester.pump ();
150- expect (router .currentBeamLocation, isA <Location2 >());
151- expect (router .beamLocationHistory.length, equals (historyLength));
152+ expect (delegate .currentBeamLocation, isA <Location2 >());
153+ expect (delegate .beamLocationHistory.length, equals (historyLength));
152154 });
153155
154156 test ('beamBack leads to previous beam state and all helpers are correct' , () {
155- router .clearBeamStateHistory ();
156- expect (router .beamStateHistory.length, 1 );
157- expect (router .currentBeamLocation, isA <Location2 >());
158-
159- router .beamToNamed ('/l1' );
160- router .beamToNamed ('/l2' );
161-
162- expect (router .beamStateHistory.length, 3 );
163- expect (router .currentBeamLocation, isA <Location2 >());
164- expect (router .canBeamBack, true );
165-
166- router .beamToNamed ('/l1/one' );
167- router .beamToNamed ('/l1/two' );
168- expect (router .beamStateHistory.length, 5 );
169- expect (router .currentBeamLocation, isA <Location1 >());
170-
171- router .beamToNamed ('/l1/two' );
172- expect (router .beamStateHistory.length, 5 );
173- expect (router .currentBeamLocation, isA <Location1 >());
174-
175- expect (router .beamBack (), true );
176- expect (router .currentBeamLocation, isA <Location1 >());
177- expect (router .currentBeamLocation.state.uri.path, equals ('/l1/one' ));
178- expect (router .beamStateHistory.length, 4 );
179-
180- expect (router .beamBack (), true );
181- expect (router .currentBeamLocation, isA <Location2 >());
182- expect (router .beamStateHistory.length, 3 );
157+ delegate .clearBeamStateHistory ();
158+ expect (delegate .beamStateHistory.length, 1 );
159+ expect (delegate .currentBeamLocation, isA <Location2 >());
160+
161+ delegate .beamToNamed ('/l1' );
162+ delegate .beamToNamed ('/l2' );
163+
164+ expect (delegate .beamStateHistory.length, 3 );
165+ expect (delegate .currentBeamLocation, isA <Location2 >());
166+ expect (delegate .canBeamBack, true );
167+
168+ delegate .beamToNamed ('/l1/one' );
169+ delegate .beamToNamed ('/l1/two' );
170+ expect (delegate .beamStateHistory.length, 5 );
171+ expect (delegate .currentBeamLocation, isA <Location1 >());
172+
173+ delegate .beamToNamed ('/l1/two' );
174+ expect (delegate .beamStateHistory.length, 5 );
175+ expect (delegate .currentBeamLocation, isA <Location1 >());
176+
177+ expect (delegate .beamBack (), true );
178+ expect (delegate .currentBeamLocation, isA <Location1 >());
179+ expect (delegate .currentBeamLocation.state.uri.path, equals ('/l1/one' ));
180+ expect (delegate .beamStateHistory.length, 4 );
181+
182+ expect (delegate .beamBack (), true );
183+ expect (delegate .currentBeamLocation, isA <Location2 >());
184+ expect (delegate .beamStateHistory.length, 3 );
183185 });
184186
185187 testWidgets ('popToNamed() beams correctly' , (tester) async {
186188 await tester.pumpWidget (
187189 MaterialApp .router (
188190 routeInformationParser: BeamerParser (),
189- routerDelegate: router ,
191+ routerDelegate: delegate ,
190192 ),
191193 );
192- router .popToNamed ('/l1/one' );
194+ delegate .popToNamed ('/l1/one' );
193195 await tester.pump ();
194- expect (router .currentBeamLocation, isA <Location1 >());
196+ expect (delegate .currentBeamLocation, isA <Location1 >());
195197 });
196198
197199 testWidgets ('notFoundRedirect works' , (tester) async {
@@ -273,4 +275,63 @@ void main() {
273275 expect (scaffoldKey.currentState? .isDrawerOpen, isFalse);
274276 expect (delegate.state.uri.path, '/test' );
275277 });
278+
279+ group ('Keeping data' , () {
280+ final delegate = BeamerDelegate (
281+ locationBuilder: (state) {
282+ if (state.uri.pathSegments.contains ('l1' )) {
283+ return Location1 (state);
284+ }
285+ if (state.uri.pathSegments.contains ('l2' )) {
286+ return Location2 (state);
287+ }
288+ return NotFound (path: state.uri.toString ());
289+ },
290+ );
291+ testWidgets ('pop keeps data' , (tester) async {
292+ await tester.pumpWidget (
293+ MaterialApp .router (
294+ routeInformationParser: BeamerParser (),
295+ routerDelegate: delegate,
296+ ),
297+ );
298+ delegate.beamToNamed ('/l1/one' , data: {'x' : 'y' });
299+ await tester.pump ();
300+ expect (delegate.currentBeamLocation.state.uri.path, '/l1/one' );
301+ expect (delegate.currentBeamLocation.state.data, {'x' : 'y' });
302+
303+ delegate.navigatorKey.currentState! .pop ();
304+ await tester.pump ();
305+ expect (delegate.currentBeamLocation.state.uri.path, '/l1' );
306+ expect (delegate.currentBeamLocation.state.data, {'x' : 'y' });
307+ });
308+
309+ test ('single location keeps data' , () {
310+ delegate.beamToNamed ('/l1' , data: {'x' : 'y' });
311+ expect (delegate.currentBeamLocation.state.data, {'x' : 'y' });
312+
313+ delegate.beamToNamed ('/l1/one' );
314+ expect (delegate.currentBeamLocation.state.data, {'x' : 'y' });
315+ });
316+
317+ test ('data is kept throughout locations if not overwritten' , () {
318+ delegate.beamToNamed ('/l1' , data: {'x' : 'y' });
319+ expect (delegate.currentBeamLocation.state.data, {'x' : 'y' });
320+
321+ delegate.beamToNamed ('/l2' );
322+ expect (delegate.currentBeamLocation.state.data, {'x' : 'y' });
323+ });
324+
325+ test ('data is not kept if overwritten' , () {
326+ delegate.beamToNamed ('/l1' , data: {'x' : 'y' });
327+ expect (delegate.currentBeamLocation.state.data, {'x' : 'y' });
328+ delegate.beamToNamed ('/l1/one' , data: {});
329+ expect (delegate.currentBeamLocation.state.data, {});
330+
331+ delegate.beamToNamed ('/l1' , data: {'x' : 'y' });
332+ expect (delegate.currentBeamLocation.state.data, {'x' : 'y' });
333+ delegate.beamToNamed ('/l2' , data: {});
334+ expect (delegate.currentBeamLocation.state.data, {});
335+ });
336+ });
276337}
0 commit comments