Skip to content

Commit ab737f6

Browse files
committed
Add basic unit tests for activity
1 parent 8c659cc commit ab737f6

File tree

5 files changed

+138
-4
lines changed

5 files changed

+138
-4
lines changed

packages/stream_feeds/lib/src/client/feeds_client_impl.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class StreamFeedsClientImpl implements StreamFeedsClient {
6565
NetworkStateProvider? networkStateProvider,
6666
AppLifecycleStateProvider? appLifecycleStateProvider,
6767
List<AutomaticReconnectionPolicy>? reconnectionPolicies,
68+
WebSocketProvider? wsProvider,
69+
api.DefaultApi? feedsRestApi,
6870
}) {
6971
// TODO: Make this configurable
7072
const endpointConfig = EndpointConfig.production;
@@ -105,6 +107,7 @@ class StreamFeedsClientImpl implements StreamFeedsClient {
105107
event_resolvers.pollAnswerCastedFeedEventResolver,
106108
event_resolvers.pollAnswerRemovedFeedEventResolver,
107109
],
110+
wsProvider: wsProvider,
108111
);
109112

110113
_connectionRecoveryHandler = ConnectionRecoveryHandler(
@@ -152,7 +155,7 @@ class StreamFeedsClientImpl implements StreamFeedsClient {
152155
_cdnClient = config.cdnClient ?? FeedsCdnClient(CdnApi(httpClient));
153156
attachmentUploader = StreamAttachmentUploader(cdn: _cdnClient);
154157

155-
final feedsApi = api.DefaultApi(httpClient);
158+
final feedsApi = feedsRestApi ?? api.DefaultApi(httpClient);
156159

157160
_activitiesRepository = ActivitiesRepository(feedsApi, attachmentUploader);
158161
_appRepository = AppRepository(feedsApi);

packages/stream_feeds/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ dev_dependencies:
4141
json_serializable: ^6.9.5
4242
mocktail: ^1.0.4
4343
retrofit_generator: ^9.6.0
44-
test: ^1.26.3
44+
test: ^1.26.3
45+
web_socket_channel: ^3.0.3

packages/stream_feeds/test/client/feeds_client_impl_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import 'package:stream_feeds/src/client/feeds_client_impl.dart';
22
import 'package:stream_feeds/stream_feeds.dart';
33
import 'package:test/test.dart';
44

5-
const testToken =
6-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoibHVrZV9za3l3YWxrZXIifQ.hZ59SWtp_zLKVV9ShkqkTsCGi_jdPHly7XNCf5T_Ev0';
5+
import '../mocks.dart';
6+
77
void main() {
88
test('Create a feeds client', () {
99
final client = StreamFeedsClient(

packages/stream_feeds/test/mocks.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import 'package:mocktail/mocktail.dart';
2+
import 'package:stream_core/stream_core.dart';
23
import 'package:stream_feeds/src/client/feeds_client_impl.dart';
34
import 'package:stream_feeds/src/repository/feeds_repository.dart';
45
import 'package:stream_feeds/stream_feeds.dart';
6+
import 'package:stream_feeds/stream_feeds.dart' as api;
7+
import 'package:web_socket_channel/web_socket_channel.dart';
58

69
class MockFeedsRepository extends Mock implements FeedsRepository {}
710

811
class MockFeedsClient extends Mock implements StreamFeedsClient {}
912

1013
class MockWebSocketClient extends Mock implements StreamWebSocketClient {}
1114

15+
class MockDefaultApi extends Mock implements api.DefaultApi {}
16+
17+
class MockWebSocketChannel extends Mock implements WebSocketChannel {}
18+
1219
class FakeFeedsClient extends Fake implements StreamFeedsClientImpl {
1320
FakeFeedsClient({
1421
User? user,
@@ -25,3 +32,6 @@ const fakeUser = User(
2532
id: 'user_id',
2633
name: 'user_name',
2734
);
35+
36+
const testToken =
37+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoibHVrZV9za3l3YWxrZXIifQ.hZ59SWtp_zLKVV9ShkqkTsCGi_jdPHly7XNCf5T_Ev0';
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// ignore_for_file: avoid_redundant_argument_values
2+
3+
import 'package:mocktail/mocktail.dart';
4+
import 'package:stream_feeds/src/client/feeds_client_impl.dart';
5+
import 'package:stream_feeds/stream_feeds.dart';
6+
import 'package:test/test.dart';
7+
8+
import '../mocks.dart';
9+
10+
void main() {
11+
late StreamFeedsClientImpl client;
12+
late MockDefaultApi feedsApi;
13+
late MockWebSocketChannel webSocketChannel;
14+
15+
setUp(() {
16+
feedsApi = MockDefaultApi();
17+
webSocketChannel = MockWebSocketChannel();
18+
19+
client = StreamFeedsClientImpl(
20+
apiKey: 'apiKey',
21+
user: const User(id: 'userId'),
22+
tokenProvider: TokenProvider.static(UserToken(testToken)),
23+
feedsRestApi: feedsApi,
24+
wsProvider: (options) => webSocketChannel,
25+
);
26+
});
27+
28+
tearDown(() {
29+
client.disconnect();
30+
});
31+
32+
group('Getting an activity', () {
33+
test('fetch activity and comments', () async {
34+
const activityId = 'id';
35+
when(() => feedsApi.getActivity(id: activityId)).thenAnswer(
36+
(_) async => Result.success(defaultActivityResponse),
37+
);
38+
when(
39+
() => feedsApi.getComments(
40+
objectId: activityId,
41+
objectType: 'activity',
42+
depth: 3,
43+
),
44+
).thenAnswer(
45+
(_) async => const Result.success(defaultCommentsResponse),
46+
);
47+
48+
final activity = client.activity(
49+
activityId: activityId,
50+
fid: const FeedId(group: 'group', id: 'id'),
51+
);
52+
53+
expect(activity, isA<Activity>());
54+
expect(activity.activityId, 'id');
55+
56+
verifyNever(() => feedsApi.getActivity(id: 'id'));
57+
58+
final result = await activity.get();
59+
60+
verify(() => feedsApi.getActivity(id: 'id')).called(1);
61+
expect(result, isA<Result<ActivityData>>());
62+
expect(result.getOrNull()?.id, 'id');
63+
});
64+
});
65+
}
66+
67+
const defaultCommentsResponse = GetCommentsResponse(
68+
comments: [],
69+
next: null,
70+
prev: null,
71+
duration: 'duration',
72+
);
73+
74+
final defaultActivityResponse = GetActivityResponse(
75+
activity: ActivityResponse(
76+
id: 'id',
77+
attachments: const [],
78+
bookmarkCount: 0,
79+
commentCount: 0,
80+
comments: const [],
81+
createdAt: DateTime(2021, 1, 1),
82+
custom: const {},
83+
feeds: const [],
84+
filterTags: const [],
85+
interestTags: const [],
86+
latestReactions: const [],
87+
mentionedUsers: const [],
88+
moderation: null,
89+
notificationContext: null,
90+
ownBookmarks: const [],
91+
ownReactions: const [],
92+
parent: null,
93+
poll: null,
94+
popularity: 0,
95+
reactionCount: 0,
96+
reactionGroups: const {},
97+
score: 0,
98+
searchData: const {},
99+
shareCount: 0,
100+
text: null,
101+
type: 'type',
102+
updatedAt: DateTime(2021, 2, 1),
103+
user: UserResponse(
104+
id: 'id',
105+
name: 'name',
106+
banned: false,
107+
blockedUserIds: const [],
108+
createdAt: DateTime(2021, 1, 1),
109+
custom: const {},
110+
language: 'language',
111+
online: false,
112+
role: 'role',
113+
teams: const [],
114+
updatedAt: DateTime(2021, 2, 1),
115+
),
116+
visibility: ActivityResponseVisibility.public,
117+
visibilityTag: null,
118+
),
119+
duration: 'duration',
120+
);

0 commit comments

Comments
 (0)