Skip to content

Commit

Permalink
Merge pull request #410 from hpi-dhc/393/add-tests
Browse files Browse the repository at this point in the history
393/add tests
  • Loading branch information
Benjamin-Frost authored Jul 31, 2022
2 parents 5cbaeda + a75a513 commit 6d66a38
Show file tree
Hide file tree
Showing 23 changed files with 845 additions and 45 deletions.
40 changes: 21 additions & 19 deletions .github/workflows/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,9 @@ jobs:
- run: flutter pub run build_runner build
- run: flutter analyze

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: ${{ env.JAVA_VERSION }}
- uses: subosito/flutter-action@v1
with:
channel: ${{ env.FLUTTER_CHANNEL }}
flutter-version: ${{ env.FLUTTER_VERSION }}
- run: flutter pub get
- run: flutter pub run build_runner build
# TODO(Benjamin-Frost): Enable this when we have tests
# - run: flutter test

build-android:
name: Build Android APK
needs: [lint, test]
needs: [lint]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -68,7 +51,7 @@ jobs:

build-ios:
name: Build iOS Package
needs: [lint, test]
needs: [lint]
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -82,3 +65,22 @@ jobs:
- run: flutter pub get
- run: flutter pub run build_runner build
- run: flutter build ios --release --no-codesign

test:
name: Test
needs: [lint, build-android, build-ios]
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
with:
flutter-version: ${{ env.FLUTTER_VERSION }}
channel: ${{ env.FLUTTER_CHANNEL }}
- run: flutter pub get
- run: flutter pub run build_runner build
- run: open -a Simulator.app
- run: flutter test integration_test -d iPhone --coverage
- uses: codecov/codecov-action@v3
with:
flags: app
directory: app/coverage
3 changes: 3 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ app.*.map.json

# environment file
.env

# coverage files
coverage/
53 changes: 53 additions & 0 deletions app/integration_test/faq_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:app/common/module.dart';
import 'package:app/faq/constants.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

void main() {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();

binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.onlyPumps;

group('integration test for the faq page', () {
final appRouter = AppRouter();
final faqWidget = MaterialApp.router(
routeInformationParser: appRouter.defaultRouteParser(),
routerDelegate: appRouter.delegate(
initialDeepLink: 'main/faq',
),
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [Locale('en', '')],
);

testWidgets('All questions are loaded and expansion tiles can be open',
(tester) async {
await tester.pumpWidget(faqWidget);
await tester.pumpAndSettle();

expect(
find
.descendant(
of: find.byKey(ValueKey('questions_column')),
matching: find.byType(ExpansionTile),
)
.evaluate()
.length,
faqList.length,
);

expect(find.text(faqList[0].question), findsOneWidget);
expect(find.text(faqList[0].answer), findsNothing);

await tester.tap(find.byType(ExpansionTile).first);
await tester.pumpAndSettle();

expect(find.text(faqList[0].answer), findsOneWidget);
});
});
}
111 changes: 111 additions & 0 deletions app/integration_test/login_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import 'package:app/common/module.dart';
import 'package:app/login/models/lab.dart';
import 'package:app/login/module.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:mocktail/mocktail.dart';

class MockLoginCubit extends MockCubit<LoginPageState>
implements LoginPageCubit {}

void main() {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
final mockLoginCubit = MockLoginCubit();

binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.onlyPumps;

group('integration tests for the login page', () {
testWidgets('test loading state', (tester) async {
when(() => mockLoginCubit.state).thenReturn(
LoginPageState.loadingUserData(),
);

await tester.pumpWidget(
MaterialApp(
home: LoginPage(cubit: mockLoginCubit),
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [Locale('en', '')],
),
);

expect(find.byType(CircularProgressIndicator), findsOneWidget);
});

testWidgets('test error state', (tester) async {
when(() => mockLoginCubit.state).thenReturn(
LoginPageState.error('Some error'),
);

await tester.pumpWidget(
MaterialApp(
home: LoginPage(cubit: mockLoginCubit),
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [Locale('en', '')],
),
);

expect(find.byIcon(Icons.error_outline_rounded), findsOneWidget);

expect(find.text('Some error'), findsOneWidget);
});

testWidgets('test loaded state', (tester) async {
when(() => mockLoginCubit.state).thenReturn(
LoginPageState.loadedUserData(),
);

await tester.pumpWidget(
MaterialApp(
home: Scaffold(body: LoginPage(cubit: mockLoginCubit)),
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [Locale('en', '')],
),
);

final BuildContext context = tester.element(find.byType(Scaffold).first);

expect(find.byIcon(Icons.check_circle_outline), findsOneWidget);
expect(find.text(context.l10n.auth_success), findsOneWidget);
});

testWidgets('test initial state', (tester) async {
when(() => mockLoginCubit.state).thenReturn(
LoginPageState.initial(),
);

await tester.pumpWidget(
MaterialApp(
home: Scaffold(body: LoginPage(cubit: mockLoginCubit)),
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [Locale('en', '')],
),
);

expect(find.byType(DropdownButtonHideUnderline), findsOneWidget);

expect(find.byType(DropdownMenuItem<String>), findsNWidgets(labs.length));
});
});
}
53 changes: 53 additions & 0 deletions app/integration_test/main_page_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:app/common/module.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

void main() {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();

binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.onlyPumps;

group('test the main page', () {
testWidgets('test that tabs change pages', (tester) async {
final appRouter = AppRouter();
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: appRouter.defaultRouteParser(),
routerDelegate: appRouter.delegate(
initialDeepLink: 'main',
),
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [Locale('en', '')],
),
);

await tester.pumpAndSettle();

final BuildContext context = tester.element(find.byType(Scaffold).first);

expect(find.text(context.l10n.general_appName), findsOneWidget);

expect(find.byIcon(Icons.search), findsOneWidget);
expect(find.byIcon(Icons.assessment), findsOneWidget);
expect(find.byIcon(Icons.lightbulb), findsOneWidget);
expect(find.byIcon(Icons.more_horiz), findsOneWidget);

// ignore: omit_local_variable_types
BottomNavigationBar bar = tester.widget(find.byType(BottomNavigationBar));

expect(bar.currentIndex, 0);

await tester.tap(find.byIcon(Icons.lightbulb));
await tester.pumpAndSettle();

bar = tester.widget(find.byType(BottomNavigationBar));
expect(bar.currentIndex, 2);
});
});
}
Loading

0 comments on commit 6d66a38

Please sign in to comment.