import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:go_router/go_router.dart'; import 'package:krow_worker_app/core/transitions.dart'; const _detail = Key('detail'); GoRouter buildRouter() => GoRouter( routes: [ GoRoute( path: '/', builder: (_, _) => const Scaffold(body: Text('home')), ), GoRoute( path: '/detail', pageBuilder: (_, state) => Transitions.push( state, const Scaffold(body: SizedBox.expand(key: _detail)), ), ), GoRoute( path: '/replaced', pageBuilder: (_, state) => Transitions.fade( state, const Scaffold(body: SizedBox.expand(key: _detail)), ), ), ], ); void main() { group('Transitions.push', () { testWidgets('settles flush against the left edge', (tester) async { final router = buildRouter(); addTearDown(router.dispose); await tester.pumpWidget(MaterialApp.router(routerConfig: router)); router.push('/detail'); await tester.pumpAndSettle(); // The page is driven by two animations at once, and its resting state // comes from the one that is *not* running. Reversing that tween leaves // the screen parked off to the side, which is easy to miss in code and // obvious on a phone. final rect = tester.getRect(find.byKey(_detail)); expect(rect.left, 0); expect( rect.width, tester.view.physicalSize.width / tester.view.devicePixelRatio, ); }); testWidgets('enters from the right rather than appearing', (tester) async { final router = buildRouter(); addTearDown(router.dispose); await tester.pumpWidget(MaterialApp.router(routerConfig: router)); router.push('/detail'); await tester.pump(); await tester.pump(const Duration(milliseconds: 80)); expect(tester.getRect(find.byKey(_detail)).left, greaterThan(0)); }); }); group('Transitions.fade', () { testWidgets('settles flush and fully opaque', (tester) async { final router = buildRouter(); addTearDown(router.dispose); await tester.pumpWidget(MaterialApp.router(routerConfig: router)); router.push('/replaced'); await tester.pumpAndSettle(); expect(tester.getRect(find.byKey(_detail)).left, 0); final fade = tester.widget( find .ancestor( of: find.byKey(_detail), matching: find.byType(FadeTransition), ) .first, ); expect(fade.opacity.value, 1); }); }); }