Skip to content

Commit

Permalink
test: introduce test that can route our pages
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Oct 27, 2023
1 parent f47cd12 commit ae04685
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 4 deletions.
13 changes: 13 additions & 0 deletions lib/pages/test_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

@RoutePage()
class SailTestPage extends StatelessWidget {
final Widget child;
const SailTestPage({super.key, required this.child});

@override
Widget build(BuildContext context) {
return child;
}
}
7 changes: 6 additions & 1 deletion lib/routing/router.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:auto_route/auto_route.dart';
import 'package:sidesail/pages/home_page.dart';
import 'package:flutter/widgets.dart';
import 'package:sidesail/routing/routes_export.dart';

part 'router.gr.dart';

Expand All @@ -25,5 +26,9 @@ class AppRouter extends _$AppRouter {
page: HomeRoute.page,
initial: true,
),

/// This route is used in tests so that we can pump a widget into a route
/// and use the real router for our test
AutoRoute(page: SailTestRoute.page),
];
}
49 changes: 48 additions & 1 deletion lib/routing/router.gr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/routing/routes_export.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// all routes should be exported from this file!

export 'package:sidesail/pages/home_page.dart';
export 'package:sidesail/pages/test_page.dart';
18 changes: 18 additions & 0 deletions test/mocks/storage_mock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:sidesail/storage/client_settings.dart';

class MockStore implements KeyValueStore {
final _db = {};

@override
Future<String?> getString(String key) async => _db[key];

@override
Future<void> setString(String key, String value) async {
_db[key] = value;
}

@override
Future<void> delete(String key) async {
_db[key] = null;
}
}
66 changes: 66 additions & 0 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:sidesail/app.dart';
import 'package:sidesail/routing/router.dart';
import 'package:sidesail/storage/client_settings.dart';

import 'mocks/storage_mock.dart';

Future<void> _setDeviceSize() async {
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
await binding.setSurfaceSize(const Size(1200, 720));
}

Future<void> loadFonts() async {
final sourceCodePro = rootBundle.load('fonts/SourceCodePro-Regular.ttf');
final fontLoader = FontLoader('SourceCodePro')..addFont(sourceCodePro);
await fontLoader.load();
}

extension TestExtension on WidgetTester {
Future<void> pumpSailPage(
Widget child, {
bool requireIntensionKyc = false,
bool requireKycLevel2 = false,
}) async {
await _setDeviceSize();
await loadFonts();
await registerTestDependencies();

await pumpWidget(
SailApp(
builder: (context, router) {
final appRouter = GetIt.I.get<AppRouter>();

return MaterialApp.router(
routerDelegate: appRouter.delegate(
initialRoutes: [SailTestRoute(child: child)],
),
routeInformationParser: appRouter.defaultRouteParser(),
title: 'SideSail',
theme: ThemeData(
fontFamily: 'SourceCodePro',
),
);
},
),
);
await pumpAndSettle();
}
}

Future<void> registerTestDependencies() async {
if (!GetIt.I.isRegistered<AppRouter>()) {
GetIt.I.registerLazySingleton<AppRouter>(
() => AppRouter(),
);
}

if (!GetIt.I.isRegistered<ClientSettings>()) {
GetIt.I.registerLazySingleton<ClientSettings>(
() => ClientSettings(store: MockStore()),
);
}
}
8 changes: 6 additions & 2 deletions test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter_test/flutter_test.dart';
import 'package:sidesail/pages/home_page.dart';

import 'package:sidesail/main.dart';
import 'test_utils.dart';

void main() {
testWidgets('RPC submit smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpSailPage(
const HomePage(),
);

// Verify that there's a submit button.
expect(find.text('Submit'), findsOneWidget);
expect(find.text('SideSail'), findsOneWidget);

// TODO: something more meaningful. Would have to mock the RPC interfaces
});
Expand Down

0 comments on commit ae04685

Please sign in to comment.