1
0
Fork 0

Add tests

mobx
Jonas Franz 2 years ago
parent 0d972b3126
commit 7dc6dd5e1b
  1. 11
      lib/app.dart
  2. 12
      lib/screens/cart/total_price_text.dart
  3. 6
      lib/stores/cart_store.dart
  4. 10
      lib/stores/cart_store.g.dart
  5. 18
      lib/stores/store_injector.dart
  6. 25
      test/cart_store_test.dart
  7. 14
      test/product_service_mock.dart
  8. 55
      test/total_price_text_test.dart

@ -4,8 +4,12 @@ import 'package:thesis_shop/screens/cart/cart_screen.dart';
import 'package:thesis_shop/screens/product_list/product_list_screen.dart'; import 'package:thesis_shop/screens/product_list/product_list_screen.dart';
import 'package:thesis_shop/service/product_service.dart'; import 'package:thesis_shop/service/product_service.dart';
import 'package:thesis_shop/stores/store_injector.dart'; import 'package:thesis_shop/stores/store_injector.dart';
import 'package:thesis_shop/stores/user_store.dart';
import 'package:thesis_shop/utils/map_keys_extension.dart'; import 'package:thesis_shop/utils/map_keys_extension.dart';
import 'stores/cart_store.dart';
import 'stores/product_store.dart';
class ThesisShopApp extends StatelessWidget { class ThesisShopApp extends StatelessWidget {
final ProductService productService; final ProductService productService;
const ThesisShopApp({Key? key, required this.productService}) const ThesisShopApp({Key? key, required this.productService})
@ -13,8 +17,13 @@ class ThesisShopApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final userStore = UserStore();
final productStore = ProductStore(productService, userStore);
final cartStore = CartStore(productStore);
return StoreInjector( return StoreInjector(
productService: productService, cartStore: cartStore,
userStore: userStore,
productStore: productStore,
child: MaterialApp( child: MaterialApp(
title: 'Thesis Shop', title: 'Thesis Shop',
theme: ThemeData(primarySwatch: Colors.red), theme: ThemeData(primarySwatch: Colors.red),

@ -1,16 +1,22 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:thesis_shop/stores/store_injector.dart';
class TotalPriceText extends StatelessWidget { class TotalPriceText extends StatelessWidget {
const TotalPriceText({Key? key}) : super(key: key); const TotalPriceText({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final cartStore = StoreInjector.of(context).cartStore;
return SafeArea( return SafeArea(
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Text( child: Observer(
'Gesamtpreis: 27€', builder: (context) => Text(
style: Theme.of(context).textTheme.labelLarge?.copyWith(fontSize: 24), 'Gesamtpreis: ${cartStore.totalPrice.toStringAsFixed(2)}',
style:
Theme.of(context).textTheme.labelLarge?.copyWith(fontSize: 24),
),
), ),
), ),
); );

@ -32,6 +32,12 @@ abstract class _CartStore with Store {
@computed @computed
int get numberOfItems => cart.length; int get numberOfItems => cart.length;
@computed
double get totalPrice => cart.fold(
0.0,
(previousValue, element) =>
previousValue + element.amount * element.product.price);
@action @action
void _changeAmountOfProduct(Product product, int amount) { void _changeAmountOfProduct(Product product, int amount) {
final oldValue = _productQuantities.putIfAbsent(product.title, () => 0); final oldValue = _productQuantities.putIfAbsent(product.title, () => 0);

@ -23,6 +23,13 @@ mixin _$CartStore on _CartStore, Store {
(_$numberOfItemsComputed ??= Computed<int>(() => super.numberOfItems, (_$numberOfItemsComputed ??= Computed<int>(() => super.numberOfItems,
name: '_CartStore.numberOfItems')) name: '_CartStore.numberOfItems'))
.value; .value;
Computed<double>? _$totalPriceComputed;
@override
double get totalPrice =>
(_$totalPriceComputed ??= Computed<double>(() => super.totalPrice,
name: '_CartStore.totalPrice'))
.value;
final _$_productQuantitiesAtom = Atom(name: '_CartStore._productQuantities'); final _$_productQuantitiesAtom = Atom(name: '_CartStore._productQuantities');
@ -56,7 +63,8 @@ mixin _$CartStore on _CartStore, Store {
String toString() { String toString() {
return ''' return '''
cart: ${cart}, cart: ${cart},
numberOfItems: ${numberOfItems} numberOfItems: ${numberOfItems},
totalPrice: ${totalPrice}
'''; ''';
} }
} }

@ -1,22 +1,20 @@
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:thesis_shop/service/product_service.dart';
import 'package:thesis_shop/stores/cart_store.dart'; import 'package:thesis_shop/stores/cart_store.dart';
import 'package:thesis_shop/stores/product_store.dart'; import 'package:thesis_shop/stores/product_store.dart';
import 'package:thesis_shop/stores/user_store.dart'; import 'package:thesis_shop/stores/user_store.dart';
class StoreInjector extends InheritedWidget { class StoreInjector extends InheritedWidget {
final UserStore userStore = UserStore(); final UserStore userStore;
late final CartStore cartStore; final CartStore cartStore;
late final ProductStore productStore; final ProductStore productStore;
StoreInjector({ const StoreInjector({
Key? key, Key? key,
required ProductService productService, required this.userStore,
required this.cartStore,
required this.productStore,
required Widget child, required Widget child,
}) : super(key: key, child: child) { }) : super(key: key, child: child);
productStore = ProductStore(productService, userStore);
cartStore = CartStore(productStore);
}
static StoreInjector of(BuildContext context) { static StoreInjector of(BuildContext context) {
final StoreInjector? result = final StoreInjector? result =

@ -0,0 +1,25 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mobx/mobx.dart';
import 'package:mobx/src/api/async.dart';
import 'package:thesis_shop/models/product.dart';
import 'package:thesis_shop/stores/cart_store.dart';
import 'package:thesis_shop/stores/product_store.dart';
import 'product_service_mock.dart';
class MockedProductStore implements ProductStore {
@override
void loadProducts() {}
@override
ObservableFuture<List<Product>> products =
ObservableFuture.value(demoProducts);
}
void main() {
test('test cart store', () {
final cartStore = CartStore(MockedProductStore());
cartStore.incrementAmountOfProduct(demoProducts.first);
expect(cartStore.amountOfProduct(demoProducts.first), 1);
});
}

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

@ -0,0 +1,55 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mobx/src/api/observable_collections.dart';
import 'package:thesis_shop/models/cart_item.dart';
import 'package:thesis_shop/models/product.dart';
import 'package:thesis_shop/screens/cart/total_price_text.dart';
import 'package:thesis_shop/stores/cart_item_store.dart';
import 'package:thesis_shop/stores/cart_store.dart';
import 'package:thesis_shop/stores/store_injector.dart';
import 'package:thesis_shop/stores/user_store.dart';
import 'cart_store_test.dart';
class MockedCartStore implements CartStore {
@override
int amountOfProduct(Product product) {
throw UnimplementedError();
}
@override
ObservableList<CartItem> get cart => throw UnimplementedError();
@override
CartItemStore cartItemStoreOfProduct(Product product) {
throw UnimplementedError();
}
@override
void decrementAmountOfProduct(Product product) {}
@override
void incrementAmountOfProduct(Product product) {}
@override
int get numberOfItems => throw UnimplementedError();
@override
double get totalPrice => 10.0;
}
void main() {
testWidgets('test total price text', (tester) async {
await tester.pumpWidget(
StoreInjector(
userStore: UserStore(),
cartStore: MockedCartStore(),
productStore: MockedProductStore(),
child: const MaterialApp(
home: TotalPriceText(),
),
),
);
expect(find.text('Gesamtpreis: 10.00€'), findsOneWidget);
});
}
Loading…
Cancel
Save