Skip to content

Commit 13a8b28

Browse files
committed
test: Support testing with poll messages in the message list
When we set up the message list in tests, we do it by preparing the API response for a message fetch or new-message event, via JSON. But {Stream,Dm}Message.toJson drops poll data on the floor, which defeats setting up a poll-style message in the message list. Until now, anyway, with this workaround. A reference in a dartdoc to something called `prepareMessageWithSubmessages` was dangling; it seemed to be dangling when it first appeared, too, in 5af5c76; there's nothing by that name.
1 parent c1fbbe5 commit 13a8b28

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

lib/api/model/submessage.dart

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class Submessage {
6464
widgetData: widgetData,
6565
pollEventSubmessages: submessages.skip(1),
6666
messageSenderId: messageSenderId,
67+
debugSubmessages: kDebugMode ? submessages : null,
6768
);
6869
case UnsupportedWidgetData():
6970
assert(debugLog('Unsupported widgetData: ${widgetData.json}'));
@@ -368,11 +369,13 @@ class Poll extends ChangeNotifier {
368369
required PollWidgetData widgetData,
369370
required Iterable<Submessage> pollEventSubmessages,
370371
required int messageSenderId,
372+
required List<Submessage>? debugSubmessages,
371373
}) {
372374
final poll = Poll._(
373375
messageSenderId: messageSenderId,
374376
question: widgetData.extraData.question,
375377
options: widgetData.extraData.options,
378+
debugSubmessages: debugSubmessages,
376379
);
377380

378381
for (final submessage in pollEventSubmessages) {
@@ -386,17 +389,23 @@ class Poll extends ChangeNotifier {
386389
required this.messageSenderId,
387390
required this.question,
388391
required List<String> options,
392+
required List<Submessage>? debugSubmessages,
389393
}) {
390394
for (int index = 0; index < options.length; index += 1) {
391395
// Initial poll options use a placeholder senderId.
392396
// See [PollEventSubmessage.optionKey] for details.
393397
_addOption(senderId: null, idx: index, option: options[index]);
394398
}
399+
if (kDebugMode) {
400+
_debugSubmessages = debugSubmessages;
401+
}
395402
}
396403

397404
final int messageSenderId;
398405
String question;
399406

407+
List<Submessage>? _debugSubmessages;
408+
400409
/// The limit of options any single user can add to a poll.
401410
///
402411
/// See https://github.com/zulip/zulip/blob/304d948416465c1a085122af5d752f03d6797003/web/shared/src/poll_data.ts#L69-L71
@@ -417,6 +426,14 @@ class Poll extends ChangeNotifier {
417426
}
418427
_applyEvent(event.senderId, pollEventSubmessage);
419428
notifyListeners();
429+
430+
if (kDebugMode) {
431+
assert(_debugSubmessages != null);
432+
_debugSubmessages!.add(Submessage(
433+
senderId: event.senderId,
434+
msgType: event.msgType,
435+
content: event.content));
436+
}
420437
}
421438

422439
void _applyEvent(int senderId, PollEventSubmessage event) {
@@ -472,9 +489,18 @@ class Poll extends ChangeNotifier {
472489
}
473490

474491
static List<Submessage> toJson(Poll? poll) {
475-
// Rather than maintaining a up-to-date submessages list, return as if it is
476-
// empty, because we are not sending the submessages to the server anyway.
477-
return [];
492+
List<Submessage>? result;
493+
494+
if (kDebugMode) {
495+
// Useful for setting up a message list with a poll message, which goes
496+
// through this codepath (when preparing a fetch response).
497+
result = poll?._debugSubmessages;
498+
}
499+
500+
// In prod, rather than maintaining a up-to-date submessages list,
501+
// return as if it is empty, because we are not sending the submessages
502+
// to the server anyway.
503+
return result ?? [];
478504
}
479505
}
480506

test/model/message_test.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,10 @@ void main() {
6161
/// Perform the initial message fetch for [messageList].
6262
///
6363
/// The test case must have already called [prepare] to initialize the state.
64-
///
65-
/// This does not support submessages. Use [prepareMessageWithSubmessages]
66-
/// instead if needed.
6764
Future<void> prepareMessages(
6865
List<Message> messages, {
6966
bool foundOldest = false,
7067
}) async {
71-
assert(messages.every((message) => message.poll == null));
7268
connection.prepare(json:
7369
eg.newestGetMessagesResult(foundOldest: foundOldest, messages: messages).toJson());
7470
await messageList.fetchInitial();

0 commit comments

Comments
 (0)