Skip to content

Commit fb4e0be

Browse files
gnpricechrisbobbe
authored andcommitted
message test [nfc]: Add a MessageListView, optionally
Some of the existing tests of MessageListView in message_list_test.dart are really almost entirely about acting on individual messages rather than message lists. They therefore belong as tests of the overall message store, to live in this file. With that "almost", though, they have a few ties to actually testing the MessageListView itself -- notably, its notification behavior. So to simplify converting them and moving them into this file, we add a MessageListView they can use. This logic is largely copied from message_list_test.dart.
1 parent 31db256 commit fb4e0be

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

test/model/message_test.dart

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,68 @@ import 'package:checks/checks.dart';
22
import 'package:test/scaffolding.dart';
33
import 'package:zulip/api/model/events.dart';
44
import 'package:zulip/api/model/model.dart';
5+
import 'package:zulip/model/message_list.dart';
6+
import 'package:zulip/model/narrow.dart';
57
import 'package:zulip/model/store.dart';
68

9+
import '../api/fake_api.dart';
710
import '../example_data.dart' as eg;
11+
import 'message_list_test.dart';
12+
import 'test_store.dart';
813

914
void main() {
10-
// These variables are the common state operated on by each test.
15+
// These "late" variables are the common state operated on by each test.
1116
// Each test case calls [prepare] to initialize them.
17+
late Subscription subscription;
1218
late PerAccountStore store;
19+
late FakeApiConnection connection;
20+
// [messageList] is here only for the sake of checking when it notifies.
21+
// For anything deeper than that, use `message_list_test.dart`.
22+
late MessageListView messageList;
23+
late int notifiedCount;
24+
25+
void checkNotified({required int count}) {
26+
check(notifiedCount).equals(count);
27+
notifiedCount = 0;
28+
}
29+
void checkNotNotified() => checkNotified(count: 0);
30+
void checkNotifiedOnce() => checkNotified(count: 1);
1331

1432
/// Initialize [store] and the rest of the test state.
15-
void prepare() {
16-
store = eg.store();
33+
void prepare({Narrow narrow = const CombinedFeedNarrow()}) {
34+
final stream = eg.stream();
35+
subscription = eg.subscription(stream);
36+
store = eg.store()
37+
..addStream(stream)..addSubscription(subscription);
38+
connection = store.connection as FakeApiConnection;
39+
notifiedCount = 0;
40+
messageList = MessageListView.init(store: store, narrow: narrow)
41+
..addListener(() {
42+
notifiedCount++;
43+
});
44+
check(messageList).fetched.isFalse();
45+
checkNotNotified();
46+
}
47+
48+
/// Perform the initial message fetch for [messageList].
49+
///
50+
/// The test case must have already called [prepare] to initialize the state.
51+
// ignore: unused_element
52+
Future<void> prepareMessages(
53+
List<Message> messages, {
54+
bool foundOldest = false,
55+
}) async {
56+
connection.prepare(json:
57+
newestResult(foundOldest: foundOldest, messages: messages).toJson());
58+
await messageList.fetchInitial();
59+
checkNotifiedOnce();
1760
}
1861

1962
Future<void> addMessages(Iterable<Message> messages) async {
2063
for (final m in messages) {
2164
await store.handleEvent(MessageEvent(id: 0, message: m));
2265
}
66+
checkNotified(count: messageList.fetched ? messages.length : 0);
2367
}
2468

2569
group('reconcileMessages', () {

0 commit comments

Comments
 (0)