Skip to content

Commit

Permalink
fix: selected combatants landscape (#50)
Browse files Browse the repository at this point in the history
* fix: selected combatants bug

* test: add test for SelectedCombatants widget
  • Loading branch information
VytorCalixto authored Nov 14, 2024
1 parent 6486e7e commit 8db25fd
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/features/combatant/add_combatant_landscape.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class AddCombatantPageLandscape extends StatelessWidget {
const AddCombatantPageLandscape({
super.key,
required this.onCombatantsAdded,
required this.onCombatantsChanged,
this.showGroupReminder = false,
this.combatants = const {},
});

final bool showGroupReminder;
final ValueChanged<Map<Combatant, int>> onCombatantsAdded;
final ValueChanged<Map<Combatant, int>> onCombatantsChanged;
final Map<Combatant, int> combatants;

@override
Expand All @@ -34,7 +36,7 @@ class AddCombatantPageLandscape extends StatelessWidget {
Expanded(
child: SelectedCombatants(
combatants: combatants,
onCombatantsChanged: onCombatantsAdded,
onCombatantsChanged: onCombatantsChanged,
),
),
Row(
Expand Down
3 changes: 3 additions & 0 deletions lib/features/combatant/add_combatant_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class _AddCombatantPageState extends State<AddCombatantPage> {
combatants: _combatants,
showGroupReminder:
widget.params.encounterType == EncounterType.encounter,
onCombatantsChanged: (combatants) => setState(() {
_combatants = combatants;
}),
onCombatantsAdded: (combatants) {
setState(() {
combatants.forEach((combatant, count) {
Expand Down
4 changes: 4 additions & 0 deletions lib/features/combatant/widgets/selected_combatants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SelectedCombatants extends StatelessWidget {
final count = combatants.values.elementAt(index);

return Row(
key: const Key('combatants-list-tile'),
mainAxisSize: MainAxisSize.max,
children: [
Text(
Expand All @@ -45,15 +46,18 @@ class SelectedCombatants extends StatelessWidget {
),
Expanded(
child: Row(
key: Key('${combatant.name}-count'),
mainAxisAlignment: MainAxisAlignment.end,
children: [
IconButton(
key: const Key('remove-combatant'),
onPressed: () =>
_updateCombatants(combatant, count - 1),
icon: Icon(MingCute.minus_circle_fill),
),
Text(count.toString()),
IconButton(
key: const Key('add-combatant'),
onPressed: () =>
_updateCombatants(combatant, count + 1),
icon: Icon(MingCute.add_circle_fill),
Expand Down
1 change: 0 additions & 1 deletion lib/features/settings/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'legal/pf2e_legal.dart';
import 'widgets/app_settings.dart';
import 'widgets/dnd5e_settings.dart';
import 'widgets/encounter_settings_widget.dart';
import 'widgets/pf2e_settings.dart';

class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
Expand Down
127 changes: 127 additions & 0 deletions test/features/combatant/widgets/selected_combatants_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import 'package:battlemaster/features/combatant/models/combatant.dart';
import 'package:battlemaster/features/combatant/models/combatant_type.dart';
import 'package:battlemaster/features/combatant/widgets/selected_combatants.dart';
import 'package:battlemaster/features/engines/models/game_engine_type.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../../build_widget.dart';

void main() {
Map<Combatant, int> combatants = {};

setUp(() {
combatants = {
Combatant(
name: 'Player',
initiative: 1,
maxHp: 0,
currentHp: 0,
armorClass: 0,
initiativeModifier: 0,
type: CombatantType.player,
engineType: GameEngineType.custom,
): 1,
Combatant(
name: 'Monster',
initiative: 1,
maxHp: 0,
currentHp: 0,
armorClass: 0,
initiativeModifier: 0,
type: CombatantType.monster,
engineType: GameEngineType.custom,
): 2,
};
});

testWidgets('Remove a combatant', (WidgetTester tester) async {
await tester.pumpWidget(
buildWidgetTree(
SelectedCombatants(
combatants: combatants,
onCombatantsChanged: (value) {
combatants = value;
},
),
),
);

// find combatants
expect(find.byKey(const Key('combatants-list-tile')), findsNWidgets(2));
expect(find.byKey(Key('Player-count')), findsOneWidget);
expect(find.byKey(Key('Monster-count')), findsOneWidget);

// find button to remove player
expect(
find.descendant(
of: find.byKey(Key('Player-count')),
matching: find.byKey(const Key('remove-combatant')),
),
findsOneWidget,
);

await tester.tap(find.descendant(
of: find.byKey(Key('Player-count')),
matching: find.byKey(const Key('remove-combatant')),
));
await tester.pump();
await tester.pumpWidget(
buildWidgetTree(
SelectedCombatants(
combatants: combatants,
onCombatantsChanged: (value) {
combatants = value;
},
),
),
);

expect(find.byKey(Key('Player-count')), findsNothing);
expect(find.byKey(Key('Monster-count')), findsOneWidget);
});

testWidgets('Add a combatant', (WidgetTester tester) async {
await tester.pumpWidget(
buildWidgetTree(
SelectedCombatants(
combatants: combatants,
onCombatantsChanged: (value) {
combatants = value;
},
),
),
);

// find combatant
expect(find.byKey(Key('Monster-count')), findsOneWidget);
expect(find.text('2'), findsOneWidget);

// find button to add combatant
expect(
find.descendant(
of: find.byKey(Key('Monster-count')),
matching: find.byKey(const Key('add-combatant')),
),
findsOneWidget,
);

await tester.tap(find.descendant(
of: find.byKey(Key('Monster-count')),
matching: find.byKey(const Key('add-combatant')),
));
await tester.pump();
await tester.pumpWidget(
buildWidgetTree(
SelectedCombatants(
combatants: combatants,
onCombatantsChanged: (value) {
combatants = value;
},
),
),
);

expect(find.text('3'), findsOneWidget);
});
}

0 comments on commit 8db25fd

Please sign in to comment.