@@ -2,29 +2,74 @@ import 'package:checks/checks.dart';
2
2
import 'package:test/scaffolding.dart' ;
3
3
import 'package:zulip/api/model/events.dart' ;
4
4
import 'package:zulip/api/model/model.dart' ;
5
+ import 'package:zulip/model/message_list.dart' ;
6
+ import 'package:zulip/model/narrow.dart' ;
5
7
import 'package:zulip/model/store.dart' ;
6
8
9
+ import '../api/fake_api.dart' ;
7
10
import '../example_data.dart' as eg;
11
+ import 'message_list_test.dart' ;
12
+ import 'test_store.dart' ;
8
13
9
14
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.
11
16
// Each test case calls [prepare] to initialize them.
17
+ late Subscription subscription;
12
18
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 );
13
31
14
32
/// 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);
16
36
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 ();
17
61
}
18
62
19
63
Future <void > addMessages (Iterable <Message > messages) async {
20
64
for (final m in messages) {
21
65
await store.handleEvent (MessageEvent (id: 0 , message: m));
22
66
}
67
+ checkNotified (count: messageList.fetched ? messages.length : 0 );
23
68
}
24
69
25
70
group ('reconcileMessages' , () {
26
71
test ('from empty' , () async {
27
- prepare ();
72
+ await prepare ();
28
73
check (store.messages).isEmpty ();
29
74
final message1 = eg.streamMessage ();
30
75
final message2 = eg.streamMessage ();
@@ -40,7 +85,7 @@ void main() {
40
85
});
41
86
42
87
test ('from not-empty' , () async {
43
- prepare ();
88
+ await prepare ();
44
89
final message1 = eg.streamMessage ();
45
90
final message2 = eg.streamMessage ();
46
91
final message3 = eg.dmMessage (from: eg.otherUser, to: [eg.selfUser]);
@@ -58,7 +103,7 @@ void main() {
58
103
});
59
104
60
105
test ('on ID collision, new message does not clobber old in store.messages' , () async {
61
- prepare ();
106
+ await prepare ();
62
107
final message = eg.streamMessage (id: 1 , content: '<p>foo</p>' );
63
108
await addMessages ([message]);
64
109
check (store.messages).deepEquals ({1 : message});
@@ -72,7 +117,7 @@ void main() {
72
117
73
118
group ('handleMessageEvent' , () {
74
119
test ('from empty' , () async {
75
- prepare ();
120
+ await prepare ();
76
121
check (store.messages).isEmpty ();
77
122
78
123
final newMessage = eg.streamMessage ();
@@ -83,7 +128,7 @@ void main() {
83
128
});
84
129
85
130
test ('from not-empty' , () async {
86
- prepare ();
131
+ await prepare ();
87
132
final messages = [
88
133
eg.streamMessage (),
89
134
eg.streamMessage (),
@@ -103,7 +148,7 @@ void main() {
103
148
});
104
149
105
150
test ('new message clobbers old on ID collision' , () async {
106
- prepare ();
151
+ await prepare ();
107
152
final message = eg.streamMessage (id: 1 , content: '<p>foo</p>' );
108
153
await addMessages ([message]);
109
154
check (store.messages).deepEquals ({1 : message});
0 commit comments