Skip to content

Commit 197f9f4

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 d8949c1 commit 197f9f4

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed

test/model/message_test.dart

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,74 @@ 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() {
33+
Future<void> prepare({Narrow narrow = const CombinedFeedNarrow()}) async {
34+
final stream = eg.stream();
35+
subscription = eg.subscription(stream);
1636
store = eg.store();
37+
await store.addStream(stream);
38+
await store.addSubscription(subscription);
39+
connection = store.connection as FakeApiConnection;
40+
notifiedCount = 0;
41+
messageList = MessageListView.init(store: store, narrow: narrow)
42+
..addListener(() {
43+
notifiedCount++;
44+
});
45+
check(messageList).fetched.isFalse();
46+
checkNotNotified();
47+
}
48+
49+
/// Perform the initial message fetch for [messageList].
50+
///
51+
/// The test case must have already called [prepare] to initialize the state.
52+
// ignore: unused_element
53+
Future<void> prepareMessages(
54+
List<Message> messages, {
55+
bool foundOldest = false,
56+
}) async {
57+
connection.prepare(json:
58+
newestResult(foundOldest: foundOldest, messages: messages).toJson());
59+
await messageList.fetchInitial();
60+
checkNotifiedOnce();
1761
}
1862

1963
Future<void> addMessages(Iterable<Message> messages) async {
2064
for (final m in messages) {
2165
await store.handleEvent(MessageEvent(id: 0, message: m));
2266
}
67+
checkNotified(count: messageList.fetched ? messages.length : 0);
2368
}
2469

2570
group('reconcileMessages', () {
2671
test('from empty', () async {
27-
prepare();
72+
await prepare();
2873
check(store.messages).isEmpty();
2974
final message1 = eg.streamMessage();
3075
final message2 = eg.streamMessage();
@@ -40,7 +85,7 @@ void main() {
4085
});
4186

4287
test('from not-empty', () async {
43-
prepare();
88+
await prepare();
4489
final message1 = eg.streamMessage();
4590
final message2 = eg.streamMessage();
4691
final message3 = eg.dmMessage(from: eg.otherUser, to: [eg.selfUser]);
@@ -58,7 +103,7 @@ void main() {
58103
});
59104

60105
test('on ID collision, new message does not clobber old in store.messages', () async {
61-
prepare();
106+
await prepare();
62107
final message = eg.streamMessage(id: 1, content: '<p>foo</p>');
63108
await addMessages([message]);
64109
check(store.messages).deepEquals({1: message});
@@ -72,7 +117,7 @@ void main() {
72117

73118
group('handleMessageEvent', () {
74119
test('from empty', () async {
75-
prepare();
120+
await prepare();
76121
check(store.messages).isEmpty();
77122

78123
final newMessage = eg.streamMessage();
@@ -83,7 +128,7 @@ void main() {
83128
});
84129

85130
test('from not-empty', () async {
86-
prepare();
131+
await prepare();
87132
final messages = [
88133
eg.streamMessage(),
89134
eg.streamMessage(),
@@ -103,7 +148,7 @@ void main() {
103148
});
104149

105150
test('new message clobbers old on ID collision', () async {
106-
prepare();
151+
await prepare();
107152
final message = eg.streamMessage(id: 1, content: '<p>foo</p>');
108153
await addMessages([message]);
109154
check(store.messages).deepEquals({1: message});

0 commit comments

Comments
 (0)