mirror of https://github.com/flutter/samples.git
[provider_shopper] Added widget tests for login, catalog and cart screens (#400)
parent
e6c7aaf8c6
commit
ddf610d682
@ -0,0 +1,62 @@
|
|||||||
|
// Copyright 2020 The Flutter team. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:provider_shopper/models/cart.dart';
|
||||||
|
import 'package:provider_shopper/models/catalog.dart';
|
||||||
|
import 'package:provider_shopper/screens/cart.dart';
|
||||||
|
|
||||||
|
CartModel cartModel;
|
||||||
|
CatalogModel catalogModel;
|
||||||
|
Widget createCartScreen() => MultiProvider(
|
||||||
|
providers: [
|
||||||
|
Provider(create: (context) => CatalogModel()),
|
||||||
|
ChangeNotifierProxyProvider<CatalogModel, CartModel>(
|
||||||
|
create: (context) => CartModel(),
|
||||||
|
update: (context, catalog, cart) {
|
||||||
|
catalogModel = catalog;
|
||||||
|
cartModel = cart;
|
||||||
|
cart.catalog = catalogModel;
|
||||||
|
return cart;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
child: MaterialApp(
|
||||||
|
home: MyCart(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('CartScreen widget tests', () {
|
||||||
|
testWidgets('Tapping BUY button displays snackbar.', (tester) async {
|
||||||
|
await tester.pumpWidget(createCartScreen());
|
||||||
|
|
||||||
|
// Verify no snackbar initially exists.
|
||||||
|
expect(find.byType(SnackBar), findsNothing);
|
||||||
|
await tester.tap(find.text('BUY'));
|
||||||
|
// Schedule animation.
|
||||||
|
await tester.pump();
|
||||||
|
// Verifying the snackbar upon clicking the button.
|
||||||
|
expect(find.byType(SnackBar), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('Testing when the cart contains items', (tester) async {
|
||||||
|
await tester.pumpWidget(createCartScreen());
|
||||||
|
|
||||||
|
// Adding five items in the cart and testing.
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
var item = catalogModel.getByPosition(i);
|
||||||
|
cartModel.add(item);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text(item.name), findsOneWidget);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Testing total price of the five items.
|
||||||
|
expect(find.text('\$${42 * 5}'), findsOneWidget);
|
||||||
|
expect(find.byIcon(Icons.done), findsNWidgets(5));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
// Copyright 2020 The Flutter team. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:provider_shopper/models/cart.dart';
|
||||||
|
import 'package:provider_shopper/models/catalog.dart';
|
||||||
|
import 'package:provider_shopper/screens/catalog.dart';
|
||||||
|
|
||||||
|
Widget createCatalogScreen() => MultiProvider(
|
||||||
|
providers: [
|
||||||
|
Provider(create: (context) => CatalogModel()),
|
||||||
|
ChangeNotifierProxyProvider<CatalogModel, CartModel>(
|
||||||
|
create: (context) => CartModel(),
|
||||||
|
update: (context, catalog, cart) {
|
||||||
|
cart.catalog = catalog;
|
||||||
|
return cart;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
child: MaterialApp(
|
||||||
|
home: MyCatalog(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final catalogListItems = CatalogModel.itemNames.sublist(0, 3);
|
||||||
|
|
||||||
|
group('CatalogScreen Widget Tests', () {
|
||||||
|
testWidgets('Testing item row counts and text', (tester) async {
|
||||||
|
await tester.pumpWidget(createCatalogScreen());
|
||||||
|
|
||||||
|
// Testing for the items on the screen after modifying
|
||||||
|
// the model for a fixed number of items.
|
||||||
|
for (String item in catalogListItems) {
|
||||||
|
expect(find.text(item), findsWidgets);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('Testing the ADD buttons and check after clicking',
|
||||||
|
(tester) async {
|
||||||
|
await tester.pumpWidget(createCatalogScreen());
|
||||||
|
|
||||||
|
// Should find ADD buttons on the screen.
|
||||||
|
expect(find.text('ADD'), findsWidgets);
|
||||||
|
|
||||||
|
// Performing the click on the ADD button of the first item in the list.
|
||||||
|
await tester.tap(find.widgetWithText(FlatButton, 'ADD').first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// Verifying if the tapped ADD button has changed to the check icon.
|
||||||
|
expect(find.byIcon(Icons.check), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
// Copyright 2020 The Flutter team. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:provider_shopper/models/cart.dart';
|
||||||
|
import 'package:provider_shopper/models/catalog.dart';
|
||||||
|
import 'package:provider_shopper/screens/catalog.dart';
|
||||||
|
import 'package:provider_shopper/screens/login.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
testWidgets('Login page Widget test', (tester) async {
|
||||||
|
await tester.pumpWidget(MultiProvider(
|
||||||
|
providers: [
|
||||||
|
Provider(create: (context) => CatalogModel()),
|
||||||
|
ChangeNotifierProxyProvider<CatalogModel, CartModel>(
|
||||||
|
create: (context) => CartModel(),
|
||||||
|
update: (context, catalog, cart) {
|
||||||
|
cart.catalog = catalog;
|
||||||
|
return cart;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
child: MaterialApp(
|
||||||
|
initialRoute: '/',
|
||||||
|
routes: {
|
||||||
|
'/': (context) => MyLogin(),
|
||||||
|
'/catalog': (context) => MyCatalog(),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
));
|
||||||
|
|
||||||
|
// Verifying the behaviour of ENTER button.
|
||||||
|
await tester.tap(find.text('ENTER'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('Catalog'), findsOneWidget);
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in new issue