Skip to content

Instantly share code, notes, and snippets.

@tolo
Last active June 18, 2024 14:32
Show Gist options
  • Select an option

  • Save tolo/922fd838cdea30b17121533b0012eda4 to your computer and use it in GitHub Desktop.

Select an option

Save tolo/922fd838cdea30b17121533b0012eda4 to your computer and use it in GitHub Desktop.

Revisions

  1. tolo revised this gist Jun 18, 2024. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion routing_config_test.dart
    Original file line number Diff line number Diff line change
    @@ -61,10 +61,14 @@ void main() {
    GlobalKey<_StatefulTestState>(debugLabel: 'testState');
    final GlobalKey<NavigatorState> rootNavigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'root');
    final GlobalKey<NavigatorState> shellNavigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'shell');

    final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>(
    RoutingConfig(
    routes: <RouteBase>[
    ShellRoute(
    navigatorKey: shellNavigatorKey,
    routes: <RouteBase>[
    GoRoute(path: '/', builder: (_, __) => const Text('home')),
    ],
    @@ -74,7 +78,7 @@ void main() {
    ),
    );
    addTearDown(config.dispose);
    await createRouterWithRoutingConfig(
    await createRouterWithRoutingConfig(
    navigatorKey: rootNavigatorKey,
    config,
    tester,
    @@ -86,6 +90,7 @@ void main() {
    config.value = RoutingConfig(
    routes: <RouteBase>[
    ShellRoute(
    navigatorKey: shellNavigatorKey,
    routes: <RouteBase>[
    GoRoute(path: '/', builder: (_, __) => const Text('home')),
    GoRoute(path: '/abc', builder: (_, __) => const Text('/abc')),
  2. tolo created this gist Jun 18, 2024.
    126 changes: 126 additions & 0 deletions routing_config_test.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,126 @@
    // Copyright 2013 The Flutter Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_test/flutter_test.dart';
    import 'package:go_router/go_router.dart';

    import 'test_helpers.dart';

    void main() {
    // ...


    /// THIS WORKS
    testWidgets('routing config works after routing changes case 3',
    (WidgetTester tester) async {
    final GlobalKey<_StatefulTestState> key =
    GlobalKey<_StatefulTestState>(debugLabel: 'testState');
    final GlobalKey<NavigatorState> rootNavigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'root');

    final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>(
    RoutingConfig(
    routes: <RouteBase>[
    GoRoute(
    path: '/',
    builder: (_, __) =>
    StatefulTest(key: key, child: const Text('home'))),
    ],
    ),
    );
    addTearDown(config.dispose);
    await createRouterWithRoutingConfig(
    navigatorKey: rootNavigatorKey,
    config,
    tester,
    errorBuilder: (_, __) => const Text('error'),
    );
    expect(find.text('home'), findsOneWidget);
    key.currentState!.value = 1;

    config.value = RoutingConfig(
    routes: <RouteBase>[
    GoRoute(
    path: '/',
    builder: (_, __) =>
    StatefulTest(key: key, child: const Text('home'))),
    GoRoute(path: '/abc', builder: (_, __) => const Text('/abc')),
    ],
    );
    await tester.pumpAndSettle();
    expect(key.currentState!.value == 1, isTrue);
    });

    /// THIS CRASHES (duplicate GlobalKey)
    testWidgets('routing config works with shell route',
    (WidgetTester tester) async {
    final GlobalKey<_StatefulTestState> key =
    GlobalKey<_StatefulTestState>(debugLabel: 'testState');
    final GlobalKey<NavigatorState> rootNavigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'root');
    final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>(
    RoutingConfig(
    routes: <RouteBase>[
    ShellRoute(
    routes: <RouteBase>[
    GoRoute(path: '/', builder: (_, __) => const Text('home')),
    ],
    builder: (_, __, Widget widget) =>
    StatefulTest(key: key, child: widget)),
    ],
    ),
    );
    addTearDown(config.dispose);
    await createRouterWithRoutingConfig(
    navigatorKey: rootNavigatorKey,
    config,
    tester,
    errorBuilder: (_, __) => const Text('error'),
    );
    expect(find.text('home'), findsOneWidget);
    key.currentState!.value = 1;

    config.value = RoutingConfig(
    routes: <RouteBase>[
    ShellRoute(
    routes: <RouteBase>[
    GoRoute(path: '/', builder: (_, __) => const Text('home')),
    GoRoute(path: '/abc', builder: (_, __) => const Text('/abc')),
    ],
    builder: (_, __, Widget widget) =>
    StatefulTest(key: key, child: widget)),
    ],
    );
    await tester.pumpAndSettle();

    expect(key.currentState!.value == 1, isTrue);
    });

    // ...
    }

    class StatefulTest extends StatefulWidget {
    const StatefulTest({super.key, required this.child});

    final Widget child;

    @override
    State<StatefulWidget> createState() => _StatefulTestState();
    }

    class _StatefulTestState extends State<StatefulTest> {
    int value = 0;

    @override
    Widget build(BuildContext context) {
    return Column(
    children: <Widget>[
    widget.child,
    Text('State: $value'),
    ],
    );
    }
    }