1
0
Fork 0
Browse Source

Add tests

mobx
Jonas Franz 6 months ago
parent
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

11
lib/app.dart

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

12
lib/screens/cart/total_price_text.dart

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

6
lib/stores/cart_store.dart

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

10
lib/stores/cart_store.g.dart

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

18
lib/stores/store_injector.dart

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

25
test/cart_store_test.dart

@ -0,0 +1,25 @@ @@ -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);
});
}

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

55
test/total_price_text_test.dart

@ -0,0 +1,55 @@ @@ -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