@@ -78,6 +78,114 @@ void main() {
7878 );
7979 });
8080
81+ group ('replace' , () {
82+ testWidgets (
83+ 'It should replace the last match with the given one' ,
84+ (WidgetTester tester) async {
85+ final GoRouter goRouter = GoRouter (
86+ initialLocation: '/' ,
87+ routes: < GoRoute > [
88+ GoRoute (path: '/' , builder: (_, __) => const SizedBox ()),
89+ GoRoute (path: '/page-0' , builder: (_, __) => const SizedBox ()),
90+ GoRoute (path: '/page-1' , builder: (_, __) => const SizedBox ()),
91+ ],
92+ );
93+ await tester.pumpWidget (
94+ MaterialApp .router (
95+ routeInformationProvider: goRouter.routeInformationProvider,
96+ routeInformationParser: goRouter.routeInformationParser,
97+ routerDelegate: goRouter.routerDelegate,
98+ ),
99+ );
100+
101+ goRouter.push ('/page-0' );
102+
103+ goRouter.routerDelegate.addListener (expectAsync0 (() {}));
104+ final GoRouteMatch first = goRouter.routerDelegate.matches.first;
105+ final GoRouteMatch last = goRouter.routerDelegate.matches.last;
106+ goRouter.replace ('/page-1' );
107+ expect (goRouter.routerDelegate.matches.length, 2 );
108+ expect (
109+ goRouter.routerDelegate.matches.first,
110+ first,
111+ reason: 'The first match should still be in the list of matches' ,
112+ );
113+ expect (
114+ goRouter.routerDelegate.matches.last,
115+ isNot (last),
116+ reason: 'The last match should have been removed' ,
117+ );
118+ expect (
119+ goRouter.routerDelegate.matches.last.fullpath,
120+ '/page-1' ,
121+ reason: 'The new location should have been pushed' ,
122+ );
123+ },
124+ );
125+ });
126+
127+ group ('replaceNamed' , () {
128+ testWidgets (
129+ 'It should replace the last match with the given one' ,
130+ (WidgetTester tester) async {
131+ final GoRouter goRouter = GoRouter (
132+ initialLocation: '/' ,
133+ routes: < GoRoute > [
134+ GoRoute (path: '/' , builder: (_, __) => const SizedBox ()),
135+ GoRoute (
136+ path: '/page-0' ,
137+ name: 'page0' ,
138+ builder: (_, __) => const SizedBox ()),
139+ GoRoute (
140+ path: '/page-1' ,
141+ name: 'page1' ,
142+ builder: (_, __) => const SizedBox ()),
143+ ],
144+ );
145+ await tester.pumpWidget (
146+ MaterialApp .router (
147+ routeInformationProvider: goRouter.routeInformationProvider,
148+ routeInformationParser: goRouter.routeInformationParser,
149+ routerDelegate: goRouter.routerDelegate,
150+ ),
151+ );
152+
153+ goRouter.pushNamed ('page0' );
154+
155+ goRouter.routerDelegate.addListener (expectAsync0 (() {}));
156+ final GoRouteMatch first = goRouter.routerDelegate.matches.first;
157+ final GoRouteMatch last = goRouter.routerDelegate.matches.last;
158+ goRouter.replaceNamed ('page1' );
159+ expect (goRouter.routerDelegate.matches.length, 2 );
160+ expect (
161+ goRouter.routerDelegate.matches.first,
162+ first,
163+ reason: 'The first match should still be in the list of matches' ,
164+ );
165+ expect (
166+ goRouter.routerDelegate.matches.last,
167+ isNot (last),
168+ reason: 'The last match should have been removed' ,
169+ );
170+ expect (
171+ goRouter.routerDelegate.matches.last,
172+ isA <GoRouteMatch >()
173+ .having (
174+ (GoRouteMatch match) => match.fullpath,
175+ 'match.fullpath' ,
176+ '/page-1' ,
177+ )
178+ .having (
179+ (GoRouteMatch match) => match.route.name,
180+ 'match.route.name' ,
181+ 'page1' ,
182+ ),
183+ reason: 'The new location should have been pushed' ,
184+ );
185+ },
186+ );
187+ });
188+
81189 testWidgets ('dispose unsubscribes from refreshListenable' ,
82190 (WidgetTester tester) async {
83191 final FakeRefreshListenable refreshListenable = FakeRefreshListenable ();
0 commit comments