Skip to content

Commit

Permalink
autoDispose the addToCartControllerProvider + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bizz84 committed Dec 20, 2023
1 parent 170ea9d commit 3191196
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
12 changes: 12 additions & 0 deletions ecommerce_app/lib/src/features/cart/domain/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ class Item {
});
final ProductID productId;
final int quantity;

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is Item &&
other.productId == productId &&
other.quantity == quantity;
}

@override
int get hashCode => productId.hashCode ^ quantity.hashCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class AddToCartController extends StateNotifier<AsyncValue<int>> {
}
}

// TODO: Should this use autoDispose?
final addToCartControllerProvider =
StateNotifierProvider<AddToCartController, AsyncValue<int>>((ref) {
StateNotifierProvider.autoDispose<AddToCartController, AsyncValue<int>>(
(ref) {
return AddToCartController(
cartService: ref.watch(cartServiceProvider),
);
Expand Down
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;
},
),
);
});
});
}
3 changes: 3 additions & 0 deletions ecommerce_app/test/src/mocks.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:ecommerce_app/src/features/authentication/data/fake_auth_repository.dart';
import 'package:ecommerce_app/src/features/cart/application/cart_service.dart';
import 'package:ecommerce_app/src/features/cart/data/local/local_cart_repository.dart';
import 'package:ecommerce_app/src/features/cart/data/remote/remote_cart_repository.dart';
import 'package:mocktail/mocktail.dart';
Expand All @@ -8,3 +9,5 @@ class MockAuthRepository extends Mock implements FakeAuthRepository {}
class MockRemoteCartRepository extends Mock implements RemoteCartRepository {}

class MockLocalCartRepository extends Mock implements LocalCartRepository {}

class MockCartService extends Mock implements CartService {}

0 comments on commit 3191196

Please sign in to comment.