@@ -3275,6 +3275,68 @@ void main() {
3275
3275
});
3276
3276
});
3277
3277
});
3278
+
3279
+ group ('of' , () {
3280
+ testWidgets (
3281
+ 'It should return the go router instance of the widget tree' ,
3282
+ (WidgetTester tester) async {
3283
+ const Key key = Key ('key' );
3284
+ final List <RouteBase > routes = < RouteBase > [
3285
+ GoRoute (
3286
+ path: '/' ,
3287
+ builder: (_, __) => const SizedBox (key: key),
3288
+ ),
3289
+ ];
3290
+
3291
+ final GoRouter router = await createRouter (routes, tester);
3292
+ final Element context = tester.element (find.byKey (key));
3293
+ final GoRouter foundRouter = GoRouter .of (context);
3294
+ expect (foundRouter, router);
3295
+ },
3296
+ );
3297
+
3298
+ testWidgets (
3299
+ 'It should throw if there is no go router in the widget tree' ,
3300
+ (WidgetTester tester) async {
3301
+ const Key key = Key ('key' );
3302
+ await tester.pumpWidget (const SizedBox (key: key));
3303
+
3304
+ final Element context = tester.element (find.byKey (key));
3305
+ expect (() => GoRouter .of (context), throwsA (anything));
3306
+ },
3307
+ );
3308
+ });
3309
+
3310
+ group ('maybeOf' , () {
3311
+ testWidgets (
3312
+ 'It should return the go router instance of the widget tree' ,
3313
+ (WidgetTester tester) async {
3314
+ const Key key = Key ('key' );
3315
+ final List <RouteBase > routes = < RouteBase > [
3316
+ GoRoute (
3317
+ path: '/' ,
3318
+ builder: (_, __) => const SizedBox (key: key),
3319
+ ),
3320
+ ];
3321
+
3322
+ final GoRouter router = await createRouter (routes, tester);
3323
+ final Element context = tester.element (find.byKey (key));
3324
+ final GoRouter ? foundRouter = GoRouter .maybeOf (context);
3325
+ expect (foundRouter, router);
3326
+ },
3327
+ );
3328
+
3329
+ testWidgets (
3330
+ 'It should return null if there is no go router in the widget tree' ,
3331
+ (WidgetTester tester) async {
3332
+ const Key key = Key ('key' );
3333
+ await tester.pumpWidget (const SizedBox (key: key));
3334
+
3335
+ final Element context = tester.element (find.byKey (key));
3336
+ expect (GoRouter .maybeOf (context), isNull);
3337
+ },
3338
+ );
3339
+ });
3278
3340
}
3279
3341
3280
3342
/// This allows a value of type T or T? to be treated as a value of type T?.
0 commit comments