wt1.1a flutter_bloc
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import 'package:aitrainer_app/bloc/account/account_bloc.dart';
|
||||
import 'package:aitrainer_app/repository/customer_repository.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:test/test.dart' as test;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
class MockCustomerRepository extends Mock implements CustomerRepository {
|
||||
|
||||
}
|
||||
|
||||
void main() {
|
||||
MockCustomerRepository customerRepository;
|
||||
AccountBloc accountBloc;
|
||||
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
test.setUp(() {
|
||||
customerRepository = MockCustomerRepository();
|
||||
accountBloc = AccountBloc(customerRepository: customerRepository);
|
||||
});
|
||||
|
||||
test.tearDown(() {
|
||||
accountBloc?.close();
|
||||
});
|
||||
|
||||
test.test('initial state is correct', () {
|
||||
expect(accountBloc.state, AccountInitial());
|
||||
});
|
||||
|
||||
group('Account', () {
|
||||
test.test(
|
||||
'emits [loading, logged in] when the customer clicked login',
|
||||
() {
|
||||
final expectedResponse = [
|
||||
AccountLoading(),
|
||||
AccountLoggedIn(),
|
||||
];
|
||||
|
||||
//verify(accountBloc.customerRepository.customer == null);
|
||||
|
||||
expectLater(
|
||||
accountBloc, emitsInOrder(expectedResponse),
|
||||
);
|
||||
|
||||
accountBloc.add(AccountLogin());
|
||||
});
|
||||
});
|
||||
|
||||
test.test(
|
||||
'emits [loading, logged out] when the customer clicked logout',
|
||||
() {
|
||||
final expectedResponse = [
|
||||
AccountLoading(),
|
||||
AccountLoggedOut(),
|
||||
];
|
||||
|
||||
expectLater(
|
||||
accountBloc, emitsInOrder(expectedResponse),
|
||||
);
|
||||
|
||||
accountBloc.add(AccountLogout());
|
||||
});
|
||||
|
||||
test.test(
|
||||
'emits [loading, logged out] when the customer data changed',
|
||||
() {
|
||||
final expectedResponse = [
|
||||
AccountLoading(),
|
||||
AccountReady(),
|
||||
];
|
||||
|
||||
expectLater(
|
||||
accountBloc, emitsInOrder(expectedResponse),
|
||||
);
|
||||
|
||||
accountBloc.add(AccountChangeCustomer());
|
||||
});
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
// This is a basic Flutter widget test.
|
||||
//
|
||||
// To perform an interaction with a widget in your test, use the WidgetTester
|
||||
// utility that Flutter provides. For example, you can send tap and scroll
|
||||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||
// tree, read text, and verify that the values of widget properties are correct.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'package:aitrainer_app/main.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
||||
// Build our app and trigger a frame.
|
||||
await tester.pumpWidget(AitrainerApp());
|
||||
|
||||
// Verify that our counter starts at 0.
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
expect(find.text('1'), findsNothing);
|
||||
|
||||
// Tap the '+' icon and trigger a frame.
|
||||
await tester.tap(find.byIcon(Icons.add));
|
||||
await tester.pump();
|
||||
|
||||
// Verify that our counter has incremented.
|
||||
expect(find.text('0'), findsNothing);
|
||||
expect(find.text('1'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// This is a basic Flutter widget test.
|
||||
//
|
||||
// To perform an interaction with a widget in your test, use the WidgetTester
|
||||
// utility that Flutter provides. For example, you can send tap and scroll
|
||||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||
// tree, read text, and verify that the values of widget properties are correct.
|
||||
|
||||
|
||||
import 'package:aitrainer_app/bloc/login_form_bloc.dart';
|
||||
import 'package:aitrainer_app/library_keys.dart';
|
||||
import 'package:aitrainer_app/localization/app_localization.dart';
|
||||
import 'package:aitrainer_app/model/user.dart';
|
||||
import 'package:aitrainer_app/repository/user_repository.dart';
|
||||
import 'package:aitrainer_app/util/common.dart';
|
||||
import 'package:aitrainer_app/view/login.dart';
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
|
||||
class MockUserRepository extends Mock implements UserRepository {
|
||||
final User user = User();
|
||||
|
||||
setEmail(String email) {
|
||||
this.user.email = email;
|
||||
}
|
||||
|
||||
setPassword(String password) {
|
||||
this.user.password = password;
|
||||
}
|
||||
|
||||
Future<void> getUser() async {
|
||||
if ( this.user.email == "sw@andio.biz" && this.user.password == "PalKataPeter1") {
|
||||
//OK
|
||||
} else {
|
||||
throw new Exception("Customer does not exist");
|
||||
}
|
||||
}
|
||||
}
|
||||
class MockLoginBloc extends MockBloc<FormBlocEvent>
|
||||
implements LoginFormBloc {}
|
||||
|
||||
void main() {
|
||||
group('LoginScreen', () {
|
||||
MockLoginBloc loginBloc;
|
||||
MockUserRepository userRepository;
|
||||
Widget loginWidget;
|
||||
|
||||
setUp(() {
|
||||
loginBloc = MockLoginBloc();
|
||||
userRepository = MockUserRepository();
|
||||
|
||||
loginWidget =
|
||||
MaterialApp(
|
||||
home: LoginPage(),
|
||||
localizationsDelegates: [
|
||||
AppLocalizationsDelegate(isTest: true),
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
routes: {
|
||||
'home': (context) => LoginPage(),
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
loginBloc.close();
|
||||
});
|
||||
|
||||
testWidgets('Display login page', (WidgetTester tester) async {
|
||||
// Build our app and trigger a frame.
|
||||
await tester.pumpWidget(loginWidget);
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(LibraryKeys.loginEmailField), findsOneWidget);
|
||||
expect(find.byKey(LibraryKeys.loginPasswordField), findsOneWidget);
|
||||
expect(find.byKey(LibraryKeys.loginOKButton), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Add zero length email', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(loginWidget);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
var emailField = find.byKey(LibraryKeys.loginEmailField);
|
||||
var pwdField = find.byKey(LibraryKeys.loginPasswordField);
|
||||
var okButton = find.byKey(LibraryKeys.loginOKButton);
|
||||
expect(emailField, findsOneWidget);
|
||||
expect(pwdField, findsOneWidget);
|
||||
expect(okButton, findsOneWidget);
|
||||
|
||||
await tester.tap(emailField);
|
||||
await tester.enterText(emailField, "sw");
|
||||
await tester.enterText(pwdField, "123");
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(okButton);
|
||||
await tester.pump(const Duration(milliseconds: 500)); // add delay
|
||||
final emailErrorFinder = find.text(Common.EMAIL_ERROR);
|
||||
expect(emailErrorFinder, findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user