test: add tests.

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2023-06-06 16:07:31 +05:30
committed by xsahil03x
parent 0bbca0bc2c
commit 5322ad7170
2 changed files with 130 additions and 2 deletions
@@ -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<StreamChatError>()),
);
},
);
test(
'''openPersistenceConnection throws an error if chatPersistenceClient is not set''',
() async {
expect(
() => client.openPersistenceConnection(user),
throwsA(const TypeMatcher<StreamChatError>()),
);
},
);
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<StreamChatError>()),
);
},
);
});
}
+12 -2
View File
@@ -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<void> connect(String userId) async => _isConnected = true;
String? get userId => _userId;
@override
Future<void> disconnect({bool flush = false}) async => _isConnected = false;
Future<void> connect(String userId) async {
_userId = userId;
_isConnected = true;
}
@override
Future<void> disconnect({bool flush = false}) async {
_userId = null;
_isConnected = false;
}
}
class MockStreamChatClient extends Mock implements StreamChatClient {