|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:meta/meta.dart'; |
| 4 | +import 'package:stream_feeds/stream_feeds.dart'; |
| 5 | +import 'package:test/test.dart' as test; |
| 6 | + |
| 7 | +import '../helpers/mocks.dart'; |
| 8 | +import '../helpers/test_data.dart'; |
| 9 | +import 'base_tester.dart'; |
| 10 | + |
| 11 | +/// Test helper for activity reaction list operations. |
| 12 | +/// |
| 13 | +/// Automatically sets up WebSocket connection, client, and test infrastructure. |
| 14 | +/// Tests are tagged with 'activity-reaction-list' by default for filtering. |
| 15 | +/// |
| 16 | +/// [user] is optional, the authenticated user for the test client (defaults to luke_skywalker). |
| 17 | +
|
| 18 | +/// [build] constructs the [ActivityReactionList] under test using the provided [StreamFeedsClient]. |
| 19 | +/// [setUp] is optional and runs before [body] for setting up mocks and test state. |
| 20 | +/// [body] is the test callback that receives an [ActivityReactionListTester] for interactions. |
| 21 | +/// [verify] is optional and runs after [body] for verifying API calls and interactions. |
| 22 | +/// [tearDown] is optional and runs after [verify] for cleanup operations. |
| 23 | +/// [skip] is optional, skip this test. |
| 24 | +/// [tags] is optional, tags for test filtering. Defaults to ['activity-reaction-list']. |
| 25 | +/// [timeout] is optional, custom timeout for this test. |
| 26 | +/// |
| 27 | +/// Example: |
| 28 | +/// ```dart |
| 29 | +/// activityReactionListTest( |
| 30 | +/// 'queries initial reactions', |
| 31 | +/// build: (client) => client.activityReactionList( |
| 32 | +/// ActivityReactionsQuery( |
| 33 | +/// activityId: 'activity-1', |
| 34 | +/// ), |
| 35 | +/// ), |
| 36 | +/// setUp: (tester) => tester.get(), |
| 37 | +/// body: (tester) async { |
| 38 | +/// expect(tester.activityReactionListState.reactions, hasLength(3)); |
| 39 | +/// }, |
| 40 | +/// ); |
| 41 | +/// ``` |
| 42 | +@isTest |
| 43 | +void activityReactionListTest( |
| 44 | + String description, { |
| 45 | + User user = const User(id: 'luke_skywalker'), |
| 46 | + required ActivityReactionList Function(StreamFeedsClient client) build, |
| 47 | + FutureOr<void> Function(ActivityReactionListTester tester)? setUp, |
| 48 | + required FutureOr<void> Function(ActivityReactionListTester tester) body, |
| 49 | + FutureOr<void> Function(ActivityReactionListTester tester)? verify, |
| 50 | + FutureOr<void> Function(ActivityReactionListTester tester)? tearDown, |
| 51 | + bool skip = false, |
| 52 | + Iterable<String> tags = const ['activity-reaction-list'], |
| 53 | + test.Timeout? timeout, |
| 54 | +}) { |
| 55 | + return testWithTester( |
| 56 | + description, |
| 57 | + user: user, |
| 58 | + build: build, |
| 59 | + createTesterFn: _createActivityReactionListTester, |
| 60 | + setUp: setUp, |
| 61 | + body: body, |
| 62 | + verify: verify, |
| 63 | + tearDown: tearDown, |
| 64 | + skip: skip, |
| 65 | + tags: tags, |
| 66 | + timeout: timeout, |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +/// A test utility for activity reaction list operations with WebSocket support. |
| 71 | +/// |
| 72 | +/// Provides helper methods for emitting events and verifying activity reaction list state. |
| 73 | +/// |
| 74 | +/// Resources are automatically cleaned up after the test completes. |
| 75 | +final class ActivityReactionListTester |
| 76 | + extends BaseTester<ActivityReactionList> { |
| 77 | + const ActivityReactionListTester._({ |
| 78 | + required ActivityReactionList activityReactionList, |
| 79 | + required super.wsStreamController, |
| 80 | + required super.feedsApi, |
| 81 | + }) : super(subject: activityReactionList); |
| 82 | + |
| 83 | + /// The activity reaction list being tested. |
| 84 | + ActivityReactionList get activityReactionList => subject; |
| 85 | + |
| 86 | + /// Current state of the activity reaction list. |
| 87 | + ActivityReactionListState get activityReactionListState { |
| 88 | + return activityReactionList.state; |
| 89 | + } |
| 90 | + |
| 91 | + /// Stream of activity reaction list state updates. |
| 92 | + Stream<ActivityReactionListState> get activityReactionListStateStream { |
| 93 | + return activityReactionList.stream; |
| 94 | + } |
| 95 | + |
| 96 | + /// Gets the activity reaction list by fetching it from the API. |
| 97 | + /// |
| 98 | + /// Call this in event tests to set up initial state before emitting events. |
| 99 | + /// Skip this in API tests that only verify method calls. |
| 100 | + /// |
| 101 | + /// Parameters: |
| 102 | + /// - [modifyResponse]: Optional function to customize the activity reaction list response |
| 103 | + Future<Result<List<FeedsReactionData>>> get({ |
| 104 | + QueryActivityReactionsResponse Function( |
| 105 | + QueryActivityReactionsResponse, |
| 106 | + )? modifyResponse, |
| 107 | + }) { |
| 108 | + final query = activityReactionList.query; |
| 109 | + |
| 110 | + final defaultReactionListResponse = |
| 111 | + createDefaultQueryActivityReactionsResponse( |
| 112 | + reactions: [ |
| 113 | + createDefaultReactionResponse(activityId: query.activityId), |
| 114 | + createDefaultReactionResponse( |
| 115 | + activityId: query.activityId, |
| 116 | + userId: 'user-2', |
| 117 | + ), |
| 118 | + createDefaultReactionResponse( |
| 119 | + activityId: query.activityId, |
| 120 | + userId: 'user-3', |
| 121 | + ), |
| 122 | + ], |
| 123 | + ); |
| 124 | + |
| 125 | + mockApi( |
| 126 | + (api) => api.queryActivityReactions( |
| 127 | + activityId: query.activityId, |
| 128 | + queryActivityReactionsRequest: query.toRequest(), |
| 129 | + ), |
| 130 | + result: switch (modifyResponse) { |
| 131 | + final modifier? => modifier(defaultReactionListResponse), |
| 132 | + _ => defaultReactionListResponse, |
| 133 | + }, |
| 134 | + ); |
| 135 | + |
| 136 | + return activityReactionList.get(); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// Creates an ActivityReactionListTester for testing activity reaction list operations. |
| 141 | +// |
| 142 | +// Automatically sets up WebSocket connection and registers cleanup handlers. |
| 143 | +// This function is for internal use by activityReactionListTest only. |
| 144 | +Future<ActivityReactionListTester> _createActivityReactionListTester({ |
| 145 | + required StreamFeedsClient client, |
| 146 | + required ActivityReactionList subject, |
| 147 | + required MockDefaultApi feedsApi, |
| 148 | + required MockWebSocketChannel webSocketChannel, |
| 149 | +}) { |
| 150 | + // Dispose activity reaction list after test |
| 151 | + test.addTearDown(subject.dispose); |
| 152 | + |
| 153 | + return createTester( |
| 154 | + client: client, |
| 155 | + webSocketChannel: webSocketChannel, |
| 156 | + create: (wsStreamController) => ActivityReactionListTester._( |
| 157 | + activityReactionList: subject, |
| 158 | + wsStreamController: wsStreamController, |
| 159 | + feedsApi: feedsApi, |
| 160 | + ), |
| 161 | + ); |
| 162 | +} |
0 commit comments