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 toast, text input, text area, tag, tab bar, switch, segment control, radio #333

Merged
merged 4 commits into from
Jan 3, 2024
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
6 changes: 3 additions & 3 deletions test/alert_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() {
expect(find.text(alertTitle), findsOneWidget);
expect(find.text(alertBody), findsOneWidget);
expect(find.byIcon(trailingIcon), findsOneWidget);
expect(find.byIcon(leadinIcon), findsOneWidget);
expect(find.byIcon(leadingIcon), findsOneWidget);
});

testWidgets("Hide alert", (tester) async {
Expand All @@ -59,7 +59,7 @@ void main() {

const String alertTitle = "Alert title";
const String alertBody = "Alert body";
const IconData leadinIcon = MoonIcons.other_frame_24_light;
const IconData leadingIcon = MoonIcons.other_frame_24_light;
const IconData trailingIcon = MoonIcons.controls_close_small_24_light;

class TestWidget extends StatefulWidget {
Expand All @@ -86,7 +86,7 @@ class _TestWidgetState extends State<TestWidget> {
home: Scaffold(
body: MoonAlert(
show: _showAlert,
leading: widget.showLeading ? const Icon(leadinIcon) : null,
leading: widget.showLeading ? const Icon(leadingIcon) : null,
title: const SizedBox(
height: 24,
child: Align(
Expand Down
6 changes: 3 additions & 3 deletions test/button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() {
);
expect(find.text(buttonLabel), findsOneWidget);
expect(find.byIcon(trailingIcon), findsOneWidget);
expect(find.byIcon(leadinIcon), findsOneWidget);
expect(find.byIcon(leadingIcon), findsOneWidget);
});

testWidgets("Button tap", (tester) async {
Expand Down Expand Up @@ -56,7 +56,7 @@ void main() {
}

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

class TestWidget extends StatefulWidget {
Expand Down Expand Up @@ -91,7 +91,7 @@ class _TestWidgetState extends State<TestWidget> {
key: widget.buttonKey,
onLongPress: widget.onLongPress,
onTap: widget.onTap,
leading: widget.showLeading ? const Icon(leadinIcon) : null,
leading: widget.showLeading ? const Icon(leadingIcon) : null,
label: widget.showLabel
? const SizedBox(
height: 24,
Expand Down
6 changes: 3 additions & 3 deletions test/chip_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ void main() {
);
expect(find.text(label), findsOneWidget);
expect(find.byIcon(trailingIcon), findsOneWidget);
expect(find.byIcon(leadinIcon), findsOneWidget);
expect(find.byIcon(leadingIcon), findsOneWidget);
});
}

const String label = "Label";
const IconData leadinIcon = MoonIcons.other_frame_24_light;
const IconData leadingIcon = MoonIcons.other_frame_24_light;
const IconData trailingIcon = MoonIcons.controls_close_small_24_light;

class TestWidget extends StatelessWidget {
Expand All @@ -61,7 +61,7 @@ class TestWidget extends StatelessWidget {
home: Scaffold(
body: MoonChip(
key: widgetKey,
leading: showLeading ? const Icon(leadinIcon) : null,
leading: showLeading ? const Icon(leadingIcon) : null,
label: showLabel ? const Text(label) : null,
trailing: showTrailing ? const Icon(trailingIcon) : null,
),
Expand Down
6 changes: 3 additions & 3 deletions test/menu_item_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void main() {
);
expect(find.text(description), findsOneWidget);
expect(find.byIcon(trailingIcon), findsOneWidget);
expect(find.byIcon(leadinIcon), findsOneWidget);
expect(find.byIcon(leadingIcon), findsOneWidget);
});

testWidgets("Tap menu item", (tester) async {
Expand All @@ -50,7 +50,7 @@ void main() {

const String title = "Title";
const String description = "Description";
const IconData leadinIcon = MoonIcons.other_frame_24_light;
const IconData leadingIcon = MoonIcons.other_frame_24_light;
const IconData trailingIcon = MoonIcons.controls_close_small_24_light;

class TestWidget extends StatelessWidget {
Expand All @@ -74,7 +74,7 @@ class TestWidget extends StatelessWidget {
home: Scaffold(
body: MoonMenuItem(
key: widgetKey,
leading: showLeading ? const Icon(leadinIcon) : null,
leading: showLeading ? const Icon(leadingIcon) : null,
description: showDiscription ? const Text(description) : null,
title: const Text(title),
trailing: showTrailing ? const Icon(trailingIcon) : null,
Expand Down
206 changes: 206 additions & 0 deletions test/radio_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moon_design/moon_design.dart';

enum Choice { first, second }

void main() {
const firstKey = Key("first_radio_test");
const secondKey = Key("second_radio_test");

Finder findSelecterFirstRadio() {
return find.byWidgetPredicate(
(widget) =>
widget is MoonRadio &&
widget.key == firstKey &&
widget.value == Choice.first &&
widget.groupValue == Choice.first,
);
}

Finder findSelecterSecondRadio() {
return find.byWidgetPredicate(
(widget) =>
widget is MoonRadio &&
widget.key == secondKey &&
widget.value == Choice.second &&
widget.groupValue == Choice.second,
);
}

testWidgets("Provided key is used", (tester) async {
await tester.pumpWidget(
const TestRadioWidget(
firstRadioKey: firstKey,
),
);

expect(
find.byWidgetPredicate(
(widget) => widget is MoonRadio && widget.key == firstKey,
),
findsOneWidget,
);
});

testWidgets("Test radio with label", (tester) async {
await tester.pumpWidget(
const TestRadioWithLabelWidget(),
);

expect(find.text(label1), findsOneWidget);
expect(find.text(label2), findsOneWidget);
});

testWidgets("Test radio switch", (tester) async {
await tester.pumpWidget(
const TestRadioWidget(
firstRadioKey: firstKey,
secondRadioKey: secondKey,
),
);

await tester.tap(find.byKey(firstKey));
await tester.pumpAndSettle();

expect(
findSelecterFirstRadio(),
findsOneWidget,
);

await tester.tap(find.byKey(secondKey));
await tester.pumpAndSettle();
expect(findSelecterFirstRadio(), findsNothing);
expect(findSelecterSecondRadio(), findsOneWidget);
await tester.tap(find.byKey(secondKey));
await tester.pumpAndSettle();
expect(findSelecterFirstRadio(), findsNothing);
expect(findSelecterSecondRadio(), findsOneWidget);
});

testWidgets("Test toggable radio switch", (tester) async {
await tester.pumpWidget(
const TestRadioWidget(
firstRadioKey: firstKey,
secondRadioKey: secondKey,
toggleable: true,
),
);

await tester.tap(find.byKey(firstKey));
await tester.pumpAndSettle();

expect(
findSelecterFirstRadio(),
findsOneWidget,
);

await tester.tap(find.byKey(secondKey));
await tester.pumpAndSettle();
expect(findSelecterFirstRadio(), findsNothing);
expect(findSelecterSecondRadio(), findsOneWidget);
await tester.tap(find.byKey(secondKey));
await tester.pumpAndSettle();
expect(findSelecterFirstRadio(), findsNothing);
expect(findSelecterSecondRadio(), findsNothing);
});
}

class TestRadioWidget extends StatefulWidget {
final bool toggleable;

final Key? firstRadioKey;
final Key? secondRadioKey;
const TestRadioWidget({
super.key,
this.toggleable = false,
this.firstRadioKey,
this.secondRadioKey,
});
@override
State<TestRadioWidget> createState() => _TestRadioWidgetState();
}

class _TestRadioWidgetState extends State<TestRadioWidget> {
Choice? value;
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
body: Center(
child: SingleChildScrollView(
padding:
const EdgeInsets.symmetric(vertical: 64.0, horizontal: 16.0),
child: Column(
children: [
MoonRadio(
key: widget.firstRadioKey,
value: Choice.first,
groupValue: value,
onChanged: (Choice? choice) => setState(() => value = choice),
toggleable: widget.toggleable,
),
const SizedBox(height: 8),
MoonRadio(
value: Choice.second,
key: widget.secondRadioKey,
groupValue: value,
onChanged: (Choice? choice) => setState(() => value = choice),
toggleable: widget.toggleable,
),
],
),
),
),
),
);
}
}

const label1 = 'Label 1';
const label2 = 'Label 2';

class TestRadioWithLabelWidget extends StatefulWidget {
const TestRadioWithLabelWidget({super.key});
@override
State<TestRadioWithLabelWidget> createState() =>
_TestRadioWithLabelWidgetState();
}

class _TestRadioWithLabelWidgetState extends State<TestRadioWithLabelWidget> {
Choice? value;
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
body: Center(
child: SingleChildScrollView(
padding:
const EdgeInsets.symmetric(vertical: 64.0, horizontal: 16.0),
child: Column(
children: [
MoonRadio.withLabel(
context,
label: label1,
value: Choice.first,
groupValue: value,
onChanged: (Choice? choice) => setState(() => value = choice),
),
const SizedBox(height: 8),
MoonRadio.withLabel(
context,
value: Choice.second,
label: label2,
groupValue: value,
onChanged: (Choice? choice) => setState(() => value = choice),
),
],
),
),
),
),
);
}
}
Loading
Loading