Skip to content
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

chore(test): add ability to mock rust calls for tests #10

Merged
merged 1 commit into from
Feb 14, 2025
Merged
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
1 change: 1 addition & 0 deletions lib/features/chat/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ChatPage extends HookWidget {
icon: const Icon(Icons.add),
),
IconButton.filled(
key: const Key('send_message_button'),
onPressed: () {
_onSendMessage(
messageController.text,
Expand Down
28 changes: 26 additions & 2 deletions test/features/chat/chat_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,40 @@ import 'package:flutter/material.dart';
import 'package:gosling/features/chat/chat_page.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:gosling/generated/rust/frb_generated.dart';
import 'package:mocktail/mocktail.dart';

import '../../helpers/mocks.dart';
import '../../helpers/widget_helpers.dart';

void main() {
testWidgets('should have logo and input field', (WidgetTester tester) async {
Future<void> main() async {
final mockApi = MockRustLibApi();
RustLib.initMock(api: mockApi);

testWidgets('should have correct initial content',
(WidgetTester tester) async {
await tester.pumpWidget(
WidgetHelpers.testableWidget(child: const ChatPage()),
);

expect(find.byType(SvgPicture), findsOneWidget);
expect(find.byType(TextField), findsOneWidget);
expect(find.byKey(const Key('send_message_button')), findsOneWidget);
});

testWidgets('should send message when button is pressed',
(WidgetTester tester) async {
when(() => mockApi.crateApiSimpleGreet(name: "Hello"))
.thenReturn("Hello Test");

await tester.pumpWidget(
WidgetHelpers.testableWidget(child: const ChatPage()),
);

await tester.enterText(find.byType(TextField), 'Hello');
await tester.tap(find.byKey(const Key('send_message_button')));
await tester.pump();

expect(find.text('Hello Test'), findsOneWidget);
});
}
File renamed without changes.