Skip to content

Commit e78df58

Browse files
committed
Add unit tests for activity comments
1 parent fc98ea2 commit e78df58

File tree

5 files changed

+299
-13
lines changed

5 files changed

+299
-13
lines changed

packages/stream_feeds/lib/src/state/activity_state.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ class ActivityStateNotifier extends StateNotifier<ActivityState>
163163
),
164164
);
165165
}
166-
commentList.onCommentAdded(ThreadedCommentData.fromComment(comment));
167166
}
168167

169168
@override
@@ -176,7 +175,6 @@ class ActivityStateNotifier extends StateNotifier<ActivityState>
176175
),
177176
);
178177
}
179-
commentList.onCommentRemoved(comment.id);
180178
}
181179

182180
@override

packages/stream_feeds/test/state/activity_test.dart

Lines changed: 244 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: avoid_redundant_argument_values
2+
13
import 'dart:async';
24
import 'dart:convert';
35

@@ -64,7 +66,7 @@ void main() {
6466
});
6567
});
6668

67-
group('Poll events', () {
69+
group('WS events', () {
6870
late StreamController<Object> wsStreamController;
6971
late MockWebSocketSink webSocketSink;
7072

@@ -85,8 +87,11 @@ void main() {
8587
await wsStreamController.close();
8688
});
8789

88-
void setupMockActivity({GetActivityResponse? activity}) {
89-
const activityId = 'id';
90+
void setupMockActivity({
91+
String activityId = 'id',
92+
GetActivityResponse? activity,
93+
GetCommentsResponse? comments,
94+
}) {
9095
when(() => feedsApi.getActivity(id: activityId)).thenAnswer(
9196
(_) async =>
9297
Result.success(activity ?? createDefaultActivityResponse()),
@@ -98,10 +103,53 @@ void main() {
98103
depth: 3,
99104
),
100105
).thenAnswer(
101-
(_) async => Result.success(createDefaultCommentsResponse()),
106+
(_) async =>
107+
Result.success(comments ?? createDefaultCommentsResponse()),
102108
);
103109
}
104110

111+
test('poll updated', () async {
112+
final originalDate = DateTime(2021, 1, 1);
113+
final updatedDate = DateTime(2021, 1, 2);
114+
115+
final poll = createDefaultPollResponseData(updatedAt: originalDate);
116+
setupMockActivity(
117+
activity: createDefaultActivityResponse(poll: poll),
118+
);
119+
120+
final activity = client.activity(
121+
activityId: 'id',
122+
fid: const FeedId(group: 'group', id: 'id'),
123+
);
124+
await activity.get();
125+
126+
expect(poll.voteCount, 0);
127+
expect(poll.updatedAt, originalDate);
128+
129+
activity.notifier.stream.listen(
130+
expectAsync1(
131+
(event) {
132+
expect(event, isA<ActivityState>());
133+
expect(event.poll?.id, 'poll-id');
134+
expect(event.poll?.voteCount, 1);
135+
expect(event.poll?.updatedAt, updatedDate);
136+
},
137+
),
138+
);
139+
140+
wsStreamController.add(
141+
jsonEncode(
142+
PollUpdatedFeedEvent(
143+
createdAt: DateTime.now(),
144+
custom: const {},
145+
fid: 'fid',
146+
poll: poll.copyWith(voteCount: 1, updatedAt: updatedDate),
147+
type: EventTypes.pollUpdated,
148+
).toJson(),
149+
),
150+
);
151+
});
152+
105153
test('poll vote casted', () async {
106154
final poll = createDefaultPollResponseData();
107155
final pollId = poll.id;
@@ -379,5 +427,197 @@ void main() {
379427
),
380428
);
381429
});
430+
431+
test('comment added', () async {
432+
const activityId = 'activity-id';
433+
const fid = FeedId(group: 'group', id: 'id');
434+
final comment1 = createDefaultCommentResponse(
435+
objectId: activityId,
436+
id: 'comment-id-1',
437+
text: 'comment-text-1',
438+
);
439+
final comment2 = createDefaultCommentResponse(
440+
objectId: activityId,
441+
id: 'comment-id-2',
442+
text: 'comment-text-2',
443+
);
444+
445+
final initialComments = createDefaultCommentsResponse(
446+
comments: [ThreadedCommentResponse.fromJson(comment1.toJson())],
447+
);
448+
449+
setupMockActivity(
450+
activityId: activityId,
451+
activity: createDefaultActivityResponse(
452+
id: activityId,
453+
comments: [comment1],
454+
),
455+
comments: initialComments,
456+
);
457+
458+
final activity = client.activity(
459+
activityId: activityId,
460+
fid: fid,
461+
);
462+
final activityData = await activity.get();
463+
expect(activityData, isA<Result<ActivityData>>());
464+
expect(activityData.getOrNull()?.id, activityId);
465+
expect(activityData.getOrNull()?.comments.length, 1);
466+
467+
expect(activity.state.activity?.commentCount, 1);
468+
expect(activity.state.comments.length, 1);
469+
470+
// The event will trigger twice, first with updated count and then with the new comment.
471+
var count = 0;
472+
activity.notifier.stream.listen(
473+
expectAsync1(
474+
count: 2,
475+
(event) {
476+
count++;
477+
if (count == 1) {
478+
expect(event, isA<ActivityState>());
479+
expect(event.comments.length, 2);
480+
}
481+
if (count == 2) {
482+
expect(event, isA<ActivityState>());
483+
expect(event.activity?.commentCount, 2);
484+
}
485+
},
486+
),
487+
);
488+
wsStreamController.add(
489+
jsonEncode(
490+
CommentAddedEvent(
491+
type: EventTypes.commentAdded,
492+
activity: createDefaultActivityResponse().activity,
493+
createdAt: DateTime.now(),
494+
custom: const {},
495+
fid: fid.rawValue,
496+
comment: comment2,
497+
),
498+
),
499+
);
500+
});
501+
502+
test('comment updated', () async {
503+
const activityId = 'activity-id';
504+
const fid = FeedId(group: 'group', id: 'id');
505+
final comment = createDefaultCommentResponse(
506+
objectId: activityId,
507+
id: 'comment-id',
508+
text: 'comment-text',
509+
);
510+
511+
final initialComments = createDefaultCommentsResponse(
512+
comments: [ThreadedCommentResponse.fromJson(comment.toJson())],
513+
);
514+
515+
setupMockActivity(
516+
activityId: activityId,
517+
activity: createDefaultActivityResponse(
518+
id: activityId,
519+
comments: [comment],
520+
),
521+
comments: initialComments,
522+
);
523+
524+
final activity = client.activity(
525+
activityId: activityId,
526+
fid: fid,
527+
);
528+
final activityData = await activity.get();
529+
expect(activityData, isA<Result<ActivityData>>());
530+
expect(activityData.getOrNull()?.id, activityId);
531+
expect(activityData.getOrNull()?.comments.length, 1);
532+
533+
expect(activity.state.activity?.commentCount, 1);
534+
expect(activity.state.comments.first.text, 'comment-text');
535+
536+
// The event will trigger twice, first with updated count and then with the new comment.
537+
activity.notifier.stream.listen(
538+
expectAsync1(
539+
(event) {
540+
expect(event, isA<ActivityState>());
541+
expect(event.activity?.commentCount, 1);
542+
expect(event.comments.first.text, 'comment-text-2');
543+
},
544+
),
545+
);
546+
wsStreamController.add(
547+
jsonEncode(
548+
CommentUpdatedEvent(
549+
type: EventTypes.commentUpdated,
550+
createdAt: DateTime.now(),
551+
custom: const {},
552+
fid: fid.rawValue,
553+
comment: comment.copyWith(text: 'comment-text-2'),
554+
),
555+
),
556+
);
557+
});
558+
559+
test('comment removed', () async {
560+
const activityId = 'activity-id';
561+
const fid = FeedId(group: 'group', id: 'id');
562+
final comment = createDefaultCommentResponse(
563+
objectId: activityId,
564+
id: 'comment-id',
565+
text: 'comment-text',
566+
);
567+
568+
final initialComments = createDefaultCommentsResponse(
569+
comments: [ThreadedCommentResponse.fromJson(comment.toJson())],
570+
);
571+
572+
setupMockActivity(
573+
activityId: activityId,
574+
activity: createDefaultActivityResponse(
575+
id: activityId,
576+
comments: [comment],
577+
),
578+
comments: initialComments,
579+
);
580+
581+
final activity = client.activity(
582+
activityId: activityId,
583+
fid: fid,
584+
);
585+
final activityData = await activity.get();
586+
expect(activityData, isA<Result<ActivityData>>());
587+
expect(activityData.getOrNull()?.id, activityId);
588+
expect(activityData.getOrNull()?.comments.length, 1);
589+
590+
expect(activity.state.activity?.commentCount, 1);
591+
expect(activity.state.comments.length, 1);
592+
593+
// The event will trigger twice, first with updated count and then with the new comment.
594+
var count = 0;
595+
activity.notifier.stream.listen(
596+
expectAsync1(
597+
count: 2,
598+
(event) {
599+
count++;
600+
if (count == 1) {
601+
expect(event.comments, isEmpty);
602+
}
603+
if (count == 2) {
604+
expect(event, isA<ActivityState>());
605+
expect(event.activity?.commentCount, 0);
606+
}
607+
},
608+
),
609+
);
610+
wsStreamController.add(
611+
jsonEncode(
612+
CommentDeletedEvent(
613+
type: EventTypes.commentDeleted,
614+
createdAt: DateTime.now(),
615+
custom: const {},
616+
fid: fid.rawValue,
617+
comment: comment,
618+
),
619+
),
620+
);
621+
});
382622
});
383623
}

packages/stream_feeds/test/state/feed_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,43 @@ void main() {
927927
);
928928
}
929929

930+
test('poll updated', () async {
931+
final originalDate = DateTime(2021, 1, 1);
932+
final updatedDate = DateTime(2021, 1, 2);
933+
final poll = createDefaultPollResponseData(updatedAt: originalDate);
934+
final pollId = poll.id;
935+
setupMockFeed(
936+
activities: [createDefaultActivityResponse(poll: poll).activity],
937+
);
938+
939+
final feed = client.feedFromId(defaultFeedId);
940+
await feed.getOrCreate();
941+
942+
expect(poll.updatedAt, originalDate);
943+
944+
feed.notifier.stream.listen(
945+
expectAsync1(
946+
(event) {
947+
expect(event, isA<FeedState>());
948+
expect(event.activities.first.poll?.id, pollId);
949+
expect(event.activities.first.poll?.updatedAt, updatedDate);
950+
},
951+
),
952+
);
953+
954+
wsStreamController.add(
955+
jsonEncode(
956+
PollUpdatedFeedEvent(
957+
createdAt: DateTime.now(),
958+
custom: const {},
959+
fid: 'fid',
960+
poll: poll.copyWith(updatedAt: updatedDate),
961+
type: EventTypes.pollUpdated,
962+
).toJson(),
963+
),
964+
);
965+
});
966+
930967
test('poll vote casted', () async {
931968
final poll = createDefaultPollResponseData();
932969
final pollId = poll.id;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
class EventTypes {
2+
static const String commentAdded = 'feeds.comment.added';
3+
static const String commentUpdated = 'feeds.comment.updated';
4+
static const String commentDeleted = 'feeds.comment.deleted';
5+
static const String commentReactionAdded = 'feeds.comment.reaction.added';
6+
static const String commentReactionDeleted = 'feeds.comment.reaction.deleted';
7+
28
static const String followCreated = 'feeds.follow.created';
39
static const String followDeleted = 'feeds.follow.deleted';
410
static const String followUpdated = 'feeds.follow.updated';
511

612
static const String pollClosed = 'feeds.poll.closed';
713
static const String pollDeleted = 'feeds.poll.deleted';
14+
static const String pollUpdated = 'feeds.poll.updated';
815
static const String pollVoteCasted = 'feeds.poll.vote_casted';
916
static const String pollVoteRemoved = 'feeds.poll.vote_removed';
1017
}

0 commit comments

Comments
 (0)