From f2726b4c5bcd292c3553ee4104071e8d2566a50e Mon Sep 17 00:00:00 2001 From: "khaled.njim" Date: Fri, 13 Dec 2024 01:23:30 +0100 Subject: [PATCH] fixup! fixup! WIP-Adding unit test for message class --- test/mixin/message_avatar_mixin_test.dart | 53 +++++++++++++++-------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/test/mixin/message_avatar_mixin_test.dart b/test/mixin/message_avatar_mixin_test.dart index 15377b116..5bd7f49a9 100644 --- a/test/mixin/message_avatar_mixin_test.dart +++ b/test/mixin/message_avatar_mixin_test.dart @@ -4,8 +4,8 @@ import 'package:fluffychat/config/localizations/localization_service.dart'; import 'package:fluffychat/config/themes.dart'; import 'package:fluffychat/presentation/mixins/message_avatar_mixin.dart'; import 'package:fluffychat/utils/responsive/responsive_utils.dart'; -import 'package:fluffychat/widgets/theme_builder.dart'; import 'package:fluffychat/widgets/avatar/avatar.dart'; +import 'package:fluffychat/widgets/theme_builder.dart'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localized_locales/flutter_localized_locales.dart'; @@ -13,36 +13,44 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:get_it/get_it.dart'; import 'package:matrix/matrix.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; +import 'package:mockito/annotations.dart'; +import 'package:mockito/mockito.dart'; +import 'message_avatar_mixin_test.mocks.dart'; -import '../utils/test_client.dart'; - +@GenerateNiceMocks([ + MockSpec(), + MockSpec(), +]) class MockMessageAvatarUtils with MessageAvatarMixin {} Future main() async { TestWidgetsFlutterBinding.ensureInitialized(); late MockMessageAvatarUtils mockMessageAvatarUtils; - + late Room room; + late User user; + late Event event; setUpAll(() { final getIt = GetIt.instance; getIt.registerSingleton(ResponsiveUtils()); mockMessageAvatarUtils = MockMessageAvatarUtils(); }); - final client = await prepareTestClient(loggedIn: true); - final room = Room(id: '!726s6s6f:example.com', client: client); - final event = Event( - content: { - 'body': 'Test message', - 'msgtype': 'm.text', - }, - type: 'm.room.message', - eventId: '7365636s6r64300:example.com', - senderId: '@bob:example.com', - originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653), - room: room, - ); - group('[MessageAvatarUtils] TEST\n', () { + setUp(() { + room = MockRoom(); + user = MockUser(); + event = Event( + content: { + 'body': 'Test message', + 'msgtype': 'm.text', + }, + type: 'm.room.message', + eventId: '7365636s6r64300:example.com', + senderId: '@bob:example.com', + originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653), + room: room, + ); + }); Future runTest( WidgetTester tester, { required Event event, @@ -50,6 +58,12 @@ Future main() async { required bool sameSender, required bool ownMessage, }) async { + when(room.requestUser(event.senderId, ignoreErrors: true)) + .thenAnswer((_) async => user); + when(room.unsafeGetUserFromMemoryOrFallback(event.senderId)) + .thenReturn(user); + when(user.avatarUrl).thenReturn(Uri.tryParse("fakeImage")); + when(user.calcDisplayname()).thenReturn('Test'); Widget? widget; await tester.pumpWidget( ThemeBuilder( @@ -85,7 +99,7 @@ Future main() async { context: context, selectMode: selectMode, ); - return const SizedBox(); + return widget!; }, ), ), @@ -99,6 +113,7 @@ Future main() async { expect(find.byType(SizedBox), findsOneWidget); } else { expect(widget, isNotNull); + verify(room.requestUser(event.senderId, ignoreErrors: true)).called(1); expect(find.byType(Avatar), findsOneWidget); } }