Skip to content

Commit

Permalink
Fix tests to use mock channel and run structure
Browse files Browse the repository at this point in the history
  • Loading branch information
backspace committed Nov 14, 2024
1 parent 9133bf6 commit 4dbf75f
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 44 deletions.
26 changes: 18 additions & 8 deletions waydowntown_app/test/games/bluetooth_collector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ import 'package:waydowntown/games/bluetooth_collector.dart';
import 'package:waydowntown/models/run.dart';

import '../test_helpers.dart';
import '../test_helpers.mocks.dart';

@GenerateMocks([FlutterBluePlusMockable, BluetoothDevice, ScanResult])
import 'bluetooth_collector_test.mocks.dart';

@GenerateMocks([
FlutterBluePlusMockable,
BluetoothDevice,
ScanResult,
])
void main() {
const submitAnswerRoute = '/waydowntown/submissions';

Expand All @@ -42,6 +39,8 @@ void main() {

late FlutterBluePlusMockable mockFlutterBluePlus;

late MockPhoenixChannel mockChannel;

setUpAll(() {
when(deviceResult1.device).thenReturn(device1);
when(device1.platformName).thenReturn("Device 1");
Expand All @@ -64,6 +63,8 @@ void main() {
dio.interceptors.add(PrettyDioLogger());
dioAdapter = DioAdapter(dio: dio);

(_, mockChannel, _) = TestHelpers.setupMockSocket();

when(mockFlutterBluePlus.adapterState)
.thenAnswer((_) => Stream.fromIterable([BluetoothAdapterState.on]));

Expand All @@ -82,7 +83,10 @@ void main() {

await tester.pumpWidget(MaterialApp(
home: BluetoothCollectorGame(
dio: dio, run: game, flutterBluePlus: mockFlutterBluePlus),
dio: dio,
run: game,
flutterBluePlus: mockFlutterBluePlus,
channel: mockChannel),
));

await tester.pumpAndSettle();
Expand Down Expand Up @@ -140,7 +144,10 @@ void main() {

await tester.pumpWidget(MaterialApp(
home: BluetoothCollectorGame(
dio: dio, run: game, flutterBluePlus: mockFlutterBluePlus),
dio: dio,
run: game,
flutterBluePlus: mockFlutterBluePlus,
channel: mockChannel),
));

await tester.pumpAndSettle();
Expand Down Expand Up @@ -234,7 +241,10 @@ void main() {

await tester.pumpWidget(MaterialApp(
home: BluetoothCollectorGame(
dio: dio, run: game, flutterBluePlus: mockFlutterBluePlus),
dio: dio,
run: game,
flutterBluePlus: mockFlutterBluePlus,
channel: mockChannel),
));

await tester.pumpAndSettle();
Expand Down
7 changes: 7 additions & 0 deletions waydowntown_app/test/games/cardinal_memory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:waydowntown/models/answer.dart';
import 'package:waydowntown/models/run.dart';

import '../test_helpers.dart';
import '../test_helpers.mocks.dart';

import 'cardinal_memory_test.mocks.dart';

Expand All @@ -29,6 +30,8 @@ void main() {
late Run game;
late MockMotionSensors mockMotionSensors;

late MockPhoenixChannel mockChannel;

setUp(() {
mockMotionSensors = MockMotionSensors();
dio = Dio(BaseOptions(baseUrl: dotenv.env['API_ROOT']!));
Expand All @@ -39,6 +42,8 @@ void main() {
const Answer(id: '2', label: 'west', order: 2),
const Answer(id: '3', label: 'south', order: 3),
]);

(_, mockChannel, _) = TestHelpers.setupMockSocket();
});

testWidgets('CardinalMemoryGame displays and submits pattern',
Expand Down Expand Up @@ -97,6 +102,7 @@ void main() {
home: CardinalMemoryGame(
dio: dio,
run: game,
channel: mockChannel,
motionSensors: mockMotionSensors,
),
));
Expand Down Expand Up @@ -193,6 +199,7 @@ void main() {
home: CardinalMemoryGame(
dio: dio,
run: shortRun,
channel: mockChannel,
motionSensors: mockMotionSensors,
),
));
Expand Down
19 changes: 16 additions & 3 deletions waydowntown_app/test/games/code_collector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:waydowntown/games/code_collector.dart';
import 'package:waydowntown/models/run.dart';

import '../test_helpers.dart';
import '../test_helpers.mocks.dart';

import 'code_collector_test.mocks.dart';

Expand All @@ -24,6 +25,7 @@ void main() {
late Dio dio;
late DioAdapter dioAdapter;
late Run game;
late MockPhoenixChannel mockChannel;

late MockMobileScannerController mockController;

Expand All @@ -36,6 +38,8 @@ void main() {
dioAdapter = DioAdapter(dio: dio);
game = TestHelpers.createMockRun(
concept: 'code_collector', correctAnswers: 0, totalAnswers: 5);

(_, mockChannel, _) = TestHelpers.setupMockSocket();
});

testWidgets('CodeCollectorGame displays scanned codes',
Expand All @@ -56,7 +60,10 @@ void main() {

await tester.pumpWidget(MaterialApp(
home: CodeCollectorGame(
dio: dio, run: game, scannerController: mockController),
dio: dio,
run: game,
channel: mockChannel,
scannerController: mockController),
));

expect(find.text('Parent Region > Test Region'), findsOneWidget);
Expand Down Expand Up @@ -133,7 +140,10 @@ void main() {

await tester.pumpWidget(MaterialApp(
home: CodeCollectorGame(
dio: dio, run: game, scannerController: mockController),
dio: dio,
run: game,
channel: mockChannel,
scannerController: mockController),
));

streamController.add(const BarcodeCapture(
Expand Down Expand Up @@ -240,7 +250,10 @@ void main() {

await tester.pumpWidget(MaterialApp(
home: CodeCollectorGame(
dio: dio, run: game, scannerController: mockController),
dio: dio,
run: game,
channel: mockChannel,
scannerController: mockController),
));

streamController.add(const BarcodeCapture(
Expand Down
20 changes: 17 additions & 3 deletions waydowntown_app/test/games/food_court_frenzy_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:waydowntown/models/answer.dart';
import 'package:waydowntown/models/run.dart';

import '../test_helpers.dart';
import '../test_helpers.mocks.dart';

void main() {
dotenv.testLoad(fileInput: File('.env').readAsStringSync());
Expand All @@ -20,6 +21,7 @@ void main() {
late Dio dio;
late DioAdapter dioAdapter;
late Run run;
late MockPhoenixChannel mockChannel;

setUp(() {
dio = Dio(BaseOptions(baseUrl: dotenv.env['API_ROOT']!));
Expand All @@ -34,6 +36,8 @@ void main() {
const Answer(id: '3', label: 'Sushi'),
],
);

(_, mockChannel, _) = TestHelpers.setupMockSocket();
});

testWidgets('FoodCourtFrenzyGame displays and submits answers',
Expand Down Expand Up @@ -74,7 +78,9 @@ void main() {
);

await tester.pumpWidget(
MaterialApp(home: FoodCourtFrenzyGame(run: run, dio: dio)),
MaterialApp(
home: FoodCourtFrenzyGame(run: run, dio: dio, channel: mockChannel),
),
);
await tester.pumpAndSettle();

Expand Down Expand Up @@ -133,7 +139,9 @@ void main() {
);

await tester.pumpWidget(
MaterialApp(home: FoodCourtFrenzyGame(run: run, dio: dio)),
MaterialApp(
home: FoodCourtFrenzyGame(run: run, dio: dio, channel: mockChannel),
),
);
await tester.pumpAndSettle();

Expand Down Expand Up @@ -176,7 +184,13 @@ void main() {
);

await tester.pumpWidget(
MaterialApp(home: FoodCourtFrenzyGame(run: singleAnswerGame, dio: dio)),
MaterialApp(
home: FoodCourtFrenzyGame(
run: singleAnswerGame,
dio: dio,
channel: mockChannel,
),
),
);
await tester.pumpAndSettle();

Expand Down
6 changes: 6 additions & 0 deletions waydowntown_app/test/games/orientation_memory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:waydowntown/models/answer.dart';
import 'package:waydowntown/models/run.dart';

import '../test_helpers.dart';
import '../test_helpers.mocks.dart';

import 'orientation_memory_test.mocks.dart';

Expand All @@ -28,6 +29,7 @@ void main() {
late DioAdapter dioAdapter;
late Run game;
late MockMotionSensors mockMotionSensors;
late MockPhoenixChannel mockChannel;

setUp(() {
mockMotionSensors = MockMotionSensors();
Expand All @@ -39,6 +41,8 @@ void main() {
const Answer(id: '2', label: 'right', order: 2),
const Answer(id: '3', label: 'left', order: 3),
]);

(_, mockChannel, _) = TestHelpers.setupMockSocket();
});

testWidgets('OrientationMemoryGame displays and submits pattern',
Expand Down Expand Up @@ -97,6 +101,7 @@ void main() {
home: OrientationMemoryGame(
dio: dio,
run: game,
channel: mockChannel,
motionSensors: mockMotionSensors,
),
));
Expand Down Expand Up @@ -195,6 +200,7 @@ void main() {
home: OrientationMemoryGame(
dio: dio,
run: shortRun,
channel: mockChannel,
motionSensors: mockMotionSensors,
),
));
Expand Down
19 changes: 13 additions & 6 deletions waydowntown_app/test/games/single_string_input_game_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:waydowntown/models/answer.dart';
import 'package:waydowntown/models/run.dart';

import '../test_helpers.dart';
import '../test_helpers.mocks.dart';

void main() {
dotenv.testLoad(fileInput: File('.env').readAsStringSync());
Expand All @@ -20,6 +21,7 @@ void main() {
late Dio dio;
late DioAdapter dioAdapter;
late Run run;
late MockPhoenixChannel mockChannel;

setUp(() {
dio = Dio(BaseOptions(baseUrl: dotenv.env['API_ROOT']!));
Expand All @@ -28,6 +30,8 @@ void main() {
run = TestHelpers.createMockRun(concept: 'fill_in_the_blank', answers: [
const Answer(id: '1', label: 'An enormous headline proclaims ____ quit!')
]);

(_, mockChannel, _) = TestHelpers.setupMockSocket();
});

testWidgets('Run is requested, displayed, and answers are posted',
Expand All @@ -50,8 +54,9 @@ void main() {
totalAnswers: 1,
isComplete: true));

await tester.pumpWidget(
MaterialApp(home: SingleStringInputGame(run: run, dio: dio)));
await tester.pumpWidget(MaterialApp(
home: SingleStringInputGame(run: run, dio: dio, channel: mockChannel),
));
await tester.pumpAndSettle();

expect(tester.testTextInput.isRegistered, isTrue);
Expand Down Expand Up @@ -102,8 +107,9 @@ void main() {
isComplete: true,
answerId: '1'));

await tester.pumpWidget(
MaterialApp(home: SingleStringInputGame(run: run, dio: dio)));
await tester.pumpWidget(MaterialApp(
home: SingleStringInputGame(run: run, dio: dio, channel: mockChannel),
));
await tester.pumpAndSettle();

final textField = find.byType(TextField);
Expand Down Expand Up @@ -134,8 +140,9 @@ void main() {
concept: 'count_the_items',
description: 'How many trees can you see in the courtyard?');

await tester.pumpWidget(
MaterialApp(home: SingleStringInputGame(run: run, dio: dio)));
await tester.pumpWidget(MaterialApp(
home: SingleStringInputGame(run: run, dio: dio, channel: mockChannel),
));
await tester.pumpAndSettle();

expect(find.text('Count the Items'), findsOneWidget);
Expand Down
14 changes: 10 additions & 4 deletions waydowntown_app/test/games/string_collector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:waydowntown/games/string_collector.dart';
import 'package:waydowntown/models/run.dart';

import '../test_helpers.dart';
import '../test_helpers.mocks.dart';

void main() {
dotenv.testLoad(fileInput: File('.env').readAsStringSync());
Expand All @@ -19,13 +20,16 @@ void main() {
late Dio dio;
late DioAdapter dioAdapter;
late Run game;
late MockPhoenixChannel mockChannel;

setUp(() {
dio = Dio(BaseOptions(baseUrl: dotenv.env['API_ROOT']!));
dio.interceptors.add(PrettyDioLogger());
dioAdapter = DioAdapter(dio: dio);
game = TestHelpers.createMockRun(
concept: 'string_collector', description: 'Collect strings');

(_, mockChannel, _) = TestHelpers.setupMockSocket();
});

testWidgets('StringCollectorGame displays and submits strings',
Expand Down Expand Up @@ -64,8 +68,9 @@ void main() {
totalAnswers: 3,
isComplete: true));

await tester.pumpWidget(
MaterialApp(home: StringCollectorGame(run: game, dio: dio)));
await tester.pumpWidget(MaterialApp(
home: StringCollectorGame(run: game, dio: dio, channel: mockChannel),
));
await tester.pumpAndSettle();

expect(find.text('Parent Region > Test Region'), findsOneWidget);
Expand Down Expand Up @@ -132,8 +137,9 @@ void main() {
"error_string", game.id, null),
);

await tester.pumpWidget(
MaterialApp(home: StringCollectorGame(run: game, dio: dio)));
await tester.pumpWidget(MaterialApp(
home: StringCollectorGame(run: game, dio: dio, channel: mockChannel),
));
await tester.pumpAndSettle();

await tester.enterText(find.byType(TextField), 'error_string');
Expand Down
Loading

0 comments on commit 4dbf75f

Please sign in to comment.