Skip to content

Instantly share code, notes, and snippets.

@rubywai
Created June 7, 2025 07:08
Show Gist options
  • Select an option

  • Save rubywai/85e82fc7b513791b017f8d1c97047179 to your computer and use it in GitHub Desktop.

Select an option

Save rubywai/85e82fc7b513791b017f8d1c97047179 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_bloc_weather/features/search/data/model/city_model.dart';
import 'package:flutter_bloc_weather/features/search/data/services/search_api_services.dart';
import 'package:flutter_bloc_weather/features/search/ui/city_search_page.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:integration_test/integration_test.dart';
class FakeSearchService implements SearchApiServices {
@override
Future<CityModel> searchCities(
{required String name, required int count}) async {
await Future.delayed(const Duration(milliseconds: 300));
if (name == "error") {
throw Exception("API error");
}
return CityModel(results: [
CitySearchModel(
id: 1,
name: "Test City",
latitude: 1.0,
longitude: 1.0,
country: "Test Country",
),
]);
}
}
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
final sl = GetIt.instance;
setUpAll(() {
if (sl.isRegistered<SearchApiServices>()) {
sl.unregister<SearchApiServices>();
}
sl.registerSingleton<SearchApiServices>(FakeSearchService());
});
testWidgets('Integration test: Search success and error', (tester) async {
await tester.pumpWidget(const MaterialApp(home: CitySearchPage()));
// Enter valid text and tap search
await tester.enterText(find.byType(TextField), "test");
await tester.tap(find.byIcon(Icons.search_rounded));
await tester.pump(); // Triggers loading
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pumpAndSettle(); // Finish async call
expect(find.text("Test City"), findsOneWidget);
expect(find.text("Test Country"), findsOneWidget);
// Test error case
await tester.enterText(find.byType(TextField), "error");
await tester.tap(find.byIcon(Icons.search_rounded));
await tester.pumpAndSettle();
expect(find.text("Failed to search"), findsOneWidget);
expect(find.text("Try Again"), findsOneWidget);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment