Skip to content

Commit

Permalink
test: add test for button components
Browse files Browse the repository at this point in the history
- add some tests for different states of `AdaptivePrimaryButton`
- add some tests for different states of `AdaptiveSecondaryButton`
  • Loading branch information
PouriaMoradi021 committed Jan 16, 2025
1 parent f6f819f commit 98aeec4
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pactus_gui_widgetbook/app_styles.dart';
import 'package:pactus_gui_widgetbook/src/core/enum/request_state_enum.dart';
import 'package:pactus_gui_widgetbook/src/features/widgets/buttons/adaptive_primary_button/adaptive_primary_button.dart';

void main() {
group('AdaptivePrimaryButton', () {
late bool wasPressed;

setUp(() {
wasPressed = false;
});

Widget createTestableWidget({
required String title,
required RequestStateEnum requestState,
required VoidCallback? onPressed,
required FluentThemeData themeWithAccent,
}) {
return FluentApp(
debugShowCheckedModeBanner: false,
theme: themeWithAccent,
home: AppTheme(
themeData: themeWithAccent,
child: ScaffoldPage(
content: Center(
child: AdaptivePrimaryButton(
title: title,
requestState: requestState,
onPressed: onPressed,
),
),
),
),
);
}

testWidgets('renders correctly and is clickable when idle', (WidgetTester tester) async {
const testTitle = 'Test Button';
final themeWithAccent = AppThemeData.lightTheme(const Color(0xFF0A4D7E));

await tester.pumpWidget(createTestableWidget(
title: testTitle,
requestState: RequestStateEnum.loaded,
onPressed: () {
wasPressed = true;
},
themeWithAccent: themeWithAccent,
));

expect(find.text(testTitle), findsOneWidget);

final button = find.byType(FilledButton);
expect(tester.widget<FilledButton>(button).onPressed, isNotNull);

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

expect(wasPressed, isTrue);
});

testWidgets('renders correctly and is disabled when loading', (WidgetTester tester) async {
const testTitle = 'Loading Button';
final themeWithAccent = AppThemeData.lightTheme(const Color(0xFF0A4D7E));

await tester.pumpWidget(createTestableWidget(
title: testTitle,
requestState: RequestStateEnum.loading,
onPressed: () {
wasPressed = true;
},
themeWithAccent: themeWithAccent,
));

expect(find.text(testTitle), findsNothing);

expect(find.byType(SizedBox), findsWidgets);

final button = find.byType(FilledButton);
expect(tester.widget<FilledButton>(button).onPressed, isNull);

await tester.tap(button);
await tester.pumpAndSettle();
expect(wasPressed, isFalse);
});


testWidgets('renders correctly with dark theme', (WidgetTester tester) async {
const testTitle = 'Dark Theme Button';
final themeWithAccent = AppThemeData.darkTheme(const Color(0xFF0F6CBD));

await tester.pumpWidget(createTestableWidget(
title: testTitle,
requestState: RequestStateEnum.loaded,
onPressed: () {
wasPressed = true;
},
themeWithAccent: themeWithAccent,
));

expect(find.text(testTitle), findsOneWidget);

final button = find.byType(FilledButton);
expect(tester.widget<FilledButton>(button).onPressed, isNotNull);

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

expect(wasPressed, isTrue);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pactus_gui_widgetbook/app_styles.dart';
import 'package:pactus_gui_widgetbook/src/core/enum/request_state_enum.dart';
import 'package:pactus_gui_widgetbook/src/features/widgets/buttons/adaptive_secondary_button/adaptive_secondary_button.dart';

void main() {
group('AdaptiveSecondaryButton', () {
late bool wasPressed;

setUp(() {
wasPressed = false;
});

Widget createTestableWidget({
required String title,
required RequestStateEnum requestState,
required VoidCallback? onPressed,
required FluentThemeData themeWithAccent,
}) {
return FluentApp(
debugShowCheckedModeBanner: false,
theme: themeWithAccent,
home: AppTheme(
themeData: themeWithAccent,
child: ScaffoldPage(
content: Center(
child: AdaptiveSecondaryButton(
title: title,
requestState: requestState,
onPressed: onPressed,
),
),
),
),
);
}

testWidgets('renders correctly and is clickable when idle', (WidgetTester tester) async {
const testTitle = 'Test Button';
final themeWithAccent = AppThemeData.lightTheme(const Color(0xFF0A4D7E));

await tester.pumpWidget(createTestableWidget(
title: testTitle,
requestState: RequestStateEnum.loaded,
onPressed: () {
wasPressed = true;
},
themeWithAccent: themeWithAccent,
));

expect(find.text(testTitle), findsOneWidget);

final button = find.byType(OutlinedButton);
expect(tester.widget<OutlinedButton>(button).onPressed, isNotNull);

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

expect(wasPressed, isTrue);
});

testWidgets('renders correctly and is disabled when loading', (WidgetTester tester) async {
const testTitle = 'Loading Button';
final themeWithAccent = AppThemeData.lightTheme(const Color(0xFF0A4D7E));

await tester.pumpWidget(createTestableWidget(
title: testTitle,
requestState: RequestStateEnum.loading,
onPressed: () {
wasPressed = true;
},
themeWithAccent: themeWithAccent,
));

expect(find.text(testTitle), findsNothing);

expect(find.byType(SizedBox), findsWidgets);

final button = find.byType(OutlinedButton);
expect(tester.widget<OutlinedButton>(button).onPressed, isNull);

await tester.tap(button);
await tester.pumpAndSettle();
expect(wasPressed, isFalse);
});

testWidgets('renders correctly with dark theme', (WidgetTester tester) async {
const testTitle = 'Dark Theme Button';
final themeWithAccent = AppThemeData.darkTheme(const Color(0xFF0F6CBD));

await tester.pumpWidget(createTestableWidget(
title: testTitle,
requestState: RequestStateEnum.loaded,
onPressed: () {
wasPressed = true;
},
themeWithAccent: themeWithAccent,
));

expect(find.text(testTitle), findsOneWidget);

final button = find.byType(OutlinedButton);
expect(tester.widget<OutlinedButton>(button).onPressed, isNotNull);

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

expect(wasPressed, isTrue);
});
});
}

0 comments on commit 98aeec4

Please sign in to comment.