Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
chore: migrate to checks for expections
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Aug 15, 2024
1 parent dbb0524 commit d1eb20c
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 49 deletions.
8 changes: 8 additions & 0 deletions packages/app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.3"
checks:
dependency: "direct main"
description:
name: checks
sha256: aad431b45a8ae2fa26db8c22e385b9cdec73f72986a1d9d9f2017f4c39ecf5c9
url: "https://pub.dev"
source: hosted
version: "0.3.0"
ci:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions packages/app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies:
build_version: ^2.1.1
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
checks: ^0.3.0
cupertino_icons: ^1.0.8 # Only required if you use Cupertino (iOS style) icons
flutter:
sdk: flutter
Expand Down
10 changes: 6 additions & 4 deletions packages/app/test/helpers/accessibility.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:checks/checks.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import 'checks.dart';
import 'pump_app.dart';

void testAccessibilityGuidelines(
Expand All @@ -13,28 +15,28 @@ void testAccessibilityGuidelines(
await tester.pumpApp(widget, overrides: overrides);

final handle = tester.ensureSemantics();
await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
await check(tester).meetsGuideline(androidTapTargetGuideline);
handle.dispose();
});
testWidgets('on iOS.', (tester) async {
await tester.pumpApp(widget, overrides: overrides);

final handle = tester.ensureSemantics();
await expectLater(tester, meetsGuideline(iOSTapTargetGuideline));
await check(tester).meetsGuideline(iOSTapTargetGuideline);
handle.dispose();
});
testWidgets('according to the WCAG.', (tester) async {
await tester.pumpApp(widget, overrides: overrides);

final handle = tester.ensureSemantics();
await expectLater(tester, meetsGuideline(textContrastGuideline));
await check(tester).meetsGuideline(textContrastGuideline);
handle.dispose();
});
testWidgets('with regard to labeling buttons.', (tester) async {
await tester.pumpApp(widget, overrides: overrides);

final handle = tester.ensureSemantics();
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
await check(tester).meetsGuideline(labeledTapTargetGuideline);
handle.dispose();
});
});
Expand Down
52 changes: 52 additions & 0 deletions packages/app/test/helpers/checks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:checks/context.dart';
import 'package:flutter_test/flutter_test.dart';

extension AccessibilityExpect on Subject<WidgetTester> {
Future<void> meetsGuideline(AccessibilityGuideline guideline) async {
await context.expectAsync(() => [guideline.description], (tester) async {
final Evaluation result = await guideline.evaluate(tester);
if (result.passed) {
return null;
}
return Rejection(which: [result.reason!]);
});
}
}

extension WidgetFinderExpect on Subject<Finder> {
Future<void> findsOne() async {
await findsExactly(1);
}

Future<void> findsExactly(int count) async {
_findsWidgets(0, count);
}

Future<void> _findsWidgets(int? min, int? max) async {
assert(min != null || max != null);
assert(min == null || max == null || min <= max);

await context.expectAsync(() => ['finds between $min and $max widgets'],
(finder) async {
int count = 0;
final Iterator<dynamic> iterator = finder.evaluate().iterator;
if (min != null) {
while (count < min && iterator.moveNext()) {
count += 1;
}
if (count < min) {
return Rejection(which: ['found less than $min widgets']);
}
}
if (max != null) {
while (count <= max && iterator.moveNext()) {
count += 1;
}
if (count > max) {
return Rejection(which: ['found more than $max widgets']);
}
}
return null;
});
}
}
11 changes: 7 additions & 4 deletions packages/app/test/src/app/app_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:checks/checks.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -6,6 +7,7 @@ import 'package:our_democracy/src/features/settings/application/settings_service
import 'package:our_democracy/src/features/settings/data/preferences_repository.dart';
import 'package:our_democracy/src/l10n/l10n.dart';

import '../../helpers/checks.dart';
import '../../helpers/mocks.dart';

extension _WidgetTesterX on WidgetTester {
Expand All @@ -27,25 +29,26 @@ extension _WidgetTesterX on WidgetTester {
void main() {
testWidgets('MyApp should build MaterialApp.router', (tester) async {
await tester.pumpWidgetPage();
expect(find.byType(MaterialApp), findsOneWidget);
check(find.byType(MaterialApp)).findsOne();
});

testWidgets('MyApp should have correct restorationScopeId', (tester) async {
await tester.pumpWidgetPage();
final app = tester.widget<MaterialApp>(find.byType(MaterialApp));
expect(app.restorationScopeId, 'app');
check(app.restorationScopeId).equals('app');
});

testWidgets('MyApp should have correct localizationsDelegates',
(tester) async {
await tester.pumpWidgetPage();
final app = tester.widget<MaterialApp>(find.byType(MaterialApp));
expect(app.localizationsDelegates, AppLocalizations.localizationsDelegates);
check(app.localizationsDelegates)
.equals(AppLocalizations.localizationsDelegates);
});

testWidgets('MyApp should have correct supportedLocales', (tester) async {
await tester.pumpWidgetPage();
final app = tester.widget<MaterialApp>(find.byType(MaterialApp));
expect(app.supportedLocales, AppLocalizations.supportedLocales);
check(app.supportedLocales).equals(AppLocalizations.supportedLocales);
});
}
15 changes: 6 additions & 9 deletions packages/app/test/src/app/bootstrap_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:checks/checks.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
Expand All @@ -20,15 +21,11 @@ void main() {

test('main does not throw', () async {
const app = MyApp();

await expectLater(
app.bootstrap(
(
runApp: (_) {},
getSharedPreferences: getSharedPreferences,
),
),
completes,
final env = (
runApp: (_) {},
getSharedPreferences: getSharedPreferences,
);

check(app.bootstrap(env)).completes();
});
}
15 changes: 8 additions & 7 deletions packages/app/test/src/app/router_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:auto_route/auto_route.dart';
import 'package:checks/checks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:our_democracy/src/app/router.dart';

Expand All @@ -10,37 +11,37 @@ void main() {
test(
'defaultRouteType is a RouteType.material.',
() {
expect(tested.defaultRouteType, isInstanceOf<MaterialRouteType>());
check(tested.defaultRouteType).isA<MaterialRouteType>();
},
);

test('should contain the correct number of routes.', () {
expect(tested.routes.length, equals(2));
check(tested.routes.length).equals(2);
});
});

group('path', () {
test('should be correct for WrapperRoute.', () {
final wrapperRoute = tested.routes[0];
expect(wrapperRoute.path, equals('/'));
check(wrapperRoute.path).equals('/');
});
test('should be correct for SampleItemListRoute.', () {
final sampleItemListRoute =
tested.routes[0].children?.routes.toList()[0];
expect(sampleItemListRoute?.path, equals(''));
check(sampleItemListRoute?.path).equals('');
});
test('should be correct for SampleItemDetailsRoute.', () {
final sampleItemDetailsRoute =
tested.routes[0].children?.routes.toList()[1];
expect(sampleItemDetailsRoute?.path, equals('sample-item'));
check(sampleItemDetailsRoute?.path).equals('sample-item');
});
test('should be correct for SettingsRoute.', () {
final settingsRoute = tested.routes[0].children?.routes.toList()[2];
expect(settingsRoute?.path, equals('settings'));
check(settingsRoute?.path).equals('settings');
});
test('should redirect on 404', () {
final redirectRoute = tested.routes[1];
expect(redirectRoute.path, equals('/*'));
check(redirectRoute.path).equals('/*');
});
});
});
Expand Down
14 changes: 8 additions & 6 deletions packages/app/test/src/app/wrapper_page_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:checks/checks.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -7,6 +8,7 @@ import 'package:our_democracy/src/l10n/l10n.dart';
import 'package:our_democracy/src/utils/design.dart';
import 'package:our_democracy/src/utils/router.dart';

import '../../helpers/checks.dart';
import '../../helpers/riverpod.dart';

extension _WidgetTesterX on WidgetTester {
Expand Down Expand Up @@ -35,8 +37,8 @@ extension _WidgetTesterX on WidgetTester {
const WrapperRoute(),
]);
await pumpAndSettle();
expect(router.urlState.url, equals('/'));
expect(find.byType(WrapperPage), findsOneWidget);
check(router.urlState.url).equals('/');
check(find.byType(WrapperPage)).findsOne();
}
}

Expand All @@ -47,28 +49,28 @@ void main() {
await tester.pumpWidgetPage();

final handle = tester.ensureSemantics();
await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
await check(tester).meetsGuideline(androidTapTargetGuideline);
handle.dispose();
});
testWidgets('on iOS.', (tester) async {
await tester.pumpWidgetPage();

final handle = tester.ensureSemantics();
await expectLater(tester, meetsGuideline(iOSTapTargetGuideline));
await check(tester).meetsGuideline(iOSTapTargetGuideline);
handle.dispose();
});
testWidgets('according to the WCAG.', (tester) async {
await tester.pumpWidgetPage();

final handle = tester.ensureSemantics();
await expectLater(tester, meetsGuideline(textContrastGuideline));
await check(tester).meetsGuideline(textContrastGuideline);
handle.dispose();
});
testWidgets('with regard to labeling buttons.', (tester) async {
await tester.pumpWidgetPage();

final handle = tester.ensureSemantics();
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
await check(tester).meetsGuideline(labeledTapTargetGuideline);
handle.dispose();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:checks/checks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:our_democracy/src/features/sample/application/sample_items_service.dart';
Expand All @@ -8,7 +9,7 @@ void main() {
final container = ProviderContainer();

final model = await container.read(sampleItemsServiceProvider.future);
expect(model.items.length, 3);
check(model.items.length).equals(3);
});
});
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:checks/checks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:our_democracy/src/features/sample/domain/sample_item_entity.dart';

void main() {
test('SampleItemEntity should correctly wrap an int', () {
const entity = SampleItemEntity(1);
expect(entity, isA<int>());
expect(entity, equals(1));
check(entity).isA<int>();
check(entity.id).equals(1);
});
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:checks/checks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:our_democracy/src/features/sample/domain/sample_items_model.dart';

Expand All @@ -11,7 +12,7 @@ void main() {
final newModel = model.copyWith(items: []);

// Assert
expect(newModel, equals(model));
check(newModel).equals(model);
});
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:checks/checks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:our_democracy/src/features/sample/presentation/items/sample_item_details_page.dart';

import '../../../../../helpers/accessibility.dart';
import '../../../../../helpers/checks.dart';
import '../../../../../helpers/pump_app.dart';

void main() {
Expand All @@ -15,7 +17,7 @@ void main() {
await tester.pumpApp(widget);

// Verify that the widget displays the expected information
expect(find.text('More Information Here'), findsOneWidget);
check(find.text('More Information Here')).findsOne();
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:checks/checks.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:our_democracy/src/features/sample/presentation/items/sample_items_list_page.dart';

import '../../../../../helpers/accessibility.dart';
import '../../../../../helpers/checks.dart';
import '../../../../../helpers/pump_app.dart';

void main() {
Expand All @@ -16,7 +18,7 @@ void main() {
await tester.pumpApp(const Material(child: widget));

// Verify that the widget displays the expected information
expect(find.text('SampleItem 1'), findsOneWidget);
check(find.text('SampleItem 1')).findsOne();
});

testAccessibilityGuidelines(const Material(child: SampleItemsListPage()));
Expand Down
Loading

0 comments on commit d1eb20c

Please sign in to comment.