Skip to content

Commit

Permalink
Add text callback and test
Browse files Browse the repository at this point in the history
  • Loading branch information
thelukewalton committed Mar 1, 2024
1 parent a7ac05b commit 4a762e2
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 57 deletions.
94 changes: 81 additions & 13 deletions example/lib/pages/components/dialpad_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,94 @@ import 'package:flutter/material.dart';
import 'package:zeta_example/widgets.dart';
import 'package:zeta_flutter/zeta_flutter.dart';

class DialPadExample extends StatelessWidget {
const double _paddingSize = 40;

class DialPadExample extends StatefulWidget {
static const String name = 'DialPad';

const DialPadExample({super.key});

@override
State<DialPadExample> createState() => _DialPadExampleState();
}

class _DialPadExampleState extends State<DialPadExample> {
String number = '', text = '';

@override
Widget build(BuildContext context) {
return ExampleScaffold(
name: name,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ZetaDialPad(),
],
),
],
),
name: DialPadExample.name,
child: LayoutBuilder(builder: (context, constraints) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: constraints.maxWidth,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const SizedBox(width: _paddingSize),
SizedBox(
width: constraints.maxWidth - (_paddingSize * 2),
child: Text(
number,
style: ZetaTextStyles.heading3,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
),
IconButton(
icon: Icon(Icons.backspace),
onPressed: () => number.length == 0
? null
: setState(
() => number = number.substring(0, (number.length - 1)),
),
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const SizedBox(),
Text(
text,
style: ZetaTextStyles.heading3,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
IconButton(
icon: Icon(Icons.backspace),
onPressed: () => text.length == 0
? null
: setState(
() => text = text.substring(0, text.length - 1),
),
)
],
),
ZetaDialPad(
onNumber: (value) => setState(() => number += value),
onText: (value) => setState(() => text += value),
),
ZetaButton.primary(
label: 'Clear',
borderType: ZetaWidgetBorder.full,
onPressed: () => setState(() => number = text = ''),
)
].divide(const SizedBox(height: ZetaSpacing.m)).toList(),
),
),
],
);
}),
);
}
}
116 changes: 116 additions & 0 deletions example/test/dialpad_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:zeta_flutter/zeta_flutter.dart';

import 'test_components.dart';

void main() {
group('ZetaDialPad Tests', () {
testWidgets('Initializes with correct parameters', (WidgetTester tester) async {
String number = '', text = '';

Future<void> debounceWait() => tester.binding.delayed(const Duration(milliseconds: 500));

await tester.pumpWidget(
TestWidget(
screenSize: Size(1000, 1000),
widget: ZetaDialPad(
onNumber: (value) => number += value,
onText: (value) => text += value,
),
),
);
final dialPadFinder = find.byType(ZetaDialPad);
final buttonFinder = find.byType(ZetaDialPadButton);

final oneFinder = find.byWidgetPredicate((widget) => widget is ZetaDialPadButton && widget.primary == '1');
final twoFinder = find.byWidgetPredicate((widget) => widget is ZetaDialPadButton && widget.primary == '2');
final threeFinder = find.byWidgetPredicate((widget) => widget is ZetaDialPadButton && widget.primary == '3');
final starFinder = find.byWidgetPredicate((widget) => widget is ZetaDialPadButton && widget.primary == '*');
final hashFinder = find.byWidgetPredicate((widget) => widget is ZetaDialPadButton && widget.primary == '#');

final ZetaDialPad dialPad = tester.firstWidget(dialPadFinder);
final List<Widget> dialPadButtons = tester.widgetList(buttonFinder).toList();

final ZetaDialPadButton one = tester.firstWidget(oneFinder);
final ZetaDialPadButton two = tester.firstWidget(twoFinder);
final ZetaDialPadButton three = tester.firstWidget(threeFinder);

/// Dial Pad built correctly.
expect(dialPad.buttonsPerRow, 3);
expect(dialPadButtons.length, 12);

/// Dial Pad buttons built correctly.
expect(one.primary, '1');
expect(one.secondary, '');
expect(two.primary, '2');
expect(two.secondary, 'ABC');
expect(three.primary, '3');
expect(three.secondary, 'DEF');

/// Tap button with only number.
await tester.tap(oneFinder);
await tester.pump();
expect(number, '1');

/// Tap button with number and text.
await tester.tap(twoFinder);
await tester.pump();
await debounceWait();
expect(number, '12');
expect(text, 'A');

/// Tap different button.
await tester.tap(threeFinder);
await tester.pump();
await debounceWait();
expect(number, '123');
expect(text, 'AD');

/// Tap text button twice.
await tester.tap(twoFinder);
await tester.tap(twoFinder);
await tester.pump();
await debounceWait();
expect(text, 'ADB');

/// Tap text button thrice.
await tester.tap(twoFinder);
await tester.tap(twoFinder);
await tester.tap(twoFinder);
await tester.pump();
await debounceWait();
expect(text, 'ADBC');

/// Tap different text buttons to ensure debounce is cancelled.
await tester.tap(twoFinder);
await tester.tap(threeFinder);
await tester.tap(twoFinder);
await tester.pump();
await debounceWait();
expect(text, 'ADBCADA');

/// Tap text button more times than there is options to ensure it loops around correctly.
await tester.tap(threeFinder);
await tester.tap(threeFinder);
await tester.tap(threeFinder);
await tester.tap(threeFinder);
await tester.tap(threeFinder);
await tester.tap(threeFinder);
await tester.tap(oneFinder);
await tester.pump();
expect(text, 'ADBCADAF');
number = '';

/// Tap buttons with symbols
await tester.ensureVisible(starFinder);
await tester.tap(starFinder);
await tester.tap(hashFinder);
await tester.pump();
expect(number, '*#');

/// Allow all timers to end in text debounce
await debounceWait();
});
});
}
Loading

0 comments on commit 4a762e2

Please sign in to comment.