From 3d6b2b1a7eeb02262819135421d5354617b797b8 Mon Sep 17 00:00:00 2001 From: DatDang Date: Mon, 23 Dec 2024 11:40:57 +0700 Subject: [PATCH] fixup! TF-3332 Prevent refresh when switching mailbox --- .../thread_repository_impl_test.dart | 129 ++++++++++++++++++ .../controller/thread_controller_test.dart | 51 +++++++ 2 files changed, 180 insertions(+) create mode 100644 test/features/thread/data/repository/thread_repository_impl_test.dart diff --git a/test/features/thread/data/repository/thread_repository_impl_test.dart b/test/features/thread/data/repository/thread_repository_impl_test.dart new file mode 100644 index 000000000..2ed0fd538 --- /dev/null +++ b/test/features/thread/data/repository/thread_repository_impl_test.dart @@ -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(), + MockSpec(), +]) +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'), + )); + }); + }); +} \ No newline at end of file diff --git a/test/features/thread/presentation/controller/thread_controller_test.dart b/test/features/thread/presentation/controller/thread_controller_test.dart index e582c7db1..8bf8bf57e 100644 --- a/test/features/thread/presentation/controller/thread_controller_test.dart +++ b/test/features/thread/presentation/controller/thread_controller_test.dart @@ -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'; @@ -413,5 +414,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'), + )); + + // assert + verify(mockGetEmailsInMailboxInteractor.execute( + any, + any, + limit: anyNamed('limit'), + sort: anyNamed('sort'), + emailFilter: anyNamed('emailFilter'), + propertiesCreated: anyNamed('propertiesCreated'), + propertiesUpdated: anyNamed('propertiesUpdated'), + getLatestChanges: false, + )); + }); + }); }); } \ No newline at end of file