-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
autoDispose the addToCartControllerProvider + tests
- Loading branch information
Showing
4 changed files
with
91 additions
and
2 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
74 changes: 74 additions & 0 deletions
74
...erce_app/test/src/features/cart/presentation/add_to_cart/add_to_cart_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,74 @@ | ||
import 'package:ecommerce_app/src/features/cart/domain/item.dart'; | ||
import 'package:ecommerce_app/src/features/cart/presentation/add_to_cart/add_to_cart_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() { | ||
const productId = '1'; | ||
group('addItem', () { | ||
test('item added with quantity = 2, success', () async { | ||
// setup | ||
const quantity = 2; | ||
const item = Item(productId: productId, quantity: quantity); | ||
final cartService = MockCartService(); | ||
when(() => cartService.addItem(item)).thenAnswer( | ||
(_) => Future.value(null), | ||
); | ||
// run & verify | ||
final controller = AddToCartController(cartService: cartService); | ||
expect( | ||
controller.state, | ||
const AsyncData(1), | ||
); | ||
controller.updateQuantity(quantity); | ||
expect( | ||
controller.state, | ||
const AsyncData(2), | ||
); | ||
// * if desired, use expectLater and emitsInOrder here to check that | ||
// * addItems emits two values | ||
await controller.addItem(productId); | ||
verify(() => cartService.addItem(item)).called(1); | ||
// check that quantity goes back to 1 after adding an item | ||
expect( | ||
controller.state, | ||
const AsyncData(1), | ||
); | ||
}); | ||
|
||
test('item added with quantity = 2, failure', () async { | ||
const quantity = 2; | ||
const item = Item(productId: productId, quantity: quantity); | ||
final cartService = MockCartService(); | ||
when(() => cartService.addItem(item)) | ||
.thenThrow((_) => Exception('Connection failed')); | ||
final controller = AddToCartController(cartService: cartService); | ||
expect( | ||
controller.state, | ||
const AsyncData(1), | ||
); | ||
controller.updateQuantity(quantity); | ||
expect( | ||
controller.state, | ||
const AsyncData(2), | ||
); | ||
// * if desired, use expectLater and emitsInOrder here to check that | ||
// * addItems emits two values | ||
await controller.addItem(productId); | ||
verify(() => cartService.addItem(item)).called(1); | ||
// check that quantity goes back to 1 after adding an item | ||
expect( | ||
controller.state, | ||
predicate<AsyncValue<int>>( | ||
(value) { | ||
expect(value.hasError, true); | ||
return true; | ||
}, | ||
), | ||
); | ||
}); | ||
}); | ||
} |
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