-
Notifications
You must be signed in to change notification settings - Fork 20
/
SearchConversationMessagesViewModelTest.kt
257 lines (230 loc) · 8.95 KB
/
SearchConversationMessagesViewModelTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.ui.home.conversations.messages
import android.os.Bundle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.core.os.bundleOf
import androidx.lifecycle.SavedStateHandle
import androidx.paging.PagingData
import androidx.paging.map
import app.cash.turbine.test
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.config.NavigationTestExtension
import com.wire.android.config.TestDispatcherProvider
import com.wire.android.config.mockUri
import com.wire.android.ui.home.conversations.mock.mockMessageWithText
import com.wire.android.ui.home.conversations.model.MessageBody
import com.wire.android.ui.home.conversations.model.UIMessage
import com.wire.android.ui.home.conversations.model.UIMessageContent
import com.wire.android.ui.home.conversations.search.messages.SearchConversationMessagesNavArgs
import com.wire.android.ui.home.conversations.search.messages.SearchConversationMessagesViewModel
import com.wire.android.ui.home.conversations.usecase.GetConversationMessagesFromSearchUseCase
import com.wire.android.ui.navArgs
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.data.id.ConversationId
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.impl.annotations.MockK
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
@OptIn(ExperimentalCoroutinesApi::class)
@ExtendWith(CoroutineTestExtension::class)
@ExtendWith(NavigationTestExtension::class)
class SearchConversationMessagesViewModelTest {
@Test
fun `given search term, when searching for messages, then specific messages are returned`() = runTest {
// given
val message1 = mockMessageWithText.copy(
messageContent = UIMessageContent.TextMessage(
messageBody = MessageBody(
UIText.DynamicString("message1")
)
)
)
val (arrangement, viewModel) = SearchConversationMessagesViewModelArrangement()
.arrange()
// when
arrangement.withSuccessSearch(pagingDataFlow = PagingData.from(listOf(message1)))
// then
viewModel.searchConversationMessagesState.searchResult.test {
awaitItem().map {
it shouldBeEqualTo message1
}
}
}
@Test
fun `given blank search term, when searching for messages, then search is not triggered`() = runTest {
// given
val searchTerm = " "
val messages = listOf(
mockMessageWithText.copy(
messageContent = UIMessageContent.TextMessage(
messageBody = MessageBody(
UIText.DynamicString(" message1")
)
)
),
mockMessageWithText.copy(
messageContent = UIMessageContent.TextMessage(
messageBody = MessageBody(
UIText.DynamicString(" message2")
)
)
)
)
val (arrangement, viewModel) = SearchConversationMessagesViewModelArrangement()
.withSuccessSearch(PagingData.from(messages))
.arrange()
// when
viewModel.searchQueryChanged(TextFieldValue(searchTerm))
advanceUntilIdle()
// then
assertEquals(
TextFieldValue(searchTerm),
viewModel.searchConversationMessagesState.searchQuery
)
coVerify(exactly = 0) {
arrangement.getSearchMessagesForConversation(
searchTerm,
arrangement.conversationId,
any()
)
}
}
@Test
fun `given search term with space, when searching for messages, then search results are as expected`() = runTest {
// given
val searchTerm = "no "
val message1 = mockMessageWithText.copy(
messageContent = UIMessageContent.TextMessage(
messageBody = MessageBody(
UIText.DynamicString("not a normal text")
)
)
)
val message2 = mockMessageWithText.copy(
messageContent = UIMessageContent.TextMessage(
messageBody = MessageBody(
UIText.DynamicString("this message contains a no message")
)
)
)
val messages = listOf(
message1,
message2
)
val (arrangement, viewModel) = SearchConversationMessagesViewModelArrangement()
.withSuccessSearch(PagingData.from(messages))
.arrange()
// when
viewModel.searchQueryChanged(TextFieldValue(searchTerm))
advanceUntilIdle()
// then
assertEquals(
TextFieldValue(searchTerm),
viewModel.searchConversationMessagesState.searchQuery
)
coVerify(exactly = 0) {
arrangement.getSearchMessagesForConversation(
searchTerm,
arrangement.conversationId,
any()
)
}
viewModel.searchConversationMessagesState.searchResult.test {
awaitItem().map {
it shouldBeEqualTo message2
}
}
}
@Test
fun `given search term with empty space at start and at end, when searching for messages, then specific messages are returned`() =
runTest {
// given
val searchTerm = "message"
val message1 = mockMessageWithText.copy(
messageContent = UIMessageContent.TextMessage(
messageBody = MessageBody(
UIText.DynamicString(" message1 ")
)
)
)
val (arrangement, viewModel) = SearchConversationMessagesViewModelArrangement()
.arrange()
// when
viewModel.searchQueryChanged(TextFieldValue(searchTerm))
// then
viewModel.searchConversationMessagesState.searchResult.test {
arrangement.withSuccessSearch(pagingDataFlow = PagingData.from(listOf(message1)))
awaitItem().map {
it shouldBeEqualTo message1
}
}
}
class SearchConversationMessagesViewModelArrangement {
val conversationId: ConversationId = ConversationId(
value = "some-dummy-value",
domain = "some-dummy-domain"
)
private val messagesChannel = Channel<PagingData<UIMessage>>(capacity = Channel.UNLIMITED)
@MockK
private lateinit var savedStateHandle: SavedStateHandle
@MockK
lateinit var getSearchMessagesForConversation: GetConversationMessagesFromSearchUseCase
private val viewModel: SearchConversationMessagesViewModel by lazy {
SearchConversationMessagesViewModel(
getSearchMessagesForConversation = getSearchMessagesForConversation,
dispatchers = TestDispatcherProvider(),
savedStateHandle = savedStateHandle
)
}
init {
// Tests setup
MockKAnnotations.init(this, relaxUnitFun = true)
mockUri()
every { savedStateHandle.navArgs<SearchConversationMessagesNavArgs>() } returns SearchConversationMessagesNavArgs(
conversationId = conversationId
)
every { savedStateHandle.get<Bundle?>("searchConversationMessagesState") } returns bundleOf(
"value" to ""
)
coEvery {
getSearchMessagesForConversation(
any(),
any(),
any()
)
} returns messagesChannel.consumeAsFlow()
}
suspend fun withSuccessSearch(
pagingDataFlow: PagingData<UIMessage>
) = apply {
messagesChannel.send(pagingDataFlow)
}
fun arrange() = this to viewModel
}
}