Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TF-3332 Prevent refresh when switching mailbox #3368

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions test/features/thread/data/repository/thread_repository_impl_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import 'package:core/data/model/source_type/data_source_type.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:tmail_ui_user/features/mailbox/data/datasource/state_datasource.dart';
import 'package:tmail_ui_user/features/thread/data/datasource/thread_datasource.dart';
import 'package:tmail_ui_user/features/thread/data/model/email_change_response.dart';
import 'package:tmail_ui_user/features/thread/data/repository/thread_repository_impl.dart';

import '../../../../fixtures/account_fixtures.dart';
import '../../../../fixtures/session_fixtures.dart';
import 'thread_repository_impl_test.mocks.dart';

@GenerateNiceMocks([
MockSpec<ThreadDataSource>(),
MockSpec<StateDataSource>(),
])
void main() {
final threadDataSource = MockThreadDataSource();
final stateDataSource = MockStateDataSource();
final threadRepository = ThreadRepositoryImpl(
{
DataSourceType.network: threadDataSource,
DataSourceType.local: threadDataSource,
},
stateDataSource,
);

group('thread repository impl test:', () {
test(
'should not call threadDatasource.getChanges '
'when getAllEmail is called '
'and getLatestChanges is false',
() async {
// arrange
when(threadDataSource.getAllEmailCache(
any,
any,
filterOption: anyNamed('filterOption'),
inMailboxId: anyNamed('inMailboxId'),
limit: anyNamed('limit'),
sort: anyNamed('sort'),
)).thenAnswer(
(_) => Future.value(List.generate(30, (index) => Email(id: EmailId(Id('$index'))))),
);
when(stateDataSource.getState(
any,
any,
any,
)).thenAnswer((_) => Future.value(State('some-state')));
when(threadDataSource.getChanges(
any,
any,
any,
propertiesCreated: anyNamed('propertiesCreated'),
propertiesUpdated: anyNamed('propertiesUpdated'),
)).thenAnswer(
(_) => Future.value(EmailChangeResponse(hasMoreChanges: false)),
);

// act
await threadRepository.getAllEmail(
SessionFixtures.aliceSession,
AccountFixtures.aliceAccountId,
getLatestChanges: false,
).last;

// assert
verifyNever(threadDataSource.getChanges(
any,
any,
any,
propertiesCreated: anyNamed('propertiesCreated'),
propertiesUpdated: anyNamed('propertiesUpdated'),
));
});

test(
'should call threadDatasource.getChanges '
'when getAllEmail is called '
'and getLatestChanges is true',
() async {
// arrange
when(threadDataSource.getAllEmailCache(
any,
any,
filterOption: anyNamed('filterOption'),
inMailboxId: anyNamed('inMailboxId'),
limit: anyNamed('limit'),
sort: anyNamed('sort'),
)).thenAnswer(
(_) => Future.value(List.generate(30, (index) => Email(id: EmailId(Id('$index'))))),
);
when(stateDataSource.getState(
any,
any,
any,
)).thenAnswer((_) => Future.value(State('some-state')));
when(threadDataSource.getChanges(
any,
any,
any,
propertiesCreated: anyNamed('propertiesCreated'),
propertiesUpdated: anyNamed('propertiesUpdated'),
)).thenAnswer(
(_) => Future.value(EmailChangeResponse(hasMoreChanges: false)),
);

// act
await threadRepository.getAllEmail(
SessionFixtures.aliceSession,
AccountFixtures.aliceAccountId,
getLatestChanges: true,
).last;

// assert
verify(threadDataSource.getChanges(
any,
any,
any,
propertiesCreated: anyNamed('propertiesCreated'),
propertiesUpdated: anyNamed('propertiesUpdated'),
));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import 'package:tmail_ui_user/features/thread/domain/usecases/refresh_changes_em
import 'package:tmail_ui_user/features/thread/domain/usecases/search_email_interactor.dart';
import 'package:tmail_ui_user/features/thread/domain/usecases/search_more_email_interactor.dart';
import 'package:tmail_ui_user/features/thread/presentation/model/search_state.dart';
import 'package:tmail_ui_user/features/thread/presentation/model/search_status.dart';
import 'package:tmail_ui_user/features/thread/presentation/thread_controller.dart';
import 'package:tmail_ui_user/main/bindings/network/binding_tag.dart';
import 'package:tmail_ui_user/main/utils/toast_manager.dart';
Expand Down Expand Up @@ -418,5 +419,55 @@ void main() {
expect(mockMailboxDashBoardController.emailsInCurrentMailbox.length, equals(0));
});
});

group('_registerObxStreamListener test:', () {
test(
'should call _getEmailsInMailboxInteractor.execute with getLatestChanges is false '
'when mailboxDashBoardController.selectedMailbox updated',
() async {
// arrange
final mailboxBefore = PresentationMailbox(MailboxId(Id('mailbox-before-id')));
final mailboxAfter = PresentationMailbox(MailboxId(Id('mailbox-after-id')));
final selectedMailbox = Rxn(mailboxBefore);
when(mockMailboxDashBoardController.sessionCurrent).thenReturn(SessionFixtures.aliceSession);
when(mockMailboxDashBoardController.accountId).thenReturn(Rxn(AccountFixtures.aliceAccountId));
when(mockMailboxDashBoardController.selectedMailbox).thenReturn(selectedMailbox);
when(mockMailboxDashBoardController.searchController).thenReturn(mockSearchController);
when(mockMailboxDashBoardController.dashBoardAction).thenReturn(Rxn());
when(mockMailboxDashBoardController.emailUIAction).thenReturn(Rxn());
when(mockMailboxDashBoardController.viewState).thenReturn(Rx(Right(UIState.idle)));
when(mockMailboxDashBoardController.emailsInCurrentMailbox).thenReturn(RxList());
when(mockMailboxDashBoardController.listEmailSelected).thenReturn(RxList());
when(mockMailboxDashBoardController.currentSelectMode).thenReturn(Rx(SelectMode.INACTIVE));
when(mockMailboxDashBoardController.filterMessageOption).thenReturn(Rx(FilterMessageOption.all));
when(mockSearchController.searchState).thenReturn(SearchState(SearchStatus.INACTIVE).obs);

// act
threadController.onInit();
mockMailboxDashBoardController.selectedMailbox.value = mailboxAfter;
await untilCalled(mockGetEmailsInMailboxInteractor.execute(
any,
any,
limit: anyNamed('limit'),
sort: anyNamed('sort'),
emailFilter: anyNamed('emailFilter'),
propertiesCreated: anyNamed('propertiesCreated'),
propertiesUpdated: anyNamed('propertiesUpdated'),
getLatestChanges: anyNamed('getLatestChanges'),
dab246 marked this conversation as resolved.
Show resolved Hide resolved
));

// assert
verify(mockGetEmailsInMailboxInteractor.execute(
any,
any,
limit: anyNamed('limit'),
sort: anyNamed('sort'),
emailFilter: anyNamed('emailFilter'),
propertiesCreated: anyNamed('propertiesCreated'),
propertiesUpdated: anyNamed('propertiesUpdated'),
getLatestChanges: false,
));
});
});
});
}
Loading