fix: correct emptyFolderProvider reference in notifier and strategy tests

This commit is contained in:
dab246 authored and Dat H. Pham committed 2026-06-15 12:06:07 +07:00
1 parent b2f486dbc3
commit 1f06b63ef5
2 files changed
+265 -97

No files matched your search

@@ -18,6 +18,7 @@ import 'package:tmail_ui_user/features/mailbox/domain/state/delete_multiple_mail
import 'package:tmail_ui_user/features/mailbox/domain/usecases/clear_mailbox_interactor.dart';
import 'package:tmail_ui_user/features/mailbox/domain/usecases/delete_multiple_mailbox_interactor.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/providers/empty_folder_provider.dart';
import 'package:tmail_ui_user/features/thread/domain/state/empty_spam_folder_state.dart';
import 'package:tmail_ui_user/features/thread/domain/state/empty_trash_folder_state.dart';
import 'package:tmail_ui_user/features/thread/domain/usecases/empty_trash_folder_interactor.dart';
@@ -65,7 +66,9 @@ void main() {
Get.put<DeleteMultipleMailboxInteractor>(mockSubfoldersInteractor);
container = ProviderContainer();
notifier = container.read(emptyFolderNotifierProvider(trashMailboxId).notifier);
notifier = container.read(
emptyFolderProvider(trashMailboxId).notifier,
);
});
tearDown(() {
@@ -75,32 +78,43 @@ void main() {
void stubClearSuccess() {
when(mockClearInteractor.execute(any, any, any, any)).thenAnswer(
(_) => Stream.value(Right<Failure, Success>(
ClearMailboxSuccess(trashMailboxId, PresentationMailbox.roleTrash, UnsignedInt(0)),
)),
(_) => Stream.value(
Right<Failure, Success>(
ClearMailboxSuccess(
trashMailboxId,
PresentationMailbox.roleTrash,
UnsignedInt(0),
),
),
),
);
}
void stubClearDelayed(Duration delay) {
when(mockClearInteractor.execute(any, any, any, any)).thenAnswer(
(_) async* {
await Future<void>.delayed(delay);
yield Right<Failure, Success>(
ClearMailboxSuccess(trashMailboxId, PresentationMailbox.roleTrash, UnsignedInt(0)),
);
},
);
when(mockClearInteractor.execute(any, any, any, any)).thenAnswer((
_,
) async* {
await Future<void>.delayed(delay);
yield Right<Failure, Success>(
ClearMailboxSuccess(
trashMailboxId,
PresentationMailbox.roleTrash,
UnsignedInt(0),
),
);
});
}
void stubSubfoldersResult(Either<Failure, Success> result) {
when(mockSubfoldersInteractor.execute(any, any, any))
.thenAnswer((_) => Stream.value(result));
when(
mockSubfoldersInteractor.execute(any, any, any),
).thenAnswer((_) => Stream.value(result));
}
List<EmptyFolderState> listenStates() {
final states = <EmptyFolderState>[];
container.listen(
emptyFolderNotifierProvider(trashMailboxId),
emptyFolderProvider(trashMailboxId),
(_, EmptyFolderState next) => states.add(next),
fireImmediately: false,
);
@@ -108,7 +122,8 @@ void main() {
}
EmptyFolderSuccess readSuccess() =>
container.read(emptyFolderNotifierProvider(trashMailboxId)) as EmptyFolderSuccess;
container.read(emptyFolderProvider(trashMailboxId))
as EmptyFolderSuccess;
Future<void> expectInteractorErrorEmitsFailure({
required Exception error,
@@ -118,7 +133,7 @@ void main() {
}) async {
stubThrow(error);
await notifier.execute(session, accountId, trashMailbox, [], useJmapClear);
final state = container.read(emptyFolderNotifierProvider(trashMailboxId));
final state = container.read(emptyFolderProvider(trashMailboxId));
expect(state, isA<EmptyFolderFailure>());
final failure = state as EmptyFolderFailure;
expect(failure.exception, error);
@@ -129,7 +144,7 @@ void main() {
group('initial state', () {
test('starts as EmptyFolderIdle', () {
expect(
container.read(emptyFolderNotifierProvider(trashMailboxId)),
container.read(emptyFolderProvider(trashMailboxId)),
isA<EmptyFolderIdle>(),
);
});
@@ -139,7 +154,13 @@ void main() {
test('does nothing when already loading', () async {
stubClearDelayed(const Duration(milliseconds: 100));
final firstCall = notifier.execute(session, accountId, trashMailbox, [], true);
final firstCall = notifier.execute(
session,
accountId,
trashMailbox,
[],
true,
);
await notifier.execute(session, accountId, trashMailbox, [], true);
await firstCall;
@@ -148,76 +169,121 @@ void main() {
});
group('execute — JMAP clear path (useJmapClear = true)', () {
test('emits Loading then Success when clear succeeds with no subfolders',
() async {
final states = listenStates();
test(
'emits Loading then Success when clear succeeds with no subfolders',
() async {
final states = listenStates();
when(mockClearInteractor.execute(any, any, any, any)).thenAnswer(
(_) => Stream.value(Right<Failure, Success>(
ClearMailboxSuccess(trashMailboxId, PresentationMailbox.roleTrash, UnsignedInt(5)),
)),
);
when(mockClearInteractor.execute(any, any, any, any)).thenAnswer(
(_) => Stream.value(
Right<Failure, Success>(
ClearMailboxSuccess(
trashMailboxId,
PresentationMailbox.roleTrash,
UnsignedInt(5),
),
),
),
);
await notifier.execute(session, accountId, trashMailbox, [], true);
await notifier.execute(session, accountId, trashMailbox, [], true);
expect(states, [
isA<EmptyFolderLoading>(),
isA<EmptyFolderSuccess>(),
]);
expect(states, [
isA<EmptyFolderLoading>(),
isA<EmptyFolderSuccess>(),
]);
final success = states.last as EmptyFolderSuccess;
expect(success.subfoldersStatus, SubfoldersDeleteStatus.none);
expect(success.mailboxId, trashMailboxId);
final success = states.last as EmptyFolderSuccess;
expect(success.subfoldersStatus, SubfoldersDeleteStatus.none);
expect(success.mailboxId, trashMailboxId);
verifyNever(mockSubfoldersInteractor.execute(any, any, any));
});
verifyNever(mockSubfoldersInteractor.execute(any, any, any));
},
);
test('deletes subfolders even when mailbox has no emails', () async {
stubSubfoldersResult(Right(DeleteMultipleMailboxAllSuccess(childIds)));
await notifier.execute(session, accountId, emptyTrashMailbox, childIds, true);
await notifier.execute(
session,
accountId,
emptyTrashMailbox,
childIds,
true,
);
expect(readSuccess().subfoldersStatus, SubfoldersDeleteStatus.allDeleted);
expect(
readSuccess().subfoldersStatus,
SubfoldersDeleteStatus.allDeleted,
);
verifyNever(mockClearInteractor.execute(any, any, any, any));
verifyNever(mockEmailDeletionInteractor.execute(any, any, any, any, any));
verifyNever(
mockEmailDeletionInteractor.execute(any, any, any, any, any),
);
});
group('with successful email clear', () {
setUp(stubClearSuccess);
test('also deletes subfolders when childIds is not empty', () async {
stubSubfoldersResult(Right(DeleteMultipleMailboxAllSuccess(childIds)));
stubSubfoldersResult(
Right(DeleteMultipleMailboxAllSuccess(childIds)),
);
await notifier.execute(session, accountId, trashMailbox, childIds, true);
await notifier.execute(
session,
accountId,
trashMailbox,
childIds,
true,
);
expect(readSuccess().subfoldersStatus, SubfoldersDeleteStatus.allDeleted);
expect(
readSuccess().subfoldersStatus,
SubfoldersDeleteStatus.allDeleted,
);
});
test('emits Success with subfoldersSomeDeleted when HasSomeSuccess', () async {
stubSubfoldersResult(Right(DeleteMultipleMailboxHasSomeSuccess(childIds)));
test(
'emits Success with subfoldersSomeDeleted when HasSomeSuccess',
() async {
stubSubfoldersResult(
Right(DeleteMultipleMailboxHasSomeSuccess(childIds)),
);
await notifier.execute(session, accountId, trashMailbox, childIds, true);
await notifier.execute(
session,
accountId,
trashMailbox,
childIds,
true,
);
expect(readSuccess().subfoldersStatus, SubfoldersDeleteStatus.someDeleted);
});
expect(
readSuccess().subfoldersStatus,
SubfoldersDeleteStatus.someDeleted,
);
},
);
test('emits Success with subfoldersDeleteFailed when subfolder deletion fails',
() async {
stubSubfoldersResult(Left(DeleteMultipleMailboxAllFailure()));
test(
'emits Success with subfoldersDeleteFailed when subfolder deletion fails',
() async {
stubSubfoldersResult(Left(DeleteMultipleMailboxAllFailure()));
await notifier.execute(session, accountId, trashMailbox, childIds, true);
await notifier.execute(
session,
accountId,
trashMailbox,
childIds,
true,
);
expect(readSuccess().subfoldersStatus, SubfoldersDeleteStatus.failed);
});
});
test('emits Failure when clear interactor throws', () async {
await expectInteractorErrorEmitsFailure(
error: Exception('network error'),
stubThrow: (error) => when(mockClearInteractor.execute(any, any, any, any))
.thenAnswer((_) => Stream.error(error)),
useJmapClear: true,
verifyMailboxId: true,
expect(
readSuccess().subfoldersStatus,
SubfoldersDeleteStatus.failed,
);
},
);
});
@@ -225,12 +291,14 @@ void main() {
Get.reset();
container.dispose();
container = ProviderContainer();
notifier = container.read(emptyFolderNotifierProvider(trashMailboxId).notifier);
notifier = container.read(
emptyFolderProvider(trashMailboxId).notifier,
);
await notifier.execute(session, accountId, trashMailbox, [], true);
expect(
container.read(emptyFolderNotifierProvider(trashMailboxId)),
container.read(emptyFolderProvider(trashMailboxId)),
isA<EmptyFolderFailure>(),
);
});
@@ -240,38 +308,109 @@ void main() {
test('emits Loading then Success when EmptyTrashFolderSuccess', () async {
final states = listenStates();
when(mockEmailDeletionInteractor.execute(any, any, any, any, any))
.thenAnswer((_) => Stream.value(Right<Failure, Success>(
when(
mockEmailDeletionInteractor.execute(any, any, any, any, any),
).thenAnswer(
(_) => Stream.value(
Right<Failure, Success>(
EmptyTrashFolderSuccess(emailIds, trashMailboxId),
)));
),
),
);
await notifier.execute(session, accountId, trashMailbox, [], false);
expect(states, [
isA<EmptyFolderLoading>(),
isA<EmptyFolderSuccess>(),
]);
expect(states, [isA<EmptyFolderLoading>(), isA<EmptyFolderSuccess>()]);
final success = states.last as EmptyFolderSuccess;
expect(success.clearedEmailIds, emailIds);
expect(success.mailboxId, trashMailboxId);
});
test('emits Failure when EmptyTrashFolderInteractor throws', () async {
await expectInteractorErrorEmitsFailure(
error: Exception('connection lost'),
stubThrow: (error) => when(mockEmailDeletionInteractor.execute(any, any, any, any, any))
.thenAnswer((_) => Stream.error(error)),
useJmapClear: false,
);
});
test(
'emits EmptyFolderInProgress states as progress events arrive',
() async {
final states = listenStates();
when(
mockEmailDeletionInteractor.execute(any, any, any, any, any),
).thenAnswer((invocation) async* {
final progressCtrl =
invocation.positionalArguments[4]
as StreamController<Either<Failure, Success>>;
progressCtrl.add(Right(EmptyingFolderState(trashMailboxId, 2, 5)));
progressCtrl.add(Right(EmptyingFolderState(trashMailboxId, 5, 5)));
yield Right<Failure, Success>(
EmptyTrashFolderSuccess(emailIds, trashMailboxId),
);
});
await notifier.execute(session, accountId, trashMailbox, [], false);
expect(states, [
isA<EmptyFolderLoading>(),
isA<EmptyFolderInProgress>(),
isA<EmptyFolderInProgress>(),
isA<EmptyFolderSuccess>(),
]);
final firstProgress = states[1] as EmptyFolderInProgress;
expect(firstProgress.mailboxId, trashMailboxId);
expect(firstProgress.countEmailsDeleted, 2);
expect(firstProgress.totalEmails, 5);
final lastProgress = states[2] as EmptyFolderInProgress;
expect(lastProgress.countEmailsDeleted, 5);
},
);
});
group('emits Failure when interactor throws', () {
for (final (name, useJmapClear, verifyMailboxId, doStub) in [
(
'JMAP clear path',
true,
true,
(Exception error) {
when(
mockClearInteractor.execute(any, any, any, any),
).thenAnswer((_) => Stream.error(error));
},
),
(
'email-by-email path',
false,
false,
(Exception error) {
when(
mockEmailDeletionInteractor.execute(any, any, any, any, any),
).thenAnswer((_) => Stream.error(error));
},
),
]) {
test(name, () async {
await expectInteractorErrorEmitsFailure(
error: Exception('interactor error'),
stubThrow: doStub,
useJmapClear: useJmapClear,
verifyMailboxId: verifyMailboxId,
);
});
}
});
group('mounted guard', () {
test('does not throw when provider disposed during operation', () async {
stubClearDelayed(const Duration(milliseconds: 50));
final call = notifier.execute(session, accountId, trashMailbox, [], true);
final call = notifier.execute(
session,
accountId,
trashMailbox,
[],
true,
);
container.dispose();
await expectLater(call, completes);
@@ -27,29 +27,58 @@ void main() {
expect(strategy.teamMailboxRole, PresentationMailbox.trashRole);
});
test('successMessage delegates to toast_message_empty_trash_folder_success', () {
when(mockL10n.toast_message_empty_trash_folder_success).thenReturn('All messages deleted');
expect(strategy.successMessage(mockL10n), 'All messages deleted');
});
test(
'successMessage delegates to toast_message_empty_trash_folder_success',
() {
when(
mockL10n.toast_message_empty_trash_folder_success,
).thenReturn('All messages deleted');
expect(strategy.successMessage(mockL10n), 'All messages deleted');
},
);
test('failureMessage delegates to emptyTrashFolderFailed', () {
when(mockL10n.emptyTrashFolderFailed).thenReturn('Empty trash failed');
expect(strategy.failureMessage(mockL10n), 'Empty trash failed');
});
test('subfoldersAllDeletedMessage is non-null and delegates to clearTrashSubfoldersSuccess', () {
when(mockL10n.clearTrashSubfoldersSuccess).thenReturn('Trash subfolders deleted');
expect(strategy.subfoldersAllDeletedMessage(mockL10n), 'Trash subfolders deleted');
});
test(
'subfoldersAllDeletedMessage is non-null and delegates to clearTrashSubfoldersSuccess',
() {
when(
mockL10n.clearTrashSubfoldersSuccess,
).thenReturn('Trash subfolders deleted');
expect(
strategy.subfoldersAllDeletedMessage(mockL10n),
'Trash subfolders deleted',
);
},
);
test('subfoldersPartiallyDeletedMessage is non-null and delegates to clearTrashSubfoldersPartialSuccess', () {
when(mockL10n.clearTrashSubfoldersPartialSuccess).thenReturn('Some subfolders not deleted');
expect(strategy.subfoldersPartiallyDeletedMessage(mockL10n), 'Some subfolders not deleted');
});
test(
'subfoldersPartiallyDeletedMessage is non-null and delegates to clearTrashSubfoldersPartialSuccess',
() {
when(
mockL10n.clearTrashSubfoldersPartialSuccess,
).thenReturn('Some subfolders not deleted');
expect(
strategy.subfoldersPartiallyDeletedMessage(mockL10n),
'Some subfolders not deleted',
);
},
);
test('subfoldersDeleteFailedMessage is non-null and delegates to clearTrashSubfoldersFailed', () {
when(mockL10n.clearTrashSubfoldersFailed).thenReturn('Failed to delete subfolders');
expect(strategy.subfoldersDeleteFailedMessage(mockL10n), 'Failed to delete subfolders');
});
test(
'subfoldersDeleteFailedMessage is non-null and delegates to clearTrashSubfoldersFailed',
() {
when(
mockL10n.clearTrashSubfoldersFailed,
).thenReturn('Failed to delete subfolders');
expect(
strategy.subfoldersDeleteFailedMessage(mockL10n),
'Failed to delete subfolders',
);
},
);
});
}