Skip to content

Commit

Permalink
Add preliminary app test
Browse files Browse the repository at this point in the history
This is a step toward adding the ability to guess.
  • Loading branch information
backspace committed Jul 3, 2024
1 parent 2a84c71 commit e30812a
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 11 deletions.
19 changes: 11 additions & 8 deletions waydowntown/waydowntown_app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
runApp(const Waydowntown());
Expand Down Expand Up @@ -73,6 +71,7 @@ class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
final dio = Dio(BaseOptions());
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
Expand Down Expand Up @@ -114,7 +113,9 @@ class _MyHomePageState extends State<MyHomePage> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const RequestGameRoute()));
builder: (context) => RequestGameRoute(
dio: dio,
)));
}),
const Text(
'You have pushed the button this many times:',
Expand All @@ -136,7 +137,9 @@ class _MyHomePageState extends State<MyHomePage> {
}

class RequestGameRoute extends StatelessWidget {
const RequestGameRoute({super.key});
final Dio dio;

const RequestGameRoute({super.key, required this.dio});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -177,11 +180,11 @@ class RequestGameRoute extends StatelessWidget {
}

Future<Game> fetchGame() async {
final response = await http.post(
Uri.parse('http://localhost:3000/api/v1/games?include=incarnation'));
final response = await dio.post('http://localhost:3000/api/v1/games',
queryParameters: {'include': 'incarnation'});

if (response.statusCode == 201) {
return Game.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
return Game.fromJson(response.data);
} else {
throw Exception('Failed to load game');
}
Expand Down
87 changes: 87 additions & 0 deletions waydowntown/waydowntown_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.8"
dio:
dependency: "direct main"
description:
name: dio
sha256: "11e40df547d418cc0c4900a9318b26304e665da6fa4755399a9ff9efd09034b5"
url: "https://pub.dev"
source: hosted
version: "5.4.3+1"
fake_async:
dependency: transitive
description:
Expand All @@ -57,11 +65,24 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
file:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
flutter:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_driver:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
flutter_lints:
dependency: "direct dev"
description:
Expand All @@ -75,6 +96,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
fuchsia_remote_debug_protocol:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
Expand All @@ -83,6 +109,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.1"
http_mock_adapter:
dependency: "direct dev"
description:
name: http_mock_adapter
sha256: "46399c78bd4a0af071978edd8c502d7aeeed73b5fb9860bca86b5ed647a63c1b"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
http_parser:
dependency: transitive
description:
Expand All @@ -91,6 +125,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
integration_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
leak_tracker:
dependency: transitive
description:
Expand Down Expand Up @@ -123,6 +162,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
logger:
dependency: transitive
description:
name: logger
sha256: af05cc8714f356fd1f3888fb6741cbe9fbe25cdb6eedbab80e1a6db21047d4a4
url: "https://pub.dev"
source: hosted
version: "2.3.0"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -155,6 +202,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.0"
platform:
dependency: transitive
description:
name: platform
sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec"
url: "https://pub.dev"
source: hosted
version: "3.1.4"
pretty_dio_logger:
dependency: "direct main"
description:
name: pretty_dio_logger
sha256: "00b80053063935cf9a6190da344c5373b9d0e92da4c944c878ff2fbef0ef6dc2"
url: "https://pub.dev"
source: hosted
version: "1.3.1"
process:
dependency: transitive
description:
name: process
sha256: "21e54fd2faf1b5bdd5102afd25012184a6793927648ea81eea80552ac9405b32"
url: "https://pub.dev"
source: hosted
version: "5.0.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -192,6 +263,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.0"
sync_http:
dependency: transitive
description:
name: sync_http
sha256: "7f0cd72eca000d2e026bcd6f990b81d0ca06022ef4e32fb257b30d3d1014a961"
url: "https://pub.dev"
source: hosted
version: "0.3.1"
term_glyph:
dependency: transitive
description:
Expand Down Expand Up @@ -240,6 +319,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.5.1"
webdriver:
dependency: transitive
description:
name: webdriver
sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
sdks:
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
12 changes: 9 additions & 3 deletions waydowntown/waydowntown_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: waydowntown_app
description: "A new Flutter project."
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
publish_to: "none" # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
Expand All @@ -19,7 +19,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: '>=3.4.0 <4.0.0'
sdk: ">=3.4.0 <4.0.0"

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
Expand All @@ -31,29 +31,35 @@ dependencies:
flutter:
sdk: flutter

dio: ^5.4.3+1

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6
http: ^1.2.1
pretty_dio_logger: ^1.3.1

dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
flutter_driver:
sdk: flutter

# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^3.0.0
http_mock_adapter: ^0.6.1

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:

# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
Expand Down
60 changes: 60 additions & 0 deletions waydowntown/waydowntown_app/test/request_game_route_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
// import 'package:pretty_dio_logger/pretty_dio_logger.dart';

import 'package:waydowntown_app/main.dart';

void main() {
testWidgets('Game is requested and displayed', (WidgetTester tester) async {
final dio = Dio(BaseOptions());
// dio.interceptors.add(PrettyDioLogger());
final dioAdapter = DioAdapter(dio: dio);

dioAdapter.onPost(
'http://localhost:3000/api/v1/games',
(server) => server.reply(
201,
{
"data": {
"id": "22261813-2171-453f-a669-db08edc70d6d",
"type": "games",
"relationships": {
"incarnation": {
"links": {
"related":
"http://localhost:3000/api/v1/incarnations/0091eb84-85c8-4e63-962b-39e1a19d2781"
},
"data": {
"type": "incarnations",
"id": "0091eb84-85c8-4e63-962b-39e1a19d2781"
}
}
}
},
"included": [
{
"id": "0091eb84-85c8-4e63-962b-39e1a19d2781",
"type": "incarnations",
"attributes": {
"concept": "fill_in_the_blank",
"mask": "An enormous headline proclaims ____ quit!"
}
}
],
"meta": {}
},
// Delay the response by 1 second
delay: const Duration(seconds: 1),
),
);

await tester.pumpWidget(MaterialApp(home: RequestGameRoute(dio: dio)));
await tester.pumpAndSettle();

expect(find.text('fill_in_the_blank'), findsOneWidget);
expect(
find.text('An enormous headline proclaims ____ quit!'), findsOneWidget);
});
}

0 comments on commit e30812a

Please sign in to comment.