-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathHomeViewModelTest.kt
More file actions
276 lines (245 loc) · 10.8 KB
/
HomeViewModelTest.kt
File metadata and controls
276 lines (245 loc) · 10.8 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* 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
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.datastore.GlobalDataStore
import com.wire.android.datastore.UserDataStore
import com.wire.android.framework.TestUser
import com.wire.android.migration.userDatabase.ShouldTriggerMigrationForUserUserCase
import com.wire.kalium.logic.data.user.SelfUser
import com.wire.kalium.logic.data.user.UserAvailabilityStatus
import com.wire.kalium.logic.feature.client.NeedsToRegisterClientUseCase
import com.wire.kalium.logic.feature.legalhold.LegalHoldStateForSelfUser
import com.wire.kalium.logic.feature.legalhold.ObserveLegalHoldStateForSelfUserUseCase
import com.wire.kalium.logic.feature.personaltoteamaccount.CanMigrateFromPersonalToTeamUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.internal.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
@OptIn(ExperimentalCoroutinesApi::class)
@ExtendWith(CoroutineTestExtension::class)
class HomeViewModelTest {
@Test
fun `given legal hold request pending, then shouldDisplayLegalHoldIndicator is true`() =
runTest {
// given
val (_, viewModel) = Arrangement()
.withLegalHoldStatus(flowOf(LegalHoldStateForSelfUser.PendingRequest))
.arrange()
// then
assertEquals(true, viewModel.homeState.shouldDisplayLegalHoldIndicator)
}
@Test
fun `given legal hold enabled, then shouldDisplayLegalHoldIndicator is true`() = runTest {
// given
val (_, viewModel) = Arrangement()
.withLegalHoldStatus(flowOf(LegalHoldStateForSelfUser.Enabled))
.arrange()
// then
assertEquals(true, viewModel.homeState.shouldDisplayLegalHoldIndicator)
}
@Test
fun `given legal hold disabled and no request available, then shouldDisplayLegalHoldIndicator is false`() =
runTest {
// given
val (_, viewModel) = Arrangement()
.withLegalHoldStatus(flowOf(LegalHoldStateForSelfUser.Disabled))
.arrange()
// then
assertEquals(false, viewModel.homeState.shouldDisplayLegalHoldIndicator)
}
@Test
fun `given legal hold enabled, when user status changes, then shouldDisplayLegalHoldIndicator should keep the same`() =
runTest {
// given
val selfFlow =
MutableStateFlow(TestUser.SELF_USER.copy(availabilityStatus = UserAvailabilityStatus.AVAILABLE))
val (_, viewModel) = Arrangement()
.withLegalHoldStatus(flowOf(LegalHoldStateForSelfUser.Enabled))
.withSelfUser(selfFlow)
.arrange()
// when
selfFlow.emit(TestUser.SELF_USER.copy(availabilityStatus = UserAvailabilityStatus.AWAY))
// then
assertEquals(true, viewModel.homeState.shouldDisplayLegalHoldIndicator)
}
@Test
fun `given migration not completed, when checking requirements, then return HomeRequirement Migration`() = runTest {
// given
val (arrangement, viewModel) = Arrangement()
.withShouldTriggerMigrationForUserReturning(true)
.arrange()
viewModel.actions.test {
// when
viewModel.checkRequirements()
advanceUntilIdle()
// then
assertEquals(HomeRequirement.Migration(TestUser.SELF_USER.id), expectMostRecentItem())
}
}
@Test
fun `given client not registered, when checking requirements, then return HomeRequirement RegisterDevice`() = runTest {
// given
val (arrangement, viewModel) = Arrangement()
.withShouldTriggerMigrationForUserReturning(false)
.withNeedsToRegisterClientReturning(true)
.arrange()
viewModel.actions.test {
// when
viewModel.checkRequirements()
// then
assertEquals(HomeRequirement.RegisterDevice, expectMostRecentItem())
}
}
@Test
fun `given initial sync not completed, when checking requirements, then return HomeRequirement InitialSync`() = runTest {
// given
val (arrangement, viewModel) = Arrangement()
.withShouldTriggerMigrationForUserReturning(false)
.withNeedsToRegisterClientReturning(false)
.withInitialSyncCompletedReturning(flowOf(false))
.arrange()
viewModel.actions.test {
// when
viewModel.checkRequirements()
// then
assertEquals(HomeRequirement.InitialSync, expectMostRecentItem())
}
}
@Test
fun `given handle not set, when checking requirements, then return HomeRequirement CreateAccountUsername`() = runTest {
// given
val (arrangement, viewModel) = Arrangement()
.withShouldTriggerMigrationForUserReturning(false)
.withNeedsToRegisterClientReturning(false)
.withInitialSyncCompletedReturning(flowOf(true))
.withSelfUser(flowOf(TestUser.SELF_USER.copy(handle = null)))
.arrange()
viewModel.actions.test {
// when
viewModel.checkRequirements()
// then
assertEquals(HomeRequirement.CreateAccountUsername, expectMostRecentItem())
}
}
@Test
fun `given migration completed and welcome not yet shown, when checking requirements, then show welcome message`() = runTest {
// given
val (arrangement, viewModel) = Arrangement()
.withShouldTriggerMigrationForUserReturning(false)
.withNeedsToRegisterClientReturning(false)
.withInitialSyncCompletedReturning(flowOf(true))
.withSelfUser(flowOf(TestUser.SELF_USER.copy(handle = "handle")))
.withMigrationCompletedReturning(true)
.withWelcomeScreenPresentedReturning(false)
.arrange()
// when
viewModel.checkRequirements()
// then
assertEquals(true, viewModel.homeState.shouldDisplayWelcomeMessage)
}
@Test
fun `given migration completed and welcome already shown, when checking requirements, then do not show welcome message`() = runTest {
// given
val (arrangement, viewModel) = Arrangement()
.withShouldTriggerMigrationForUserReturning(false)
.withNeedsToRegisterClientReturning(false)
.withInitialSyncCompletedReturning(flowOf(true))
.withSelfUser(flowOf(TestUser.SELF_USER.copy(handle = "handle")))
.withMigrationCompletedReturning(true)
.withWelcomeScreenPresentedReturning(true)
.arrange()
// when
viewModel.checkRequirements()
// then
assertEquals(false, viewModel.homeState.shouldDisplayWelcomeMessage)
}
internal class Arrangement {
@MockK
lateinit var savedStateHandle: SavedStateHandle
@MockK
lateinit var globalDataStore: GlobalDataStore
@MockK
lateinit var dataStore: UserDataStore
@MockK
lateinit var observeSelfUser: ObserveSelfUserUseCase
@MockK
lateinit var needsToRegisterClient: NeedsToRegisterClientUseCase
@MockK
lateinit var observeLegalHoldStatusForSelfUser: ObserveLegalHoldStateForSelfUserUseCase
@MockK
lateinit var shouldTriggerMigrationForUser: ShouldTriggerMigrationForUserUserCase
@MockK
lateinit var canMigrateFromPersonalToTeam: CanMigrateFromPersonalToTeamUseCase
private val viewModel by lazy {
HomeViewModel(
savedStateHandle = savedStateHandle,
globalDataStore = globalDataStore,
dataStore = dataStore,
observeSelf = observeSelfUser,
needsToRegisterClient = needsToRegisterClient,
observeLegalHoldStatusForSelfUser = observeLegalHoldStatusForSelfUser,
shouldTriggerMigrationForUser = shouldTriggerMigrationForUser,
canMigrateFromPersonalToTeam = canMigrateFromPersonalToTeam,
)
}
init {
MockKAnnotations.init(this, relaxUnitFun = true)
withSelfUser(flowOf(TestUser.SELF_USER))
withCanMigrateFromPersonalToTeamReturning(true)
withLegalHoldStatus(flowOf(LegalHoldStateForSelfUser.Disabled))
}
fun withSelfUser(result: Flow<SelfUser>) = apply {
coEvery { observeSelfUser.invoke() } returns result
}
private fun withCanMigrateFromPersonalToTeamReturning(result: Boolean) = apply {
coEvery { canMigrateFromPersonalToTeam.invoke() } returns result
coEvery { dataStore.isCreateTeamNoticeRead() } returns flowOf(false)
}
fun withLegalHoldStatus(result: Flow<LegalHoldStateForSelfUser>) = apply {
coEvery { observeLegalHoldStatusForSelfUser.invoke() } returns result
}
fun withShouldTriggerMigrationForUserReturning(result: Boolean) = apply {
coEvery { shouldTriggerMigrationForUser(any()) } returns result
}
fun withNeedsToRegisterClientReturning(result: Boolean) = apply {
coEvery { needsToRegisterClient() } returns result
}
fun withInitialSyncCompletedReturning(result: Flow<Boolean>) = apply {
coEvery { dataStore.initialSyncCompleted } returns result
}
fun withMigrationCompletedReturning(result: Boolean) = apply {
coEvery { globalDataStore.isMigrationCompleted() } returns result
}
fun withWelcomeScreenPresentedReturning(result: Boolean) = apply {
coEvery { globalDataStore.isWelcomeScreenPresented() } returns result
}
fun arrange() = this to viewModel
}
}