Skip to content

Tests for Pages #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions test/pages_test/chat_list_screen_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_nearby_connections_example/pages/ChatListScreen.dart';
import 'package:flutter_nearby_connections_example/pages/ChatPage.dart';
import 'package:flutter_nearby_connections_example/classes/Global.dart';

void main() {
testWidgets('ChatListScreen UI Test', (WidgetTester tester) async {

Global.conversations['John Doe'] = {}; // Just a dummy conversation
Global.messages = []; // Clearing messages for a clean slate

await tester.pumpWidget(MaterialApp(home: ChatListScreen()));

await tester.pumpAndSettle();

// Verify that the UI is as expected
expect(find.text('Chats'), findsOneWidget);
expect(find.byType(BottomNavigationBar), findsOneWidget);
expect(find.byType(TextField), findsOneWidget);
expect(find.byType(ListView), findsOneWidget);
});

testWidgets('ChatListScreen Interaction Test', (WidgetTester tester) async {

Global.conversations['John Doe'] = {}; // Just a dummy conversation
Global.messages = []; // Clearing messages for a clean slate

await tester.pumpWidget(MaterialApp(home: ChatListScreen()));

await tester.pumpAndSettle();

// Simulate tapping on a conversation
await tester.tap(find.text('John Doe'));

// Navigation to ChatPage
await tester.pumpAndSettle();

// Verify that we are on the ChatPage
expect(find.text('Chat with John Doe'), findsOneWidget);
});
}
36 changes: 36 additions & 0 deletions test/pages_test/chatpage_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_nearby_connections_example/pages/ChatPage.dart';
import 'package:flutter_nearby_connections_example/classes/Global.dart';

void main() {
testWidgets('ChatPage UI Test', (WidgetTester tester) async {
final String converser = 'John Doe';
await tester.pumpWidget(MaterialApp(home: ChatPage(converser)));
await tester.pumpAndSettle();

// Verify that the UI is as expected
expect(find.text('Chat with $converser'), findsOneWidget);
expect(find.byType(ListView), findsOneWidget);
expect(find.byType(TextFormField), findsOneWidget);
expect(find.byType(ElevatedButton), findsOneWidget);
});

testWidgets('ChatPage Interaction Test', (WidgetTester tester) async {
final String converser = 'John Doe';
await tester.pumpWidget(MaterialApp(home: ChatPage(converser)));
await tester.pumpAndSettle();

// Enter Text
await tester.enterText(find.byType(TextFormField), 'Hello, John!');

// Tap the send button
await tester.tap(find.byType(ElevatedButton));

// Wait for animations to complete
await tester.pumpAndSettle();

// Verify that the message is sent
expect(find.text('sent: Hello, John!'), findsOneWidget);
});
}
31 changes: 31 additions & 0 deletions test/pages_test/devicelistscreen_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_nearby_connections_example/pages/DevicesListScreen.dart';
import 'package:flutter_nearby_connections_example/pages/ChatPage.dart';
import 'package:flutter_nearby_connections_example/classes/Global.dart';

void main() {
testWidgets('DevicesListScreen UI Test', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(home: DevicesListScreen(deviceType: DeviceType.browser)));
await tester.pumpAndSettle();

// Verify that the UI is as expected
expect(find.text('Available Devices'), findsOneWidget);
expect(find.byType(TextFormField), findsOneWidget);
expect(find.byType(ListView), findsOneWidget);
});

testWidgets('DevicesListScreen Interaction Test', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(home: DevicesListScreen(deviceType: DeviceType.browser)));
await tester.pumpAndSettle();

// Simulate tapping on the first device in the list
await tester.tap(find.byType(GestureDetector).first);

// Wait for animations to complete
await tester.pumpAndSettle();

// Verify that we have navigated to ChatPage
expect(find.byType(ChatPage), findsOneWidget);
});
}
34 changes: 34 additions & 0 deletions test/pages_test/profile_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_nearby_connections_example/pages/Profile.dart';
import 'package:flutter_nearby_connections_example/pages/DeviceListScreen.dart';
import 'package:nanoid/nanoid.dart';
import '../classes/Global.dart';

void main() {
testWidgets('Profile UI Test', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(home: Profile()));

// Verify that the app has the expected text.
expect(find.text("Your Username will be your name+\$custom_length_id"), findsOneWidget);
expect(find.byType(TextFormField), findsOneWidget);
expect(find.byType(ElevatedButton), findsOneWidget);
});

testWidgets('Profile Interaction Test', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(home: Profile()));

// Enter a name into the TextFormField.
await tester.enterText(find.byType(TextFormField), 'JohnDoe');

// Tap on the save button.
await tester.tap(find.text('Save'));
await tester.pump();

// Verify that the Global.myName is updated.
expect(Global.myName, 'JohnDoe');

// Verify that the navigation to DeviceListScreen occurs.
expect(find.byType(DevicesListScreen), findsOneWidget);
});
}