[LLC] Refactor attachment_file_uploader usage in client and channel

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-02-17 20:14:01 +05:30
parent 517c7e489d
commit b29f673e91
5 changed files with 144 additions and 71 deletions
@@ -10,8 +10,12 @@ import 'package:stream_chat/src/models/reaction.dart';
import 'package:stream_chat/src/models/own_user.dart';
import 'package:test/test.dart';
import 'package:stream_chat/stream_chat.dart';
class MockDio extends Mock implements DioForNative {}
class MockAttachmentUploader extends Mock implements AttachmentFileUploader {}
class MockHttpClientAdapter extends Mock implements HttpClientAdapter {}
void main() {
@@ -44,6 +48,61 @@ void main() {
mockDio.post<String>('/channels/messaging/testid/message', data: {
'message': message.toJson(),
})).called(1);
// final imageFile = MultipartFile.fromString('value');
//
// channelClient
// .sendImage(imageFile)
// .then((response) {
// final imageUrl = response.file;
// final attachment = Attachment(
// type = "image",
// imageUrl = imageUrl,
// )
// val message = Message(
// attachments = mutableListOf(attachment),
// )
// channelClient
// .sendMessage(message)
// .enqueue {
// /* ... */
// }
// })
// .catchError(onError);
//final channelClient = client.channel("messaging", id:'general');
//
// // Upload an image without detailed progress
// channelClient.sendImage(imageFile).enqueue { result->
// if (result.isSuccess) {
// // Successful upload, you can now attach this image
// // to an message that you then send to a channel
// val imageUrl = result.data()
// val attachment = Attachment(
// type = "image",
// imageUrl = imageUrl,
// )
// val message = Message(
// attachments = mutableListOf(attachment),
// )
// channelClient.sendMessage(message).enqueue { /* ... */ }
// }
// }
//
// // Upload a file, monitoring for progress with a ProgressCallback
// channelClient.sendFile(anyOtherFile, object : ProgressCallback {
// override fun onSuccess(file: String) {
// val fileUrl = file
// }
//
// override fun onError(error: ChatError) {
// // Handle error
// }
//
// override fun onProgress(progress: Long) {
// // You can render the uploading progress here
// }
// }).enqueue() // No callback passed to enqueue, as we'll get notified above anyway
});
test('markRead', () async {
@@ -170,6 +229,11 @@ void main() {
test('sendFile', () async {
final mockDio = MockDio();
final mockUploader = MockAttachmentUploader();
final file = AttachmentFile(path: 'filePath/fileName.pdf');
final channelId = 'testId';
final channelType = 'messaging';
when(mockDio.options).thenReturn(BaseOptions());
when(mockDio.interceptors).thenReturn(Interceptors());
@@ -178,23 +242,25 @@ void main() {
'api-key',
httpClient: mockDio,
tokenProvider: (_) async => '',
attachmentUploader: mockUploader,
);
final channelClient = client.channel('messaging', id: 'testid');
final file = MultipartFile.fromString('file');
final channelClient = client.channel(channelType, id: channelId);
when(mockDio.post<String>('/channels/messaging/testid/file',
data: argThat(isA<FormData>(), named: 'data')))
.thenAnswer((_) async => Response(data: '{}', statusCode: 200));
when(mockUploader.sendFile(file, channelId, channelType))
.thenAnswer((_) async => SendFileResponse());
await channelClient.sendFile(file);
verify(mockDio.post<String>('/channels/messaging/testid/file',
data: argThat(isA<FormData>(), named: 'data')))
.called(1);
verify(mockUploader.sendFile(file, channelId, channelType)).called(1);
});
test('sendImage', () async {
final mockDio = MockDio();
final mockUploader = MockAttachmentUploader();
final image = AttachmentFile(path: 'imagePath/imageName.jpeg');
final channelId = 'testId';
final channelType = 'messaging';
when(mockDio.options).thenReturn(BaseOptions());
when(mockDio.interceptors).thenReturn(Interceptors());
@@ -203,18 +269,16 @@ void main() {
'api-key',
httpClient: mockDio,
tokenProvider: (_) async => '',
attachmentUploader: mockUploader,
);
final channelClient = client.channel('messaging', id: 'testid');
final file = MultipartFile.fromString('file');
final channelClient = client.channel(channelType, id: channelId);
when(mockDio.post<String>('/channels/messaging/testid/image',
data: argThat(isA<FormData>(), named: 'data')))
.thenAnswer((_) async => Response(data: '{}', statusCode: 200));
when(mockUploader.sendImage(image, channelId, channelType))
.thenAnswer((_) async => SendImageResponse());
await channelClient.sendImage(file);
await channelClient.sendImage(image);
verify(mockDio.post<String>('/channels/messaging/testid/image',
data: argThat(isA<FormData>(), named: 'data')))
verify(mockUploader.sendImage(image, channelId, channelType))
.called(1);
});