From c8975da794463c51e934afd1d32ab91e03863257 Mon Sep 17 00:00:00 2001 From: Jan Marsh Date: Fri, 9 Sep 2022 14:44:11 +0200 Subject: [PATCH] decrement with widget testing examples --- .../presentation/counter.controller.dart | 7 ++ .../counter/presentation/counter.page.dart | 25 +++++- test/widget_test.dart | 81 ++++++++++++++----- 3 files changed, 91 insertions(+), 22 deletions(-) diff --git a/lib/src/features/counter/presentation/counter.controller.dart b/lib/src/features/counter/presentation/counter.controller.dart index fc9eec1..32d8cd4 100644 --- a/lib/src/features/counter/presentation/counter.controller.dart +++ b/lib/src/features/counter/presentation/counter.controller.dart @@ -13,4 +13,11 @@ class CounterController { counterModel.value += 1; counterRepository.updateCounter(counterModel: counterModel); } + + Future decrement() async { + if (counterModel.value > 0) { + counterModel.value -= 1; + counterRepository.updateCounter(counterModel: counterModel); + } + } } diff --git a/lib/src/features/counter/presentation/counter.page.dart b/lib/src/features/counter/presentation/counter.page.dart index da1bff7..b43d489 100644 --- a/lib/src/features/counter/presentation/counter.page.dart +++ b/lib/src/features/counter/presentation/counter.page.dart @@ -24,6 +24,12 @@ class _CounterPageState extends State { }); } + void _decrementCounter() { + setState(() { + counterController.decrement(); + }); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -44,10 +50,21 @@ class _CounterPageState extends State { ], ), ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), + floatingActionButton: Column( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + FloatingActionButton( + onPressed: _incrementCounter, + tooltip: 'Increment', + child: const Icon(Icons.add), + ), + const SizedBox(height: 10), + FloatingActionButton( + onPressed: _decrementCounter, + tooltip: 'Decrement', + child: const Icon(Icons.remove), + ), + ], ), ); } diff --git a/test/widget_test.dart b/test/widget_test.dart index 6031a2c..adebacb 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -12,24 +12,69 @@ import 'package:counter_workshop/src/features/counter/data/repositories/counter. import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +const _desktopSize = Size(1024, 768); + void main() { - testWidgets('Counter increments smoke test', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget( - App(counterRepository: CounterRepository(counterApi: CounterFakeApi(), counterDatabase: CounterDatabase())), - const Duration(milliseconds: 300), // Because of FakeApi delay - ); - - // Verify that our counter starts at 0. - expect(find.text('0'), findsOneWidget); - expect(find.text('1'), findsNothing); - - // Tap the '+' icon and trigger a frame. - await tester.tap(find.byIcon(Icons.add)); - await tester.pumpAndSettle(const Duration(milliseconds: 300)); // Because of FakeApi delay - - // Verify that our counter has incremented. - expect(find.text('0'), findsNothing); - expect(find.text('1'), findsOneWidget); + group('Counter Buttons Tests', () { + // Test Inceptors + setUpAll(() async { + final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized(); + binding.window.physicalSizeTestValue = _desktopSize; + }); + setUp(() async {}); + tearDownAll(() async {}); + tearDown(() async {}); + + testWidgets('Counter increments smoke test', (WidgetTester tester) async { + // Build our app and trigger a frame. + await tester.pumpWidget( + App(counterRepository: CounterRepository(counterApi: CounterFakeApi(), counterDatabase: CounterDatabase())), + const Duration(milliseconds: 300), // Because of FakeApi delay + ); + + // Verify that our counter starts at 0. + expect(find.text('0'), findsOneWidget); + expect(find.text('1'), findsNothing); + + // Tap the '+' icon and trigger a frame. + await tester.tap(find.byIcon(Icons.add)); + await tester.pumpAndSettle(const Duration(milliseconds: 300)); // Because of FakeApi delay + + // Verify that our counter has incremented. + expect(find.text('0'), findsNothing); + expect(find.text('1'), findsOneWidget); + }); + testWidgets('Counter decrements smoke test', (WidgetTester tester) async { + // Build our app and trigger a frame. + await tester.pumpWidget( + App(counterRepository: CounterRepository(counterApi: CounterFakeApi(), counterDatabase: CounterDatabase())), + const Duration(milliseconds: 300), // Because of FakeApi delay + ); + + // Verify that our counter starts at 0. + expect(find.text('0'), findsOneWidget); + expect(find.text('1'), findsNothing); + + // Tap the '-' icon and trigger a frame. + await tester.tap(find.byTooltip('Decrement')); + await tester.pumpAndSettle(); + await tester.pumpAndSettle(const Duration(milliseconds: 300)); // Because of FakeApi delay + + // Verify that our counter does not decremented. + expect(find.text('-1'), findsNothing); + expect(find.text('0'), findsOneWidget); + + await tester.tap(find.byIcon(Icons.add)); + await tester.pumpAndSettle(const Duration(milliseconds: 300)); // Because of FakeApi delay + // Verify that our counter has incremented. + expect(find.text('0'), findsNothing); + expect(find.text('1'), findsOneWidget); + + await tester.tap(find.byIcon(Icons.remove)); + await tester.pumpAndSettle(const Duration(milliseconds: 300)); // Because of FakeApi delay + // Verify that our counter has decremented. + expect(find.text('1'), findsNothing); + expect(find.text('0'), findsOneWidget); + }); }); }