Skip to content

Commit

Permalink
TW-1702: error handling on download file on web fixed + tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Te-Z committed May 28, 2024
1 parent 6cd553c commit 19f784e
Show file tree
Hide file tree
Showing 3 changed files with 1,214 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/widgets/mixins/download_file_on_web_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/presentation/model/chat/downloading_state_presentation_model.dart';
import 'package:fluffychat/utils/exception/downloading_exception.dart';
import 'package:fluffychat/utils/manager/download_manager/download_file_state.dart';
import 'package:fluffychat/utils/manager/download_manager/download_manager.dart';
import 'package:fluffychat/widgets/twake_app.dart';
Expand Down Expand Up @@ -44,8 +45,15 @@ mixin DownloadFileOnWebMixin<T extends StatefulWidget> on State<T> {
void setupDownloadingProcess(Either<Failure, Success> resultEvent) {
resultEvent.fold(
(failure) {
Logs().e('$T::onDownloadingProcess(): $failure');
downloadFileStateNotifier.value = const NotDownloadPresentationState();
Logs().e('MessageDownloadContentWeb::onDownloadingProcess(): $failure');
if (failure is DownloadFileFailureState &&
failure.exception is CancelDownloadingException) {
downloadFileStateNotifier.value =
const NotDownloadPresentationState();
} else {
downloadFileStateNotifier.value =
DownloadErrorPresentationState(error: failure);
}
},
(success) {
if (success is DownloadingFileState) {
Expand Down
150 changes: 150 additions & 0 deletions test/mixin/download/download_file_on_web_mixin_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import 'dart:io';

import 'package:dartz/dartz.dart' hide State, OpenFile;
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/presentation/model/chat/downloading_state_presentation_model.dart';
import 'package:fluffychat/utils/exception/downloading_exception.dart';
import 'package:fluffychat/utils/manager/download_manager/download_file_state.dart';
import 'package:fluffychat/utils/manager/download_manager/download_manager.dart';
import 'package:fluffychat/widgets/mixins/download_file_on_web_mixin.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:matrix/matrix.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'download_file_on_web_mixin_test.mocks.dart';

const fakeEventId = "fakeEventId";
const fakeFilename = "fakeFilename";

class MockRoom extends Mock implements Room {
@override
// ignore: hash_and_equals
bool operator ==(dynamic other) {
if (identical(this, other)) return true;
// ignore: unrelated_type_equality_checks
return other is Room && other.id == id;
}

@override
Map<String, MatrixFile> get sendingFilePlaceholders => super.noSuchMethod(
Invocation.getter(#sendingFilePlaceholders),
returnValue: <String, MatrixFile>{},
returnValueForMissingStub: <String, MatrixFile>{},
);
}

class MockEvent extends Mock implements Event {
MockEvent(this.fakeRoom);
final Room fakeRoom;
@override
String get eventId => fakeEventId;

@override
Room get room => fakeRoom;

@override
Map get infoMap => super.noSuchMethod(
Invocation.getter(#infoMap),
returnValue: {},
returnValueForMissingStub: {},
);
}

class DummyWidget extends StatefulWidget {
const DummyWidget({required this.event, super.key});

final Event event;

@override
// ignore: no_logic_in_create_state
DummyWidgetState createState() => DummyWidgetState(event);
}

class DummyWidgetState extends State<DummyWidget> with DownloadFileOnWebMixin {
DummyWidgetState(this.fakeEvent);

final Event fakeEvent;

@override
Widget build(BuildContext context) {
return Container();
}

@override
Event get event => fakeEvent;

@override
void handleDownloadMatrixFileSuccessDone({
required DownloadMatrixFileSuccessState success,
}) {}
}

@GenerateNiceMocks([
MockSpec<File>(),
MockSpec<DownloadManager>(),
])
void main() {
group('DownloadFileOnWebMixin: setupDownloadingProcess', () {
late DummyWidgetState dummyState;
late Event fakeEvent;
late Room fakeRoom;

setUp(() {
getIt.registerSingleton<DownloadManager>(MockDownloadManager());
fakeRoom = MockRoom();
fakeEvent = MockEvent(fakeRoom);
dummyState = DummyWidgetState(fakeEvent);
});

tearDown(() {
getIt.reset();
});

test('should handle failure', () {
final failure = DownloadFileFailureState(exception: Exception());
dummyState.setupDownloadingProcess(Left(failure));

expect(
dummyState.downloadFileStateNotifier.value,
isInstanceOf<DownloadErrorPresentationState>(),
);
});

test('should handle cancel', () {
final failure = DownloadFileFailureState(
exception: CancelDownloadingException(),
);
dummyState.setupDownloadingProcess(Left(failure));

expect(
dummyState.downloadFileStateNotifier.value,
isInstanceOf<NotDownloadPresentationState>(),
);
});

test('should handle success with DownloadingFileState', () {
const success = DownloadingFileState(receive: 10, total: 100);
dummyState.setupDownloadingProcess(const Right(success));

expect(
dummyState.downloadFileStateNotifier.value,
isInstanceOf<DownloadingPresentationState>(),
);
});

test('should handle success with DownloadMatrixFileSuccessState', () {
TestWidgetsFlutterBinding.ensureInitialized();

final success = DownloadMatrixFileSuccessState(
matrixFile: MatrixFile(name: "name"),
);
dummyState.setupDownloadingProcess(Right(success));

expect(
dummyState.downloadFileStateNotifier.value,
isInstanceOf<NotDownloadPresentationState>(),
);
});
});
}
Loading

0 comments on commit 19f784e

Please sign in to comment.