1
0
Fork 0
Browse Source

Add tests

bloc
Jonas Franz 6 months ago
parent
commit
6bb5e0e9bb
  1. 4
      lib/bloc/product_bloc.dart
  2. 8
      lib/bloc/product_bloc_states.dart
  3. 31
      test/cart_bloc_test.dart
  4. 14
      test/product_service_mock.dart
  5. 49
      test/total_price_test.dart

4
lib/bloc/product_bloc.dart

@ -13,7 +13,7 @@ class ProductBloc implements Disposable { @@ -13,7 +13,7 @@ class ProductBloc implements Disposable {
final UserBloc _userBloc;
bool _hasDiscount;
var _productsState = ProductsState.loading();
var _productsState = const ProductsState.loading();
ProductsState get productsState => _productsState;
final _productsStateController = StreamController<ProductsState>.broadcast();
@ -59,7 +59,7 @@ class ProductBloc implements Disposable { @@ -59,7 +59,7 @@ class ProductBloc implements Disposable {
void _handleLoadProductEvents() {
_loadProductEventController.stream.listen((_) async {
_productsState = ProductsState.loading();
_productsState = const ProductsState.loading();
_productsStateSink.add(_productsState);
try {

8
lib/bloc/product_bloc_states.dart

@ -3,11 +3,9 @@ part of 'product_bloc.dart'; @@ -3,11 +3,9 @@ part of 'product_bloc.dart';
abstract class ProductsState {
const ProductsState._();
factory ProductsState.loading() => const ProductsLoading();
factory ProductsState.error(String errorMessage) =>
ProductsError(errorMessage);
factory ProductsState.loaded(List<Product> products) =>
ProductsLoaded(products);
const factory ProductsState.loading() = ProductsLoading;
const factory ProductsState.error(String errorMessage) = ProductsError;
const factory ProductsState.loaded(List<Product> products) = ProductsLoaded;
bool isLoading() => this is ProductsLoading;
bool isLoaded() => this is ProductsLoaded;

31
test/cart_bloc_test.dart

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:thesis_shop/bloc/cart_bloc.dart';
import 'package:thesis_shop/bloc/product_bloc.dart';
import 'product_service_mock.dart';
class ProductBlocMock implements ProductBloc {
const ProductBlocMock();
@override
Future<void> dispose() async {}
@override
Sink<LoadProductEvent> get loadProductSink => throw UnimplementedError();
@override
final ProductsState productsState = const ProductsState.loaded(demoProducts);
@override
Stream<ProductsState> get productsStateStream =>
Stream.value(productsState).asBroadcastStream();
}
void main() {
test('test cart bloc', () async {
const productBlocMock = ProductBlocMock();
final cartBloc = CartBloc(productBlocMock);
final expectedCart = {demoProducts.first: 1};
expectLater(cartBloc.cartStream, emits(expectedCart));
cartBloc.quantityEventSink.add(IncreaseQuantityEvent(demoProducts.first));
});
}

14
test/product_service_mock.dart

@ -1,15 +1,17 @@ @@ -1,15 +1,17 @@
import 'package:thesis_shop/models/product.dart';
import 'package:thesis_shop/service/product_service.dart';
const demoProducts = [
Product(title: 'Bananen', price: 3),
Product(title: 'Äpfel', price: 2),
Product(title: 'Birnen', price: 2.5),
Product(title: 'Kirschen', price: 1.2),
];
class MockedProductService implements ProductService {
@override
Future<List<Product>> fetchProducts() async {
return const [
Product(title: 'Bananen', price: 3),
Product(title: 'Äpfel', price: 2),
Product(title: 'Birnen', price: 2.5),
Product(title: 'Kirschen', price: 1.2),
];
return demoProducts;
}
@override

49
test/total_price_test.dart

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:thesis_shop/bloc/cart_bloc.dart';
import 'package:thesis_shop/bloc/product_bloc.dart';
import 'package:thesis_shop/bloc/user_bloc.dart';
import 'package:thesis_shop/bloc_provider.dart';
import 'package:thesis_shop/screens/cart/total_price_text.dart';
import 'product_service_mock.dart';
class CartBlocMock implements CartBloc {
@override
Stream<int> numberOfProductsStream = const Stream.empty();
@override
Cart get cart => throw UnimplementedError();
@override
Stream<Cart> get cartStream => throw UnimplementedError();
@override
Future<void> dispose() async {}
@override
final int numberOfProducts = 1;
@override
Sink<ChangeQuantityEvent> get quantityEventSink => throw UnimplementedError();
@override
double get totalPrice => 10.00;
@override
Stream<double> get totalPriceStream => Stream.value(totalPrice);
}
void main() {
testWidgets('test total price widget', (tester) async {
final blocProvider = BlocProvider(
userBloc: UserBloc(),
productBloc: ProductBloc(MockedProductService(), UserBloc()),
cartBloc: CartBlocMock());
final appStateWithTotalPrice = AppState(
blocProvider: blocProvider,
child: const MaterialApp(home: TotalPriceText()));
await tester.pumpWidget(appStateWithTotalPrice);
expect(find.text("Gesamtpreis: 10.00€"), findsOneWidget);
});
}
Loading…
Cancel
Save