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

Tests for Dm View Model #577

Open
wants to merge 3 commits into
base: dev
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
4 changes: 3 additions & 1 deletion lib/app/app.locator.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/ui/views/main/dm/dm_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
37 changes: 31 additions & 6 deletions test/helpers/test_helpers.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -100,7 +102,6 @@ DMService getAndRegisterDMService() {
Future.delayed(Duration(seconds: 3));
return Users();
});

when(service.getCurrentLoggedInUser()).thenReturn(User(
id: '1',
firstName: 'Dedan',
Expand All @@ -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<Results> 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<DMService>(service);
return service;
}
Expand Down Expand Up @@ -146,6 +160,17 @@ AuthService getAndRegisterAuthService() {
CentrifugeService getAndRegisterCentrifugeService() {
_removeRegistrationIfExists<CentrifugeService>();
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<CentrifugeService>(service);
return service;
}
Expand Down
16 changes: 10 additions & 6 deletions test/services_test/dm_service_test.dart
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -48,6 +52,6 @@ void main() {
final service = getAndRegisterDMService();
service.sendMessage('12345', '98765', 'Hello, are u still working');
});
}); */

});
*/
}
82 changes: 55 additions & 27 deletions test/viewmodel_tests/dm_viewmodel_test.dart
Original file line number Diff line number Diff line change
@@ -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);
}); */
});
*/
}