test
This commit is contained in:
147
test/unit/packages/inspection/widgets/captcha_widget_test.dart
Normal file
147
test/unit/packages/inspection/widgets/captcha_widget_test.dart
Normal file
@@ -0,0 +1,147 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:rasadyar_inspection/presentation/widget/captcha/logic.dart';
|
||||
import 'package:rasadyar_inspection/presentation/widget/captcha/view.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
class MockCaptchaWidgetLogic extends GetxController
|
||||
with StateMixin<CaptchaModel>, Mock implements CaptchaWidgetLogic {
|
||||
@override
|
||||
GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
TextEditingController captchaController = TextEditingController();
|
||||
|
||||
@override
|
||||
void getCaptcha() {}
|
||||
}
|
||||
|
||||
class CaptchaModel {
|
||||
final String captchaImage;
|
||||
final String captchaToken;
|
||||
|
||||
CaptchaModel({required this.captchaImage, required this.captchaToken});
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('CaptchaWidget Tests', () {
|
||||
late MockCaptchaWidgetLogic mockController;
|
||||
|
||||
setUp(() {
|
||||
mockController = MockCaptchaWidgetLogic();
|
||||
Get.put<CaptchaWidgetLogic>(mockController);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
Get.reset();
|
||||
});
|
||||
|
||||
testWidgets('should display captcha container with correct styling', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
|
||||
.thenReturn(Container());
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: CaptchaWidget(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
expect(find.byType(Row), findsOneWidget);
|
||||
expect(find.byType(GestureDetector), findsOneWidget);
|
||||
expect(find.byType(Container), findsWidgets);
|
||||
|
||||
final container = tester.widget<Container>(find.byType(Container).first);
|
||||
expect(container.clipBehavior, Clip.antiAliasWithSaveLayer);
|
||||
});
|
||||
|
||||
testWidgets('should have correct form structure', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
|
||||
.thenReturn(Container());
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: CaptchaWidget(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
expect(find.byType(Form), findsOneWidget);
|
||||
expect(find.byType(Expanded), findsOneWidget);
|
||||
|
||||
final form = tester.widget<Form>(find.byType(Form));
|
||||
expect(form.autovalidateMode, AutovalidateMode.disabled);
|
||||
});
|
||||
|
||||
testWidgets('should handle tap on captcha container', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
|
||||
.thenReturn(Container());
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: CaptchaWidget(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Find and tap the gesture detector
|
||||
await tester.tap(find.byType(GestureDetector));
|
||||
await tester.pump();
|
||||
|
||||
// Assert
|
||||
verify(() => mockController.getCaptcha()).called(1);
|
||||
});
|
||||
|
||||
testWidgets('should display correct row layout', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
|
||||
.thenReturn(Container());
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: CaptchaWidget(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
final row = tester.widget<Row>(find.byType(Row));
|
||||
expect(row.crossAxisAlignment, CrossAxisAlignment.start);
|
||||
});
|
||||
|
||||
testWidgets('should have SizedBox with correct width', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
|
||||
.thenReturn(Container());
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: CaptchaWidget(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
expect(find.byType(SizedBox), findsOneWidget);
|
||||
final sizedBox = tester.widget<SizedBox>(find.byType(SizedBox));
|
||||
expect(sizedBox.width, 8);
|
||||
});
|
||||
});
|
||||
}
|
||||
162
test/unit/packages/inspection/widgets/search_widget_test.dart
Normal file
162
test/unit/packages/inspection/widgets/search_widget_test.dart
Normal file
@@ -0,0 +1,162 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:rasadyar_inspection/presentation/widget/search.dart';
|
||||
import 'package:rasadyar_inspection/presentation/widget/base_page/logic.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
class MockBaseLogic extends GetxController with Mock implements BaseLogic {
|
||||
@override
|
||||
RxBool showSearch = false.obs;
|
||||
|
||||
@override
|
||||
RxnString searchValue = RxnString();
|
||||
|
||||
@override
|
||||
void setSearchCallback(void Function(String?)? callback) {}
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('SearchWidget Tests', () {
|
||||
late MockBaseLogic mockController;
|
||||
late void Function(String?)? mockCallback;
|
||||
|
||||
setUp(() {
|
||||
mockController = MockBaseLogic();
|
||||
mockCallback = (String? value) {};
|
||||
|
||||
Get.put<BaseLogic>(mockController);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
Get.reset();
|
||||
});
|
||||
|
||||
testWidgets('should create SearchWidget with callback', (WidgetTester tester) async {
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SearchWidget(onSearchChanged: mockCallback),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
expect(find.byType(SearchWidget), findsOneWidget);
|
||||
verify(() => mockController.setSearchCallback(any())).called(1);
|
||||
});
|
||||
|
||||
testWidgets('should show animated container when search is visible', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
mockController.showSearch.value = true;
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SearchWidget(onSearchChanged: mockCallback),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
expect(find.byType(AnimatedContainer), findsOneWidget);
|
||||
expect(find.byType(Visibility), findsOneWidget);
|
||||
|
||||
final visibility = tester.widget<Visibility>(find.byType(Visibility));
|
||||
expect(visibility.visible, true);
|
||||
});
|
||||
|
||||
testWidgets('should hide search when showSearch is false', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
mockController.showSearch.value = false;
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SearchWidget(onSearchChanged: mockCallback),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
|
||||
// Assert
|
||||
final animatedContainer = tester.widget<AnimatedContainer>(find.byType(AnimatedContainer));
|
||||
expect(animatedContainer.height, 0);
|
||||
});
|
||||
|
||||
testWidgets('should display correct height when search is visible', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
mockController.showSearch.value = true;
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SearchWidget(onSearchChanged: mockCallback),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
final animatedContainer = tester.widget<AnimatedContainer>(find.byType(AnimatedContainer));
|
||||
expect(animatedContainer.height, 40);
|
||||
expect(animatedContainer.duration, const Duration(milliseconds: 300));
|
||||
expect(animatedContainer.curve, Curves.easeInOut);
|
||||
});
|
||||
|
||||
testWidgets('should have correct padding', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
mockController.showSearch.value = true;
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SearchWidget(onSearchChanged: mockCallback),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
expect(find.byType(Padding), findsOneWidget);
|
||||
final padding = tester.widget<Padding>(find.byType(Padding));
|
||||
expect(padding.padding, const EdgeInsets.symmetric(horizontal: 8));
|
||||
});
|
||||
|
||||
testWidgets('should work without callback', (WidgetTester tester) async {
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SearchWidget(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert
|
||||
expect(find.byType(SearchWidget), findsOneWidget);
|
||||
verify(() => mockController.setSearchCallback(null)).called(1);
|
||||
});
|
||||
|
||||
testWidgets('should have text editing controller', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
mockController.showSearch.value = true;
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SearchWidget(onSearchChanged: mockCallback),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Assert - The widget should contain text editing functionality
|
||||
expect(find.byType(SearchWidget), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user