Skip to content

Commit

Permalink
fixup! fixup! WIP-Adding unit test for message class
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledNjim committed Dec 13, 2024
1 parent 6f2486c commit f2726b4
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions test/mixin/message_avatar_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,66 @@ 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';
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<User>(),
MockSpec<Room>(),
])
class MockMessageAvatarUtils with MessageAvatarMixin {}

Future<void> 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<void> runTest(
WidgetTester tester, {
required Event event,
required bool selectMode,
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(
Expand Down Expand Up @@ -85,7 +99,7 @@ Future<void> main() async {
context: context,
selectMode: selectMode,
);
return const SizedBox();
return widget!;
},
),
),
Expand All @@ -99,6 +113,7 @@ Future<void> 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);
}
}
Expand Down

0 comments on commit f2726b4

Please sign in to comment.