Skip to content

3.1 Architektur Decrement #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: architektur
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/src/features/counter/presentation/counter.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ class CounterController {
counterModel.value += 1;
counterRepository.updateCounter(counterModel: counterModel);
}

Future<void> decrement() async {
if (counterModel.value > 0) {
counterModel.value -= 1;
counterRepository.updateCounter(counterModel: counterModel);
}
}
}
25 changes: 21 additions & 4 deletions lib/src/features/counter/presentation/counter.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class _CounterPageState extends State<CounterPage> {
});
}

void _decrementCounter() {
setState(() {
counterController.decrement();
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -44,10 +50,21 @@ class _CounterPageState extends State<CounterPage> {
],
),
),
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),
),
],
),
);
}
Expand Down
81 changes: 63 additions & 18 deletions test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}