cyberhybridhub/test/widgets/swipe_question_tile_test.dart
nathanAndrinoid a521a74214
Some checks failed
Admin portal tests / test (push) Has been cancelled
balance controled working spelling and market questions.
2026-06-17 04:20:45 +00:00

132 lines
3.7 KiB
Dart

import 'package:cyberhybridhub/services/questions_hub_service.dart';
import 'package:cyberhybridhub/widgets/swipe_question_tile.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
setUp(() {
// Ensure the panel is marked open so the tile captures keyboard events.
QuestionsHubService.instance.questionPanelOpen.value = true;
});
tearDown(() {
QuestionsHubService.instance.questionPanelOpen.value = false;
});
testWidgets('allows swiping right at zero to submit answer', (tester) async {
num? submittedValue;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SwipeQuestionTile(
questionId: 'test-q-1',
onSwipeRight: (val) async {
submittedValue = val;
return null;
},
onSwipeLeft: () async {},
),
),
),
);
// Swipe right starting from the center of the widget.
// Swipe distance needs to exceed _swipeThreshold (96).
final Finder tileFinder = find.byType(SwipeQuestionTile);
final Offset start = tester.getCenter(tileFinder);
// Drag right by 200 pixels.
await tester.dragFrom(start, const Offset(200, 0));
await tester.pumpAndSettle();
expect(submittedValue, 0);
});
testWidgets('allows swiping left at zero to defer question', (tester) async {
bool deferred = false;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SwipeQuestionTile(
questionId: 'test-q-2',
onSwipeRight: (_) async => null,
onSwipeLeft: () async {
deferred = true;
},
),
),
),
);
// Swipe left starting from the center of the widget.
final Finder tileFinder = find.byType(SwipeQuestionTile);
final Offset start = tester.getCenter(tileFinder);
// Drag left by 200 pixels.
await tester.dragFrom(start, const Offset(-200, 0));
await tester.pumpAndSettle();
expect(deferred, true);
});
testWidgets('allows right arrow key holding/press at zero to submit answer', (tester) async {
num? submittedValue;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SwipeQuestionTile(
questionId: 'test-q-3',
onSwipeRight: (val) async {
submittedValue = val;
return null;
},
onSwipeLeft: () async {},
),
),
),
);
await tester.pump();
// Simulate holding right arrow key.
await tester.sendKeyDownEvent(LogicalKeyboardKey.arrowRight);
await tester.pump();
await tester.pump(const Duration(seconds: 2)); // Wait past hold edge duration (1s)
await tester.sendKeyUpEvent(LogicalKeyboardKey.arrowRight);
await tester.pumpAndSettle();
expect(submittedValue, 0);
});
testWidgets('allows left arrow key holding/press at zero to defer question', (tester) async {
bool deferred = false;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SwipeQuestionTile(
questionId: 'test-q-4',
onSwipeRight: (_) async => null,
onSwipeLeft: () async {
deferred = true;
},
),
),
),
);
await tester.pump();
// Simulate holding left arrow key.
await tester.sendKeyDownEvent(LogicalKeyboardKey.arrowLeft);
await tester.pump();
await tester.pump(const Duration(seconds: 2));
await tester.sendKeyUpEvent(LogicalKeyboardKey.arrowLeft);
await tester.pumpAndSettle();
expect(deferred, true);
});
}