feat: add ability to cancel in-flight HTTP requests and manage client lifecycle

This commit is contained in:
sasanktumpati
2024-12-07 13:06:15 +05:30
parent 596cf05576
commit 036bac311c
7 changed files with 169 additions and 92 deletions

View File

@ -2,57 +2,108 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:apidash/consts.dart';
import 'package:apidash/widgets/button_send.dart';
import '../test_consts.dart';
void main() {
testWidgets('Testing for Send Request button', (tester) async {
dynamic changedValue;
await tester.pumpWidget(
MaterialApp(
title: 'Send Request button',
theme: kThemeDataLight,
home: Scaffold(
body: SendButton(
isWorking: false,
onTap: () {
changedValue = 'Send';
group('SendButton', () {
testWidgets('renders send state correctly when not working',
(tester) async {
bool sendPressed = false;
bool cancelPressed = false;
await tester.pumpWidget(
MaterialApp(
theme: kThemeDataLight,
home: Scaffold(
body: SendButton(
isWorking: false,
onTap: () => sendPressed = true,
onCancel: () => cancelPressed = true,
),
),
),
);
// Verify initial send state
expect(find.byIcon(Icons.send), findsOneWidget);
expect(find.text(kLabelSend), findsOneWidget);
expect(find.byIcon(Icons.cancel), findsNothing);
expect(find.text(kLabelCancel), findsNothing);
// Tap and verify callback
await tester.tap(find.byType(FilledButton));
expect(sendPressed, isTrue);
expect(cancelPressed, isFalse);
});
testWidgets('renders cancel state correctly when working', (tester) async {
bool sendPressed = false;
bool cancelPressed = false;
await tester.pumpWidget(
MaterialApp(
theme: kThemeDataLight,
home: Scaffold(
body: SendButton(
isWorking: true,
onTap: () => sendPressed = true,
onCancel: () => cancelPressed = true,
),
),
),
);
// Verify initial cancel state
expect(find.byIcon(Icons.send), findsNothing);
expect(find.text(kLabelSend), findsNothing);
expect(find.byIcon(Icons.cancel), findsOneWidget);
expect(find.text(kLabelCancel), findsOneWidget);
// Tap and verify callback
await tester.tap(find.byType(FilledButton));
expect(sendPressed, isFalse);
expect(cancelPressed, isTrue);
});
testWidgets('updates UI when isWorking changes', (tester) async {
bool isWorking = false;
await tester.pumpWidget(
MaterialApp(
theme: kThemeDataLight,
home: StatefulBuilder(
builder: (context, setState) {
return Scaffold(
body: SendButton(
isWorking: isWorking,
onTap: () => setState(() => isWorking = true),
onCancel: () => setState(() => isWorking = false),
),
);
},
),
),
),
);
);
expect(find.byIcon(Icons.send), findsOneWidget);
expect(find.text(kLabelSend), findsOneWidget);
final button1 = find.byType(FilledButton);
expect(button1, findsOneWidget);
// Initial send state
expect(find.byIcon(Icons.send), findsOneWidget);
expect(find.text(kLabelSend), findsOneWidget);
await tester.tap(button1);
expect(changedValue, 'Send');
});
// Tap to start working
await tester.tap(find.byType(FilledButton));
await tester.pump();
testWidgets(
'Testing for Send Request button when RequestModel is viewed and is waiting for response',
(tester) async {
await tester.pumpWidget(
MaterialApp(
title: 'Send Request button',
theme: kThemeDataLight,
home: Scaffold(
body: SendButton(
isWorking: true,
onTap: () {},
),
),
),
);
// Verify cancel state
expect(find.byIcon(Icons.cancel), findsOneWidget);
expect(find.text(kLabelCancel), findsOneWidget);
expect(find.byIcon(Icons.send), findsNothing);
expect(find.text(kLabelSending), findsOneWidget);
final button1 = find.byType(FilledButton);
expect(button1, findsOneWidget);
// Tap to cancel
await tester.tap(find.byType(FilledButton));
await tester.pump();
expect(tester.widget<FilledButton>(button1).enabled, isFalse);
// Verify back to send state
expect(find.byIcon(Icons.send), findsOneWidget);
expect(find.text(kLabelSend), findsOneWidget);
});
});
}