diff --git a/app/test/features/spaces/search_provider_test.dart b/app/test/features/spaces/search_provider_test.dart new file mode 100644 index 000000000000..3a6dd112c44f --- /dev/null +++ b/app/test/features/spaces/search_provider_test.dart @@ -0,0 +1,56 @@ +import 'package:acter/common/providers/room_providers.dart'; +import 'package:acter/common/providers/space_providers.dart'; +import 'package:acter/features/search/providers/quick_search_providers.dart'; +import 'package:acter/features/spaces/providers/space_list_provider.dart'; +import 'package:acter_avatar/acter_avatar.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import '../../helpers/mock_space_providers.dart'; + +void main() { + group('Spaces Search Provider Test', () { + test( + 'Display Name update triggers proper update of search values on quick search', + () async { + final spaces = [ + MockSpace(id: 'a'), + MockSpace(id: 'b'), + MockSpace(id: 'c'), + ]; + final Map spaceInfos = { + 'a': const AvatarInfo(uniqueId: 'a', displayName: 'abc'), + 'b': const AvatarInfo( + uniqueId: 'b', + displayName: null, + ), // not yet loaded + 'c': const AvatarInfo(uniqueId: 'c', displayName: null), + }; + final container = ProviderContainer( + overrides: [ + spacesProvider.overrideWith((a) => MockSpaceListNotifiers(spaces)), + roomAvatarInfoProvider.overrideWith( + () => MockRoomAvatarInfoNotifier(items: spaceInfos), + ), + bookmarkedSpacesProvider.overrideWith((a) => []), + ], + ); + + final all = container.read(spaceListQuickSearchedProvider); + expect(all.length, 3); + + // add a search term + container.read(quickSearchValueProvider.notifier).state = 'a'; + + final onlyOne = container.read(spaceListQuickSearchedProvider); + expect(onlyOne.length, 1); + + // update a space info + spaceInfos['b'] = const AvatarInfo(uniqueId: 'b', displayName: 'abc'); + // we only refresh the inner provider + container.refresh(roomAvatarInfoProvider('b')); + + final itsTwo = container.read(spaceListQuickSearchedProvider); + expect(itsTwo.length, 2); + }); + }); +} diff --git a/app/test/helpers/mock_space_providers.dart b/app/test/helpers/mock_space_providers.dart index e61f5039d9b6..cfbe781e44d0 100644 --- a/app/test/helpers/mock_space_providers.dart +++ b/app/test/helpers/mock_space_providers.dart @@ -8,8 +8,12 @@ import 'package:mocktail/mocktail.dart'; class MockRoomAvatarInfoNotifier extends FamilyNotifier with Mock implements RoomAvatarInfoNotifier { + final Map items; + + MockRoomAvatarInfoNotifier({this.items = const {}}); + @override - AvatarInfo build(arg) => AvatarInfo(uniqueId: arg); + AvatarInfo build(arg) => items[arg] ?? AvatarInfo(uniqueId: arg); } class RetryMockAsyncSpaceNotifier extends FamilyAsyncNotifier @@ -28,7 +32,21 @@ class RetryMockAsyncSpaceNotifier extends FamilyAsyncNotifier } } -class MockSpace extends Fake implements Space {} +class MockSpace extends Fake implements Space { + final String id; + final bool bookmarked; + + MockSpace({ + this.id = 'id', + this.bookmarked = false, + }); + + @override + String getRoomIdStr() => id; + + @override + bool isBookmarked() => bookmarked; +} class MockSpaceHierarchyRoomInfo extends Fake implements SpaceHierarchyRoomInfo {