From 5322ad717068d6bd29408d3a8fb4774dba9a1ad3 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Tue, 6 Jun 2023 16:07:31 +0530 Subject: [PATCH] test: add tests. Signed-off-by: xsahil03x --- .../test/src/client/client_test.dart | 118 ++++++++++++++++++ packages/stream_chat/test/src/mocks.dart | 14 ++- 2 files changed, 130 insertions(+), 2 deletions(-) diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index 7a605557..b1f724af 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -2544,4 +2544,122 @@ void main() { }, ); }); + + group('PersistenceConnectionTests', () { + const apiKey = 'test-api-key'; + late final api = FakeChatApi(); + late final ws = FakeWebSocket(); + + final user = User(id: 'test-user-id'); + final token = Token.development(user.id).rawValue; + + late StreamChatClient client; + + setUp(() async { + client = StreamChatClient(apiKey, chatApi: api, ws: ws); + expect(client.persistenceEnabled, isFalse); + }); + + tearDown(() { + client.chatPersistenceClient = null; + expect(client.persistenceEnabled, isFalse); + client.dispose(); + }); + + test('openPersistenceConnection connects the client to the user', () async { + client.chatPersistenceClient = MockPersistenceClient(); + await client.openPersistenceConnection(user); + expect(client.persistenceEnabled, isTrue); + }); + + test( + '''multiple call to openPersistenceConnection does not throws an error if already connected to the same user''', + () async { + client.chatPersistenceClient = MockPersistenceClient(); + await client.openPersistenceConnection(user); + expect(client.persistenceEnabled, isTrue); + + expect(() => client.openPersistenceConnection(user), returnsNormally); + expect(() => client.openPersistenceConnection(user), returnsNormally); + expect(() => client.openPersistenceConnection(user), returnsNormally); + }, + ); + + test( + '''openPersistenceConnection throws an error if client is already connected to a different user''', + () async { + client.chatPersistenceClient = MockPersistenceClient(); + await client.openPersistenceConnection(user); + expect(client.persistenceEnabled, isTrue); + + expect( + () => client.openPersistenceConnection(user.copyWith(id: 'new-id')), + throwsA(const TypeMatcher()), + ); + }, + ); + + test( + '''openPersistenceConnection throws an error if chatPersistenceClient is not set''', + () async { + expect( + () => client.openPersistenceConnection(user), + throwsA(const TypeMatcher()), + ); + }, + ); + + test('closePersistenceConnection disconnects the client', () async { + client.chatPersistenceClient = MockPersistenceClient(); + await client.openPersistenceConnection(user); + expect(client.persistenceEnabled, isTrue); + + await client.closePersistenceConnection(); + expect(client.persistenceEnabled, isFalse); + }); + + test( + '''closePersistenceConnection does nothing if chatPersistenceClient is not connected''', + () async { + client.chatPersistenceClient = MockPersistenceClient(); + expect(client.persistenceEnabled, isFalse); + + expect(() => client.closePersistenceConnection(), returnsNormally); + }, + ); + + test( + '''closePersistenceConnection does nothing if chatPersistenceClient is not set''', + () async { + expect(client.persistenceEnabled, isFalse); + expect(() => client.closePersistenceConnection(), returnsNormally); + }, + ); + + test( + '''connectUser should re-use the persistence connection if already connected''', + () async { + client.chatPersistenceClient = MockPersistenceClient(); + await client.openPersistenceConnection(user); + expect(client.persistenceEnabled, isTrue); + + await client.connectUser(user, token, connectWebSocket: false); + expect(client.persistenceEnabled, isTrue); + }, + ); + + test( + '''connectUser should throw if the persistence connection if already connected to a different user''', + () async { + client.chatPersistenceClient = MockPersistenceClient(); + await client.openPersistenceConnection(user.copyWith(id: 'new-id')); + expect(client.persistenceEnabled, isTrue); + + expect( + () => client.connectUser(user, token, connectWebSocket: false), + throwsA(const TypeMatcher()), + ); + }, + ); + }); } diff --git a/packages/stream_chat/test/src/mocks.dart b/packages/stream_chat/test/src/mocks.dart index 86a77a66..f412d15a 100644 --- a/packages/stream_chat/test/src/mocks.dart +++ b/packages/stream_chat/test/src/mocks.dart @@ -64,16 +64,26 @@ class MockAttachmentFileUploader extends Mock implements AttachmentFileUploader {} class MockPersistenceClient extends Mock implements ChatPersistenceClient { + String? _userId; bool _isConnected = false; @override bool get isConnected => _isConnected; @override - Future connect(String userId) async => _isConnected = true; + String? get userId => _userId; @override - Future disconnect({bool flush = false}) async => _isConnected = false; + Future connect(String userId) async { + _userId = userId; + _isConnected = true; + } + + @override + Future disconnect({bool flush = false}) async { + _userId = null; + _isConnected = false; + } } class MockStreamChatClient extends Mock implements StreamChatClient {