Skip to content

Commit 22c4622

Browse files
committed
Refactor ChatClientTest to simplify test setup
- Remove unused properties and simplify the `setUp` method. - Make test properties `private`. - Remove `runTest` from test function signatures as it's already handled by the `TestCoroutineExtension`.
1 parent aca0137 commit 22c4622

File tree

1 file changed

+32
-44
lines changed
  • stream-chat-android-client/src/test/java/io/getstream/chat/android/client

1 file changed

+32
-44
lines changed

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/ChatClientTest.kt

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,20 @@ package io.getstream.chat.android.client
1818

1919
import androidx.lifecycle.Lifecycle
2020
import androidx.lifecycle.testing.TestLifecycleOwner
21-
import io.getstream.chat.android.client.api.ChatApi
2221
import io.getstream.chat.android.client.api.ChatClientConfig
2322
import io.getstream.chat.android.client.api2.mapping.DtoMapping
2423
import io.getstream.chat.android.client.clientstate.DisconnectCause
2524
import io.getstream.chat.android.client.clientstate.UserStateService
26-
import io.getstream.chat.android.client.errorhandler.factory.ErrorHandlerFactory
2725
import io.getstream.chat.android.client.errors.ChatErrorCode
2826
import io.getstream.chat.android.client.events.ChatEvent
2927
import io.getstream.chat.android.client.events.DisconnectedEvent
3028
import io.getstream.chat.android.client.events.UnknownEvent
3129
import io.getstream.chat.android.client.extensions.cidToTypeAndId
3230
import io.getstream.chat.android.client.network.NetworkStateProvider
33-
import io.getstream.chat.android.client.notifications.ChatNotifications
3431
import io.getstream.chat.android.client.notifications.handler.NotificationConfig
3532
import io.getstream.chat.android.client.parser.EventArguments
3633
import io.getstream.chat.android.client.parser2.adapters.internal.StreamDateFormatter
3734
import io.getstream.chat.android.client.persistance.repository.noop.NoOpRepositoryFactory
38-
import io.getstream.chat.android.client.persistence.repository.ChatClientRepository
39-
import io.getstream.chat.android.client.plugin.factory.PluginFactory
4035
import io.getstream.chat.android.client.scope.ClientTestScope
4136
import io.getstream.chat.android.client.scope.UserTestScope
4237
import io.getstream.chat.android.client.socket.FakeChatSocket
@@ -70,7 +65,7 @@ import java.util.Date
7065

7166
internal class ChatClientTest {
7267

73-
companion object {
68+
private companion object {
7469
@JvmField
7570
@RegisterExtension
7671
val testCoroutines = TestCoroutineExtension()
@@ -87,43 +82,36 @@ internal class ChatClientTest {
8782
val eventF = UnknownEvent("f", createdAt, rawCreatedAt, null, emptyMap<Any, Any>())
8883
}
8984

90-
lateinit var lifecycleOwner: TestLifecycleOwner
91-
lateinit var api: ChatApi
92-
lateinit var client: ChatClient
93-
lateinit var fakeChatSocket: FakeChatSocket
94-
lateinit var result: MutableList<ChatEvent>
95-
val token = randomString()
96-
val userId = randomString()
97-
val user = randomUser(id = userId)
98-
val tokenUtils: TokenUtils = mock()
99-
var pluginFactories: List<PluginFactory> = emptyList()
100-
var errorHandlerFactories: List<ErrorHandlerFactory> = emptyList()
101-
102-
@Suppress("LongMethod")
85+
private lateinit var lifecycleOwner: TestLifecycleOwner
86+
private lateinit var client: ChatClient
87+
private lateinit var fakeChatSocket: FakeChatSocket
88+
private lateinit var result: MutableList<ChatEvent>
89+
private val token = randomString()
90+
private val userId = randomString()
91+
private val user = randomUser(id = userId)
92+
private val tokenUtils: TokenUtils = mock()
93+
10394
@BeforeEach
10495
fun setUp() {
10596
val apiKey = "api-key"
10697
val wssUrl = "socket.url"
10798
val config = ChatClientConfig(
108-
apiKey,
109-
"hello.http",
110-
"cdn.http",
111-
wssUrl,
112-
false,
113-
Mother.chatLoggerConfig(),
114-
false,
115-
false,
116-
NotificationConfig(),
99+
apiKey = apiKey,
100+
httpUrl = "hello.http",
101+
cdnHttpUrl = "cdn.http",
102+
wssUrl = wssUrl,
103+
warmUp = false,
104+
loggerConfig = Mother.chatLoggerConfig(),
105+
distinctApiCalls = false,
106+
debugRequests = false,
107+
notificationConfig = NotificationConfig(),
117108
)
118109
whenever(tokenUtils.getUserId(token)) doReturn userId
119110
lifecycleOwner = TestLifecycleOwner(coroutineDispatcher = testCoroutines.dispatcher)
120-
api = mock()
121-
val userStateService = UserStateService()
122111
val clientScope = ClientTestScope(testCoroutines.scope)
123112
val userScope = UserTestScope(clientScope)
124113
val lifecycleObserver = StreamLifecycleObserver(userScope, lifecycleOwner.lifecycle)
125114
val tokenManager = FakeTokenManager("")
126-
val notifications = mock<ChatNotifications>()
127115
val networkStateProvider: NetworkStateProvider = mock()
128116
whenever(networkStateProvider.isConnected()) doReturn true
129117
fakeChatSocket = FakeChatSocket(
@@ -136,19 +124,19 @@ internal class ChatClientTest {
136124
)
137125
client = ChatClient(
138126
config = config,
139-
api = api,
127+
api = mock(),
140128
dtoMapping = DtoMapping(NoOpMessageTransformer, NoOpUserTransformer),
141-
notifications = notifications,
129+
notifications = mock(),
142130
tokenManager = tokenManager,
143131
userCredentialStorage = mock(),
144-
userStateService = userStateService,
132+
userStateService = UserStateService(),
145133
tokenUtils = tokenUtils,
146134
clientScope = clientScope,
147135
userScope = userScope,
148136
retryPolicy = NoRetryPolicy(),
149137
appSettingsManager = mock(),
150138
chatSocket = fakeChatSocket,
151-
pluginFactories = pluginFactories,
139+
pluginFactories = emptyList(),
152140
mutableClientState = Mother.mockedClientState(),
153141
repositoryFactoryProvider = NoOpRepositoryFactory.Provider,
154142
currentUserFetcher = mock(),
@@ -184,7 +172,7 @@ internal class ChatClientTest {
184172
}
185173

186174
@Test
187-
fun `Simple subscribe for one event`() = runTest {
175+
fun `Simple subscribe for one event`() {
188176
client.subscribe {
189177
result.add(it)
190178
}
@@ -195,7 +183,7 @@ internal class ChatClientTest {
195183
}
196184

197185
@Test
198-
fun `Simple subscribe for multiple events`() = runTest {
186+
fun `Simple subscribe for multiple events`() {
199187
client.subscribe {
200188
result.add(it)
201189
}
@@ -223,7 +211,7 @@ internal class ChatClientTest {
223211
}
224212

225213
@Test
226-
fun `Subscribe for Java Class event types`() = runTest {
214+
fun `Subscribe for Java Class event types`() {
227215
client.subscribeFor(eventA::class.java, eventC::class.java) {
228216
result.add(it)
229217
}
@@ -236,7 +224,7 @@ internal class ChatClientTest {
236224
}
237225

238226
@Test
239-
fun `Subscribe for KClass event types`() = runTest {
227+
fun `Subscribe for KClass event types`() {
240228
client.subscribeFor(eventA::class, eventC::class) {
241229
result.add(it)
242230
}
@@ -249,7 +237,7 @@ internal class ChatClientTest {
249237
}
250238

251239
@Test
252-
fun `Subscribe for event types with type parameter`() = runTest {
240+
fun `Subscribe for event types with type parameter`() {
253241
client.subscribeFor<UnknownEvent> {
254242
result.add(it)
255243
}
@@ -262,7 +250,7 @@ internal class ChatClientTest {
262250
}
263251

264252
@Test
265-
fun `Subscribe for single string event type`() = runTest {
253+
fun `Subscribe for single string event type`() {
266254
client.subscribeForSingle("d") {
267255
result.add(it)
268256
}
@@ -276,7 +264,7 @@ internal class ChatClientTest {
276264
}
277265

278266
@Test
279-
fun `Subscribe for single event, with event type as type parameter`() = runTest {
267+
fun `Subscribe for single event, with event type as type parameter`() {
280268
client.subscribeForSingle<UnknownEvent> {
281269
result.add(it)
282270
}
@@ -289,7 +277,7 @@ internal class ChatClientTest {
289277
}
290278

291279
@Test
292-
fun `Unsubscribe from events`() = runTest {
280+
fun `Unsubscribe from events`() {
293281
val disposable = client.subscribe {
294282
result.add(it)
295283
}
@@ -305,7 +293,7 @@ internal class ChatClientTest {
305293
}
306294

307295
@Test
308-
fun `Given connected user When handle event with updated user Should updated user value`() = runTest {
296+
fun `Given connected user When handle event with updated user Should updated user value`() {
309297
val updateUser = user.copy(
310298
extraData = mutableMapOf(),
311299
name = "updateUserName",

0 commit comments

Comments
 (0)