Skip to content

Commit 9e3907a

Browse files
committed
autocomplete test [nfc]: Use deepEquals and conditions for end-to-end test
This will make it more smoothly generalize to situations where different types of results are more intermingled.
1 parent b10b058 commit 9e3907a

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

test/model/autocomplete_checks.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:checks/checks.dart';
22
import 'package:zulip/api/model/model.dart';
33
import 'package:zulip/model/autocomplete.dart';
4+
import 'package:zulip/model/compose.dart';
45
import 'package:zulip/widgets/compose_box.dart';
56

67
extension ComposeContentControllerChecks on Subject<ComposeContentController> {
@@ -20,6 +21,10 @@ extension UserMentionAutocompleteResultChecks on Subject<UserMentionAutocomplete
2021
Subject<int> get userId => has((r) => r.userId, 'userId');
2122
}
2223

24+
extension WildcardMentionAutocompleteResultChecks on Subject<WildcardMentionAutocompleteResult> {
25+
Subject<WildcardMentionOption> get wildcardOption => has((x) => x.wildcardOption, 'wildcardOption');
26+
}
27+
2328
extension TopicAutocompleteResultChecks on Subject<TopicAutocompleteResult> {
2429
Subject<TopicName> get topic => has((r) => r.topic, 'topic');
2530
}

test/model/autocomplete_test.dart

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -813,11 +813,15 @@ void main() {
813813
return results;
814814
}
815815

816-
Iterable<int> getUsersFromResults(Iterable<MentionAutocompleteResult> results)
817-
=> results.map((e) => (e as UserMentionAutocompleteResult).userId);
816+
Condition<Object?> isUser(int userId) {
817+
return (it) => it.isA<UserMentionAutocompleteResult>()
818+
.userId.equals(userId);
819+
}
818820

819-
Iterable<WildcardMentionOption> getWildcardOptionsFromResults(Iterable<MentionAutocompleteResult> results)
820-
=> results.map((e) => (e as WildcardMentionAutocompleteResult).wildcardOption);
821+
Condition<Object?> isWildcard(WildcardMentionOption option) {
822+
return (it) => it.isA<WildcardMentionAutocompleteResult>()
823+
.wildcardOption.equals(option);
824+
}
821825

822826
final stream = eg.stream();
823827
const topic = 'topic';
@@ -848,20 +852,21 @@ void main() {
848852
// 3. Users most recent in the DM conversations.
849853
// 4. Human vs. Bot users (human users come first).
850854
// 5. Users by name alphabetical order.
851-
final results1 = await getResults(topicNarrow, MentionAutocompleteQuery(''));
852-
check(getWildcardOptionsFromResults(results1.take(2)))
853-
.deepEquals([WildcardMentionOption.all, WildcardMentionOption.topic]);
854-
check(getUsersFromResults(results1.skip(2)))
855-
.deepEquals([1, 5, 4, 2, 7, 3, 6]);
855+
check(await getResults(topicNarrow, MentionAutocompleteQuery(''))).deepEquals([
856+
isWildcard(WildcardMentionOption.all),
857+
isWildcard(WildcardMentionOption.topic),
858+
...[1, 5, 4, 2, 7, 3, 6].map(isUser),
859+
]);
856860

857861
// Check the ranking applies also to results filtered by a query.
858-
final results2 = await getResults(topicNarrow, MentionAutocompleteQuery('t'));
859-
check(getWildcardOptionsFromResults(results2.take(2)))
860-
.deepEquals([WildcardMentionOption.stream, WildcardMentionOption.topic]);
861-
check(getUsersFromResults(results2.skip(2))).deepEquals([2, 3]);
862-
final results3 = await getResults(topicNarrow, MentionAutocompleteQuery('f'));
863-
check(getWildcardOptionsFromResults(results3.take(0))).deepEquals([]);
864-
check(getUsersFromResults(results3.skip(0))).deepEquals([5, 4]);
862+
check(await getResults(topicNarrow, MentionAutocompleteQuery('t'))).deepEquals([
863+
isWildcard(WildcardMentionOption.stream),
864+
isWildcard(WildcardMentionOption.topic),
865+
isUser(2), isUser(3),
866+
]);
867+
check(await getResults(topicNarrow, MentionAutocompleteQuery('f'))).deepEquals([
868+
isUser(5), isUser(4),
869+
]);
865870
});
866871
});
867872

0 commit comments

Comments
 (0)