1
+ import 'dart:convert' ;
2
+
1
3
import 'package:checks/checks.dart' ;
4
+ import 'package:http/http.dart' as http;
2
5
import 'package:flutter/widgets.dart' ;
3
6
import 'package:flutter_checks/flutter_checks.dart' ;
4
7
import 'package:flutter_test/flutter_test.dart' ;
@@ -8,6 +11,8 @@ import 'package:zulip/api/model/submessage.dart';
8
11
import 'package:zulip/model/store.dart' ;
9
12
import 'package:zulip/widgets/poll.dart' ;
10
13
14
+ import '../stdlib_checks.dart' ;
15
+ import '../api/fake_api.dart' ;
11
16
import '../example_data.dart' as eg;
12
17
import '../model/binding.dart' ;
13
18
import '../model/test_store.dart' ;
@@ -17,6 +22,8 @@ void main() {
17
22
TestZulipBinding .ensureInitialized ();
18
23
19
24
late PerAccountStore store;
25
+ late FakeApiConnection connection;
26
+ late Message message;
20
27
21
28
Future <void > preparePollWidget (
22
29
WidgetTester tester,
@@ -28,13 +35,14 @@ void main() {
28
35
await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
29
36
store = await testBinding.globalStore.perAccount (eg.selfAccount.id);
30
37
await store.addUsers (users ?? [eg.selfUser, eg.otherUser]);
38
+ connection = store.connection as FakeApiConnection ;
31
39
32
- Message message = eg.streamMessage (
40
+ message = eg.streamMessage (
33
41
sender: eg.selfUser,
34
42
submessages: [eg.submessage (content: submessageContent)]);
35
43
await store.handleEvent (MessageEvent (id: 0 , message: message));
36
44
await tester.pumpWidget (TestZulipApp (accountId: eg.selfAccount.id,
37
- child: PollWidget (poll: message.poll! )));
45
+ child: PollWidget (messageId : message.id, poll: message.poll! )));
38
46
await tester.pump ();
39
47
40
48
for (final (voter, idx) in voterIdxPairs) {
@@ -106,4 +114,41 @@ void main() {
106
114
question: 'title' , options: []));
107
115
check (findInPoll (find.text ('This poll has no options yet.' ))).findsOne ();
108
116
});
117
+
118
+ void checkVoteRequest (PollOptionKey key, PollVoteOp op) {
119
+ check (connection.takeRequests ()).single.isA< http.Request > ()
120
+ ..method.equals ('POST' )
121
+ ..url.path.equals ('/api/v1/submessage' )
122
+ ..bodyFields.deepEquals ({
123
+ 'message_id' : jsonEncode (message.id),
124
+ 'msg_type' : 'widget' ,
125
+ 'content' : jsonEncode (PollVoteEventSubmessage (key: key, op: op)),
126
+ });
127
+ }
128
+
129
+ testWidgets ('tap to toggle vote' , (tester) async {
130
+ await preparePollWidget (tester, eg.pollWidgetData (
131
+ question: 'title' , options: ['A' ]), voterIdxPairs: [(eg.otherUser, 0 )]);
132
+ final optionKey = PollEventSubmessage .optionKey (senderId: null , idx: 0 );
133
+
134
+ // Because eg.selfUser didn't vote for the option, add their vote.
135
+ connection.prepare (json: {});
136
+ await tester.tap (findTextAtRow ('1' , index: 0 ));
137
+ await tester.pump (Duration .zero);
138
+ checkVoteRequest (optionKey, PollVoteOp .add);
139
+
140
+ // We don't local echo right now,
141
+ // so wait to hear from the server to get the poll updated.
142
+ await store.handleEvent (
143
+ eg.submessageEvent (message.id, eg.selfUser.userId,
144
+ content: PollVoteEventSubmessage (key: optionKey, op: PollVoteOp .add)));
145
+ // Wait for the poll widget rebuild
146
+ await tester.pump (Duration .zero);
147
+
148
+ // Because eg.selfUser did vote for the option, remove their vote.
149
+ connection.prepare (json: {});
150
+ await tester.tap (findTextAtRow ('2' , index: 0 ));
151
+ await tester.pump (Duration .zero);
152
+ checkVoteRequest (optionKey, PollVoteOp .remove);
153
+ });
109
154
}
0 commit comments