Skip to content
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

chore: [NO-TASK] Add tests for auth_code, bottom_sheet, button, carousel, checkbox, ch… #330

Merged
merged 2 commits into from
Dec 28, 2023
Merged
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
97 changes: 97 additions & 0 deletions test/auth_code_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moon_design/moon_design.dart';

void main() {
const key = Key("auth_code_test");

testWidgets("Provided key is used", (tester) async {
await tester.pumpWidget(
const TestWidget(
authCodeKey: key,
),
);
expect(find.byKey(key), findsOneWidget);
});

testWidgets("Enter valid code", (tester) async {
await tester.pumpWidget(
const TestWidget(
authCodeKey: key,
),
);
final widget = find.byType(TextFormField).first;
await tester.enterText(widget, '9921');
await tester.pump(const Duration(milliseconds: 110));
expect(find.text(errorMessage), findsNothing);
});
testWidgets("Enter invalid code", (tester) async {
await tester.pumpWidget(
const TestWidget(
authCodeKey: key,
),
);
final widget = find.byType(TextFormField).first;
await tester.enterText(widget, '1111');
await tester.pump(const Duration(milliseconds: 110));
expect(find.text(errorMessage), findsOneWidget);
});

testWidgets("Initial error message", (tester) async {
await tester.pumpWidget(
const TestWidget(
authCodeKey: key,
initialErrorMessage: initialErrorMessage,
),
);
final widget = find.byType(TextFormField).first;
expect(find.text(initialErrorMessage), findsOneWidget);
await tester.enterText(widget, '1111');
await tester.pump(const Duration(milliseconds: 110));
expect(find.text(initialErrorMessage), findsNothing);
expect(find.text(errorMessage), findsOneWidget);
});
}

const errorMessage = 'Invalid authentication code. Please try again.';
const initialErrorMessage = 'Initial error message';

class TestWidget extends StatelessWidget {
const TestWidget({
super.key,
this.authCodeKey,
this.initialErrorMessage,
});

final Key? authCodeKey;
final String? initialErrorMessage;

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Directionality(
textDirection: TextDirection.ltr,
child: MoonAuthCode(
key: authCodeKey,
authInputFieldCount: 4,
autoFocus: true,
enableInputFill: true,
validator: (String? pin) {
return pin?.length == 4 && pin != '9921' ? errorMessage : null;
},
errorText: initialErrorMessage,
errorBuilder: (BuildContext context, String? errorText) {
return Align(
child: Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(errorText ?? ''),
),
);
},
),
),
),
);
}
}
136 changes: 136 additions & 0 deletions test/bottom_sheet_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moon_design/moon_design.dart';

// Test
void main() {
const key = Key("bottom_sheet_test");

testWidgets("Botton sheet is shown after clicking on button", (tester) async {
await tester.pumpWidget(const TestWidget(key: key, content: content));
final button = find.byType(MoonFilledButton);

expect(button, findsOneWidget);

await tester.tap(button);
await tester.pumpAndSettle();

expect(find.byWidget(content), findsOneWidget);
});

testWidgets("Botton sheet is hidden after clicking outside content",
(tester) async {
await tester.pumpWidget(const TestWidget(key: key, content: content));
final button = find.byType(MoonFilledButton);

expect(button, findsOneWidget);

await tester.tap(button);
await tester.pumpAndSettle();

expect(find.byWidget(content), findsOneWidget);

await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();

expect(find.byWidget(content), findsNothing);
});

testWidgets(
"Botton sheet is not hidden after clicking outside content if not dismissable",
(tester) async {
await tester.pumpWidget(
const TestWidget(
key: key,
content: content,
isDismissible: false,
),
);
final button = find.byType(MoonFilledButton);

expect(button, findsOneWidget);

await tester.tap(button);
await tester.pumpAndSettle();

expect(find.byWidget(content), findsOneWidget);

await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();

expect(find.byWidget(content), findsOneWidget);
});
testWidgets("Botton sheet is expanded", (tester) async {
await tester.pumpWidget(
const TestWidget(
key: key,
content: content,
isExpanded: true,
),
);
final button = find.byType(MoonFilledButton);

expect(button, findsOneWidget);

await tester.tap(button);
await tester.pumpAndSettle();

expect(find.byWidget(content), findsOneWidget);

await tester.tapAt(const Offset(1, 1));
await tester.pumpAndSettle();

expect(find.byWidget(content), findsOneWidget);

await tester.tapAt(const Offset(100, 1));
await tester.pumpAndSettle();

expect(find.byWidget(content), findsOneWidget);
});
}

const String bottomSheetText = "Botton sheet content";
const Widget content = Text(bottomSheetText);

class TestWidget extends StatelessWidget {
final Widget content;
final bool isDismissible;
final bool isExpanded;

const TestWidget({
required this.content,
this.isDismissible = true,
this.isExpanded = false,
super.key,
});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: StatefulBuilder(
builder: (context, setState) {
return MoonFilledButton(
label: const Text("Tap me"),
onTap: () => bottomSheetBuilder(context),
);
},
),
),
),
);
}

Future<dynamic> bottomSheetBuilder(BuildContext context) {
return showMoonModalBottomSheet(
isExpanded: isExpanded,
context: context,
backgroundColor: Colors.black38,
enableDrag: true,
isDismissible: isDismissible,
height: MediaQuery.of(context).size.height * 0.5,
builder: (BuildContext context) => content,
);
}
}
117 changes: 117 additions & 0 deletions test/button_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moon_design/moon_design.dart';

void main() {
const key = Key("button_test");

testWidgets("Provided key is used", (tester) async {
await tester.pumpWidget(
const TestWidget(
key: key,
),
);
expect(find.byKey(key), findsOneWidget);
});

testWidgets("Button with leading, trailing, label", (tester) async {
await tester.pumpWidget(
const TestWidget(
showLeading: true,
showLabel: true,
showTrailing: true,
),
);
expect(find.text(buttonLabel), findsOneWidget);
expect(find.byIcon(trailingIcon), findsOneWidget);
expect(find.byIcon(leadinIcon), findsOneWidget);
});

testWidgets("Button tap", (tester) async {
bool tapped = false;
await tester.pumpWidget(
TestWidget(
onTap: () => tapped = true,
),
);
await tester.tap(find.byType(MoonButton));
await tester.pumpAndSettle();
expect(tapped, true);
});

testWidgets("Button long press", (tester) async {
bool longPressed = false;
bool tapped = false;
await tester.pumpWidget(
TestWidget(
onLongPress: () => longPressed = true,
onTap: () => tapped = true,
),
);
await tester.longPress(find.byType(MoonButton));
await tester.pumpAndSettle();
expect(longPressed, true);
expect(tapped, false);
});
}

const String buttonLabel = "Button label";
const IconData leadinIcon = MoonIcons.other_frame_24_light;
const IconData trailingIcon = MoonIcons.controls_close_small_24_light;

class TestWidget extends StatefulWidget {
final bool showLeading;
final bool showTrailing;
final bool showLabel;
final VoidCallback? onTap;
final VoidCallback? onLongPress;
final Key? buttonKey;

const TestWidget({
super.key,
this.showLeading = false,
this.showTrailing = false,
this.showLabel = false,
this.buttonKey,
this.onTap,
this.onLongPress,
});
@override
State<TestWidget> createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
bool _showAlert = true;

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: MoonButton(
key: widget.buttonKey,
onLongPress: widget.onLongPress,
onTap: widget.onTap,
leading: widget.showLeading ? const Icon(leadinIcon) : null,
label: widget.showLabel
? const SizedBox(
height: 24,
child: Align(
alignment: AlignmentDirectional.centerStart,
child: Text(buttonLabel),
),
)
: null,
trailing: widget.showTrailing
? MoonButton.icon(
buttonSize: MoonButtonSize.xs,
disabledOpacityValue: 1,
icon: const Icon(trailingIcon, size: 24),
gap: 0,
onTap: () => setState(() => _showAlert = !_showAlert),
)
: null,
),
),
);
}
}
Loading