-
Notifications
You must be signed in to change notification settings - Fork 0
/
location_search_page_test.dart
87 lines (63 loc) · 2.99 KB
/
location_search_page_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import 'package:flutter/material.dart';
import 'package:weather_app/location_search/location_search_api.dart';
import 'package:weather_app/main.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:logger/logger.dart';
import '../forecast/forecast_page_object.dart';
import '../test_dependencies.dart';
import 'location_search_api_builders.dart';
import 'location_search_page_object.dart';
void main() {
testWidgets('Search for locations', (WidgetTester tester) async {
final testDependencies = await tester.pumpWithTestDependencies(const App());
const searchApiUrl = 'https://geocoding-api.open-meteo.com/v1/search';
const searchUrl = '$searchApiUrl?name=Louisville&count=10&language=en&format=json';
await tester.goToSearch();
expect(find.byType(TextField), findsOneWidget);
testDependencies.http.stub((
url: searchUrl,
statusCode: 200,
body: buildLocationSearchJson(),
));
await tester.submitSearch('Louisville', settle: false);
await tester.pump();
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pump();
expect(find.text('Louisville'), findsNWidgets(3));
expect(find.text('Colorado'), findsOneWidget);
expect(find.text('Kentucky'), findsOneWidget);
final lastRequest = testDependencies.http.lastRequest();
expect(lastRequest, equals((method: 'GET', url: searchUrl, body: '')));
});
testWidgets('Search for locations, name URI encoding', (WidgetTester tester) async {
final testDependencies = await tester.pumpWithTestDependencies(const App());
testDependencies.http.stub((url: null, statusCode: 200, body: buildLocationSearchJson()));
await tester.goToSearch();
await tester.submitSearch('Louisville Colorado');
final lastRequest = testDependencies.http.lastRequest();
const uriEncodedName = 'Louisville%20Colorado';
const expectedUrl = '$searchApiUrl?name=$uriEncodedName&count=10&language=en&format=json';
expect(lastRequest?.url, equals(expectedUrl));
});
testWidgets('Search for locations, on http error', (WidgetTester tester) async {
final testDependencies = await tester.pumpWithTestDependencies(const App());
testDependencies.http.stub((url: null, statusCode: 400, body: buildLocationSearchJson()));
await tester.goToSearch();
await tester.submitSearch('Louisville');
expect(find.text('Unexpected response from api'), findsOneWidget);
});
testWidgets('Search for locations, on invalid json', (WidgetTester tester) async {
Logger.level = Level.nothing;
final invalidResultsJson = {
'results': [
{'name': 1, 'region': 'Kentucky'},
{'name': 2, 'region': 'Colorado'},
]
};
final testDependencies = await tester.pumpWithTestDependencies(const App());
testDependencies.http.stub((url: null, statusCode: 200, body: invalidResultsJson));
await tester.goToSearch();
await tester.submitSearch('Louisville');
expect(find.text('Failed to parse response'), findsOneWidget);
});
}