-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update unit, widget, integration tests
- Loading branch information
Showing
11 changed files
with
284 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
ecommerce_app/test/src/features/checkout/application/fake_checkout_service_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'package:ecommerce_app/src/features/authentication/data/fake_auth_repository.dart'; | ||
import 'package:ecommerce_app/src/features/authentication/domain/app_user.dart'; | ||
import 'package:ecommerce_app/src/features/cart/data/remote/remote_cart_repository.dart'; | ||
import 'package:ecommerce_app/src/features/cart/domain/cart.dart'; | ||
import 'package:ecommerce_app/src/features/checkout/application/fake_checkout_service.dart'; | ||
import 'package:ecommerce_app/src/features/orders/data/fake_orders_repository.dart'; | ||
import 'package:ecommerce_app/src/features/orders/domain/order.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../../mocks.dart'; | ||
|
||
void main() { | ||
const testUser = AppUser(uid: 'abc'); | ||
setUpAll(() { | ||
// needed for MockOrdersRepository | ||
registerFallbackValue(Order( | ||
id: '1', | ||
userId: testUser.uid, | ||
items: {'1': 1}, | ||
orderStatus: OrderStatus.confirmed, | ||
orderDate: DateTime(2022, 7, 13), | ||
total: 15, | ||
)); | ||
// needed for MockRemoteCartRepository | ||
registerFallbackValue(const Cart()); | ||
}); | ||
|
||
late MockAuthRepository authRepository; | ||
late MockRemoteCartRepository remoteCartRepository; | ||
late MockOrdersRepository ordersRepository; | ||
setUp(() { | ||
authRepository = MockAuthRepository(); | ||
remoteCartRepository = MockRemoteCartRepository(); | ||
ordersRepository = MockOrdersRepository(); | ||
}); | ||
|
||
FakeCheckoutService makeCheckoutService() { | ||
final container = ProviderContainer( | ||
overrides: [ | ||
authRepositoryProvider.overrideWithValue(authRepository), | ||
remoteCartRepositoryProvider.overrideWithValue(remoteCartRepository), | ||
ordersRepositoryProvider.overrideWithValue(ordersRepository), | ||
], | ||
); | ||
addTearDown(container.dispose); | ||
return container.read(checkoutServiceProvider); | ||
} | ||
|
||
group('placeOrder', () { | ||
test('null user, throws', () async { | ||
// setup | ||
when(() => authRepository.currentUser).thenReturn(null); | ||
final checkoutService = makeCheckoutService(); | ||
// run | ||
expect(checkoutService.placeOrder, throwsA(isA<TypeError>())); | ||
}); | ||
|
||
test('empty cart, throws', () async { | ||
// setup | ||
when(() => authRepository.currentUser).thenReturn(testUser); | ||
when(() => remoteCartRepository.fetchCart(testUser.uid)).thenAnswer( | ||
(_) => Future.value(const Cart()), | ||
); | ||
final checkoutService = makeCheckoutService(); | ||
// run | ||
expect(checkoutService.placeOrder, throwsStateError); | ||
}); | ||
|
||
test('non-empty cart, creates order', () async { | ||
// setup | ||
when(() => authRepository.currentUser).thenReturn(testUser); | ||
when(() => remoteCartRepository.fetchCart(testUser.uid)).thenAnswer( | ||
(_) => Future.value(const Cart({'1': 1})), | ||
); | ||
when(() => ordersRepository.addOrder(testUser.uid, any())).thenAnswer( | ||
(_) => Future.value(), | ||
); | ||
when(() => remoteCartRepository.setCart(testUser.uid, const Cart())) | ||
.thenAnswer( | ||
(_) => Future.value(), | ||
); | ||
final checkoutService = makeCheckoutService(); | ||
// run | ||
await checkoutService.placeOrder(); | ||
// verify | ||
verify(() => ordersRepository.addOrder(testUser.uid, any())).called(1); | ||
verify(() => remoteCartRepository.setCart(testUser.uid, const Cart())); | ||
}); | ||
}); | ||
} |
26 changes: 26 additions & 0 deletions
26
ecommerce_app/test/src/features/checkout/checkout_robot.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
class CheckoutRobot { | ||
CheckoutRobot(this.tester); | ||
final WidgetTester tester; | ||
|
||
Future<void> startCheckout() async { | ||
final finder = find.text('Checkout'); | ||
expect(finder, findsOneWidget); | ||
await tester.tap(finder); | ||
await tester.pumpAndSettle(); | ||
} | ||
|
||
// payment | ||
Future<void> startPayment() async { | ||
final finder = find.text('Pay'); | ||
expect(finder, findsOneWidget); | ||
await tester.tap(finder); | ||
await tester.pumpAndSettle(); | ||
} | ||
|
||
void expectPayButtonFound() { | ||
final finder = find.text('Pay'); | ||
expect(finder, findsOneWidget); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...rce_app/test/src/features/checkout/presentation/checkout_screen/checkout_screen_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import '../../../../robot.dart'; | ||
|
||
void main() { | ||
testWidgets('checkout when not previously signed in', (tester) async { | ||
final r = Robot(tester); | ||
await r.pumpMyApp(); | ||
// add a product and start checkout | ||
await r.products.selectProduct(); | ||
await r.cart.addToCart(); | ||
await r.cart.openCart(); | ||
await r.checkout.startCheckout(); | ||
// sign in from checkout screen | ||
r.auth.expectEmailAndPasswordFieldsFound(); | ||
await r.auth.signInWithEmailAndPassword(); | ||
// check that we move to the payment page | ||
r.checkout.expectPayButtonFound(); | ||
}); | ||
|
||
testWidgets('checkout when previously signed in', (tester) async { | ||
final r = Robot(tester); | ||
await r.pumpMyApp(); | ||
// sign in first | ||
await r.auth.openEmailPasswordSignInScreen(); | ||
await r.auth.signInWithEmailAndPassword(); | ||
// then add a product and start checkout | ||
await r.products.selectProduct(); | ||
await r.cart.addToCart(); | ||
await r.cart.openCart(); | ||
await r.checkout.startCheckout(); | ||
// expect that we see the payment page right away | ||
r.checkout.expectPayButtonFound(); | ||
}); | ||
} |
53 changes: 53 additions & 0 deletions
53
...e_app/test/src/features/checkout/presentation/payment/payment_button_controller_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'package:ecommerce_app/src/features/checkout/presentation/payment/payment_button_controller.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../../../mocks.dart'; | ||
|
||
void main() { | ||
group('pay', () { | ||
test('success', () async { | ||
// setup | ||
final checkoutService = MockCheckoutService(); | ||
when(() => checkoutService.placeOrder()).thenAnswer( | ||
(_) => Future.value(null), | ||
); | ||
final controller = | ||
PaymentButtonController(checkoutService: checkoutService); | ||
// run & verify | ||
expectLater( | ||
controller.stream, | ||
emitsInOrder([ | ||
const AsyncLoading<void>(), | ||
const AsyncData<void>(null), | ||
]), | ||
); | ||
await controller.pay(); | ||
}); | ||
|
||
test('failure', () async { | ||
// setup | ||
final checkoutService = MockCheckoutService(); | ||
when(() => checkoutService.placeOrder()).thenThrow( | ||
Exception('Card declined'), | ||
); | ||
final controller = | ||
PaymentButtonController(checkoutService: checkoutService); | ||
// run & verify | ||
expectLater( | ||
controller.stream, | ||
emitsInOrder([ | ||
const AsyncLoading<void>(), | ||
predicate<AsyncValue<void>>( | ||
(value) { | ||
expect(value.hasError, true); | ||
return true; | ||
}, | ||
), | ||
]), | ||
); | ||
await controller.pay(); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:ecommerce_app/src/features/orders/presentation/orders_list/order_card.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
class OrdersRobot { | ||
OrdersRobot(this.tester); | ||
final WidgetTester tester; | ||
|
||
void expectFindZeroOrders() { | ||
final finder = find.byType(OrderCard); | ||
expect(finder, findsNothing); | ||
} | ||
|
||
void expectFindNOrders(int count) { | ||
final finder = find.byType(OrderCard); | ||
expect(finder, findsNWidgets(count)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters