diff --git a/lib/app/app.locator.dart b/lib/app/app.locator.dart index a5b84ca7..458c09fb 100644 --- a/lib/app/app.locator.dart +++ b/lib/app/app.locator.dart @@ -43,7 +43,9 @@ Future setupLocator( locator.registerLazySingleton(() => DMService()); locator.registerLazySingleton(() => OrganizationService()); locator.registerLazySingleton(() => StartupService()); - locator.registerLazySingleton(() => CentrifugeService()); + final centrifugeService = await CentrifugeService.getInstance(); + locator.registerSingleton(centrifugeService); + locator.registerLazySingleton(() => ConnectivityService()); locator.registerLazySingleton(() => FakeZuriApiService()); locator.registerLazySingleton(() => FilesService()); diff --git a/lib/ui/views/main/dm/dm_viewmodel.dart b/lib/ui/views/main/dm/dm_viewmodel.dart index c687a30b..90824226 100644 --- a/lib/ui/views/main/dm/dm_viewmodel.dart +++ b/lib/ui/views/main/dm/dm_viewmodel.dart @@ -33,13 +33,13 @@ class DmViewModel extends BaseViewModel { _dmRoomInfo = _dmService.getExistingRoomInfo; if (_dmRoomInfo == null) { //we dont have a conversation yet so create a new room - await _dmService.createRoom(_currentLoggedInUser, _user); + _roomId=await _dmService.createRoom(_currentLoggedInUser, _user); ///_dmService.getRoomInfo(_roomId); } else { _roomId = _dmRoomInfo!.roomInfo.id; } - _messages = (await _dmService.fetchRoomMessages(_roomId)); + _messages = await _dmService.fetchRoomMessages(_roomId); fetchPinnedMessages(); //_dmService.markMessageAsRead('614b1e8f44a9bd81cedc0a29'); setBusy(false); @@ -299,7 +299,7 @@ class DmViewModel extends BaseViewModel { } void scrollToBottom() { - SchedulerBinding.instance!.addPostFrameCallback((_) { + SchedulerBinding.instance?.addPostFrameCallback((_) { rightSideBarController.animateTo( rightSideBarController.position.maxScrollExtent, duration: const Duration(milliseconds: 10), diff --git a/test/helpers/test_helpers.dart b/test/helpers/test_helpers.dart index 8d71c2a2..9f674d45 100644 --- a/test/helpers/test_helpers.dart +++ b/test/helpers/test_helpers.dart @@ -1,5 +1,7 @@ +import 'dart:async'; import 'dart:convert'; +import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; import 'package:stacked_services/stacked_services.dart'; @@ -100,7 +102,6 @@ DMService getAndRegisterDMService() { Future.delayed(Duration(seconds: 3)); return Users(); }); - when(service.getCurrentLoggedInUser()).thenReturn(User( id: '1', firstName: 'Dedan', @@ -113,11 +114,24 @@ DMService getAndRegisterDMService() { createdAt: 'createdAt', updatedAt: 'updatedAt', token: 'token')); - when(service.getExistingRoomInfo) - .thenReturn(DM( - roomInfo: DMRoomsResponse(), - currentUserProfile: UserProfile(), - otherUserProfile: UserProfile())); + when(service.getExistingRoomInfo).thenReturn(DM( + roomInfo: DMRoomsResponse(), + currentUserProfile: UserProfile(), + otherUserProfile: UserProfile())); + when(service.fetchRoomMessages(any)).thenAnswer((u) async { + await Future.delayed(Duration(microseconds: 100)); + List res = []; + res.add(Results()); + return res; + }); + when(service.fetchPinnedMessages(any)).thenAnswer((realInvocation) async { + await Future.delayed(Duration(microseconds: 100)); + return []; + }); + when(service.sendMessage(any, any, any)).thenAnswer((realInvocation) async { + await Future.delayed(Duration(microseconds: 100)); + return SendMessageResponse(data: SendMessageResponseData()); + }); locator.registerSingleton(service); return service; } @@ -146,6 +160,17 @@ AuthService getAndRegisterAuthService() { CentrifugeService getAndRegisterCentrifugeService() { _removeRegistrationIfExists(); final service = MockCentrifugeService(); + when(service.subscribe(any)).thenAnswer((_) async { + await Future.delayed(Duration(microseconds: 100)); + return null; + }); + when(service.listen( + socketId: anyNamed('socketId'), + channelId: anyNamed('channelId'), + onData: anyNamed('onData'))) + .thenAnswer((realInvocation) { + return StreamController.broadcast().stream.listen((event) {}); + }); locator.registerSingleton(service); return service; } diff --git a/test/services_test/dm_service_test.dart b/test/services_test/dm_service_test.dart index fa622d09..536c8f70 100644 --- a/test/services_test/dm_service_test.dart +++ b/test/services_test/dm_service_test.dart @@ -1,16 +1,20 @@ import 'package:flutter_test/flutter_test.dart'; + + import '../helpers/test_helpers.dart'; void main() { setUp(() => registerServices()); - tearDown(() => unregisterServices()); -/* group('runLogic -', () { + tearDown(() => unregisterServices());/* + group('runLogic -', () { test('set New Room Info test', () { final service = getAndRegisterDMService(); - service.setNewRoomInfo(Users(firstName: 'Zuri User')); + final serv=DMService(); + + serv.setNewRoomInfo(Users(firstName: 'Zuri User')); expect( - service.getExistingRoomInfo!.otherUserProfile.firstName, 'Zuri User'); + serv.getExistingRoomInfo!.otherUserProfile.firstName, 'Zuri User'); }); test('get user', () { final service = getAndRegisterDMService(); @@ -48,6 +52,6 @@ void main() { final service = getAndRegisterDMService(); service.sendMessage('12345', '98765', 'Hello, are u still working'); }); - }); */ - + }); + */ } diff --git a/test/viewmodel_tests/dm_viewmodel_test.dart b/test/viewmodel_tests/dm_viewmodel_test.dart index 0eeabf1e..42e7d293 100644 --- a/test/viewmodel_tests/dm_viewmodel_test.dart +++ b/test/viewmodel_tests/dm_viewmodel_test.dart @@ -1,39 +1,67 @@ import 'package:flutter_test/flutter_test.dart'; - +import 'package:zc_desktop_flutter/model/app_models.dart'; +import 'package:zc_desktop_flutter/ui/views/main/dm/dm_viewmodel.dart'; import '../helpers/test_helpers.dart'; void main() { - setUp(() => registerServices()); - tearDown(() => unregisterServices());/* + var dmViewModel; + setUp(() { + registerServices(); + dmViewModel = DmViewModel(); + dmViewModel.runTask(); + }); + tearDown(() => unregisterServices()); + group('DmViewmodelTest -', () { + test('Sending Message', () async { + await dmViewModel + .sendMessage('Hey. Good morning. Am here to check up on you.'); + expect( + dmViewModel.messages.last.message + .contains('Hey. Good morning. Am here to check up on you.'), + true); + }); - group('DM Logic tests -', () { - test('Sending Message', () { - final dmViewModel = DmViewModel(); - dmViewModel.runTask(); - dmViewModel - .sendMessage('Hey. Good morning. Am here to check up on you.'); - //expect(dmViewModel.messages.length, 1); - }); + test('Test Time and Date Formatting', () { + //we need more tests to check all possible expected return values for date.. ie today,yesterday + expect(dmViewModel.formatDate('2021-10-26T16:30:19.122000Z'), + 'Tue October 26'); + expect(dmViewModel.formatTime('2021-10-26T16:30:19.122000Z'), '16:30 PM'); + //we need more tests to cover all scenarios for this function ie when there is a message + expect(dmViewModel.isSameDate(1), true); + }); - test('Test Time and Date Formatting', () { - final dmViewModel = DmViewModel(); - //we need more tests to check all possible expected return values for date.. ie today,yesterday - expect(dmViewModel.formatDate('2021-10-26T16:30:19.122000Z'), - 'Tue October 26'); - expect( - dmViewModel.formatTime('2021-10-26T16:30:19.122000Z'), '16:30 PM'); - //we need more tests to cover all scenarios for this function ie when there is a message - expect(dmViewModel.isSameDate(1), true); - }); + test('Test adding pinned message', () { + final dmViewModel = DmViewModel(); + dmViewModel.pinnedMessages.add(PinnedMessageContent()); + expect(dmViewModel.pinnedMessages.length, 1); + }); + }); - test('teee', () { - final dmViewModel = DmViewModel(); - dmViewModel.pinnedMessages.add(PinnedMessageContent()); - expect(dmViewModel.pinnedMessages.length, 1); - }); + group('UI related functions', () { + test('Message Hovering function test ', () { + dmViewModel.onMessageHovered(true, 1); + expect(dmViewModel.onMessageTileHover, true); + expect(dmViewModel.onMessageHoveredIndex, 1); }); + + test('onHoverActionsHovered funtion test', () { + dmViewModel.onHoverActionsHovered(true, 'reation', 20.0); + expect(dmViewModel.onHoverActionsHover, true); + expect(dmViewModel.hoverAction, 'reation'); + expect(dmViewModel.hoverWidth, 20.0); + }); + + test('toggleShowingNewMessageIn', () { + dmViewModel.toggleShowingNewMessageIn(true); + expect(dmViewModel.showingNewMessageIn, true); + }); + + /* test('scrollToBottom function test', () { + dmViewModel.scrollToBottom(); + expect(dmViewModel.rightSideBarController.offset, + dmViewModel.rightSideBarController.position.maxScrollExtent); + }); */ }); - */ }