-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
167 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
|
||
import 'package:mockito/mockito.dart'; | ||
|
||
import '../entities/chat_session.dart'; | ||
import '../entities/message.dart'; | ||
import '../usecases/chat_usecase.dart'; | ||
|
||
class MockChatUseCase extends Mock implements ChatUseCase { | ||
@override | ||
Stream<Message> sendMessage(Message newMessage, ChatSession session) { | ||
return super.noSuchMethod( | ||
Invocation.method(#sendMessage, [newMessage, session]), | ||
returnValue: Stream<Message>.empty(), // Provide a default empty Stream | ||
returnValueForMissingStub: Stream<Message>.empty(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
|
||
import 'package:mockito/mockito.dart'; | ||
|
||
import '../entities/chat_session.dart'; | ||
import '../usecases/chat_session_usecase.dart'; | ||
|
||
class MockChatSessionUseCase extends Mock implements ChatSessionUseCase { | ||
@override | ||
Future<List<ChatSession>> getAllSessions() { | ||
return super.noSuchMethod( | ||
Invocation.method(#getAllSessions, []), | ||
returnValue: Future.value(<ChatSession>[]), // Return an empty Future<List<ChatSession>> | ||
returnValueForMissingStub: Future.value(<ChatSession>[]), // Provide a default empty Future | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'package:mockito/mockito.dart'; | ||
import '../usecases/function_tools_usecase.dart'; | ||
|
||
class MockFunctionToolsUseCase extends Mock implements FunctionToolsUseCase { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'package:domain/entities/user.dart'; | ||
import 'package:domain/mocks/auth_usecase_mock.dart'; | ||
import 'package:domain/mocks/user_usecase_mock.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:swiftcomp/presentation/chat/viewModels/chat_view_model.dart'; | ||
import 'package:domain/mocks/chat_usecase_mock.dart'; | ||
import 'package:domain/mocks/chatsession_usecase_mock.dart'; | ||
import 'package:domain/mocks/functiontool_usecase_mock.dart'; | ||
|
||
void main() { | ||
group('ChatViewModel Tests', () { | ||
late MockChatUseCase mockChatUseCase; | ||
late MockChatSessionUseCase mockChatSessionUseCase; | ||
late MockFunctionToolsUseCase mockFunctionToolsUseCase; | ||
late MockAuthUseCase mockAuthUseCase; | ||
late MockUserUseCase mockUserUseCase; | ||
late ChatViewModel chatViewModel; | ||
|
||
setUp(() { | ||
// Initialize the mocks and view model before each test | ||
mockAuthUseCase = MockAuthUseCase(); | ||
mockUserUseCase = MockUserUseCase(); | ||
mockChatUseCase = MockChatUseCase(); | ||
mockChatSessionUseCase = MockChatSessionUseCase(); | ||
mockFunctionToolsUseCase = MockFunctionToolsUseCase(); | ||
chatViewModel = ChatViewModel( | ||
chatUseCase: mockChatUseCase, | ||
authUseCase: mockAuthUseCase, | ||
userUserCase: mockUserUseCase, | ||
chatSessionUseCase: mockChatSessionUseCase, | ||
functionToolsUseCase: mockFunctionToolsUseCase, | ||
); | ||
}); | ||
|
||
tearDown(() { | ||
// Clean up resources or reset mock state after each test | ||
reset(mockAuthUseCase); | ||
reset(mockUserUseCase); | ||
reset(mockChatUseCase); | ||
reset(mockChatSessionUseCase); | ||
reset(mockFunctionToolsUseCase); | ||
}); | ||
|
||
group('fetchAuthSessionNew', () { | ||
test('fetchAuthSessionNew sets isLoggedIn to true and fetches user when logged in', () async { | ||
// Arrange | ||
when(mockAuthUseCase.isLoggedIn()).thenAnswer((_) async => true); | ||
final mockUser = | ||
User(name: 'Test User', email: '[email protected]', avatarUrl: 'test-avatar.png'); | ||
when(mockUserUseCase.fetchMe()).thenAnswer((_) async => mockUser); | ||
|
||
// Act | ||
await chatViewModel.fetchAuthSessionNew(); | ||
|
||
// Assert | ||
expect(chatViewModel.isLoggedIn, true); | ||
expect(chatViewModel.user, mockUser); | ||
verify(mockAuthUseCase.isLoggedIn()).called(1); | ||
verify(mockUserUseCase.fetchMe()).called(1); | ||
}); | ||
|
||
test('fetchAuthSessionNew handles exception and resets state', () async { | ||
// Arrange | ||
when(mockAuthUseCase.isLoggedIn()).thenThrow(Exception('Auth error')); | ||
|
||
// Act | ||
await chatViewModel.fetchAuthSessionNew(); | ||
|
||
// Assert | ||
expect(chatViewModel.isLoggedIn, false); | ||
expect(chatViewModel.user, null); | ||
verify(mockAuthUseCase.isLoggedIn()).called(1); | ||
}); | ||
|
||
test('fetchAuthSessionNew sets isLoggedIn to false and user to null when not logged in', | ||
() async { | ||
// Arrange | ||
when(mockAuthUseCase.isLoggedIn()).thenAnswer((_) async => false); | ||
|
||
// Act | ||
await chatViewModel.fetchAuthSessionNew(); | ||
|
||
// Assert | ||
expect(chatViewModel.isLoggedIn, false); | ||
expect(chatViewModel.user, null); | ||
verify(mockAuthUseCase.isLoggedIn()).called(1); | ||
verifyNever(mockUserUseCase.fetchMe()); | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters