Skip to content

Commit 27e567e

Browse files
committed
Fix the test.
1 parent f7f9a78 commit 27e567e

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/user/editprofile/EditUserProfilePresenter.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import androidx.compose.runtime.remember
2626
import androidx.compose.runtime.rememberCoroutineScope
2727
import androidx.compose.runtime.saveable.rememberSaveable
2828
import androidx.compose.runtime.setValue
29+
import androidx.core.net.toUri
2930
import dagger.assisted.Assisted
3031
import dagger.assisted.AssistedFactory
3132
import dagger.assisted.AssistedInject
@@ -111,8 +112,12 @@ class EditUserProfilePresenter @AssistedInject constructor(
111112
)
112113
}
113114

114-
private fun hasDisplayNameChanged(name: String?, currentUser: MatrixUser?) = name?.trim() != currentUser?.displayName?.trim()
115-
private fun hasAvatarUrlChanged(avatarUri: Uri?, currentUser: MatrixUser?) = avatarUri?.toString()?.trim() != currentUser?.avatarUrl?.trim()
115+
private fun hasDisplayNameChanged(name: String?, currentUser: MatrixUser) =
116+
name?.trim() != currentUser.displayName?.trim()
117+
118+
private fun hasAvatarUrlChanged(avatarUri: Uri?, currentUser: MatrixUser) =
119+
// Need to call `toUri()?.toString()` to make the test pass (we mockk Uri)
120+
avatarUri?.toString()?.trim() != currentUser.avatarUrl?.toUri()?.toString()?.trim()
116121

117122
private fun CoroutineScope.saveChanges(name: String?, avatarUri: Uri?, currentUser: MatrixUser, action: MutableState<Async<Unit>>) = launch {
118123
val results = mutableListOf<Result<Unit>>()

features/preferences/impl/src/test/kotlin/io/element/android/features/preferences/impl/user/editprofile/EditUserProfilePresenterTest.kt

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import io.element.android.libraries.mediapickers.test.FakePickerProvider
3333
import io.element.android.libraries.mediaupload.api.MediaUploadInfo
3434
import io.element.android.libraries.mediaupload.test.FakeMediaPreProcessor
3535
import io.element.android.tests.testutils.WarmUpRule
36+
import io.element.android.tests.testutils.consumeItemsUntilPredicate
3637
import io.mockk.every
3738
import io.mockk.mockk
3839
import io.mockk.mockkStatic
@@ -87,22 +88,22 @@ class EditUserProfilePresenterTest {
8788
}
8889

8990
@Test
90-
fun `present - initial state is created from room info`() = runTest {
91+
fun `present - initial state is created from user info`() = runTest {
9192
val user = aMatrixUser(avatarUrl = AN_AVATAR_URL)
9293
val presenter = createEditUserProfilePresenter(matrixUser = user)
9394
moleculeFlow(RecompositionMode.Immediate) {
9495
presenter.present()
9596
}.test {
9697
val initialState = awaitItem()
97-
assertThat(initialState.userId).isEqualTo(user.userId.value)
98+
assertThat(initialState.userId).isEqualTo(user.userId)
9899
assertThat(initialState.displayName).isEqualTo(user.displayName)
99100
assertThat(initialState.userAvatarUrl).isEqualTo(userAvatarUri)
100101
assertThat(initialState.avatarActions).containsExactly(
101102
AvatarAction.ChoosePhoto,
102103
AvatarAction.TakePhoto,
103104
AvatarAction.Remove
104105
)
105-
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
106+
assertThat(initialState.saveButtonEnabled).isFalse()
106107
assertThat(initialState.saveAction).isInstanceOf(Async.Uninitialized::class.java)
107108
}
108109
}
@@ -178,26 +179,26 @@ class EditUserProfilePresenterTest {
178179
presenter.present()
179180
}.test {
180181
val initialState = awaitItem()
181-
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
182+
assertThat(initialState.saveButtonEnabled).isFalse()
182183
// Once a change is made, the save button is enabled
183184
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
184185
awaitItem().apply {
185-
assertThat(saveButtonEnabled).isEqualTo(true)
186+
assertThat(saveButtonEnabled).isTrue()
186187
}
187188
// If it's reverted then the save disables again
188189
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name"))
189190
awaitItem().apply {
190-
assertThat(saveButtonEnabled).isEqualTo(false)
191+
assertThat(saveButtonEnabled).isFalse()
191192
}
192193
// Make a change...
193194
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
194195
awaitItem().apply {
195-
assertThat(saveButtonEnabled).isEqualTo(true)
196+
assertThat(saveButtonEnabled).isTrue()
196197
}
197198
// Revert it...
198199
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
199200
awaitItem().apply {
200-
assertThat(saveButtonEnabled).isEqualTo(false)
201+
assertThat(saveButtonEnabled).isFalse()
201202
}
202203
}
203204
}
@@ -211,26 +212,26 @@ class EditUserProfilePresenterTest {
211212
presenter.present()
212213
}.test {
213214
val initialState = awaitItem()
214-
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
215+
assertThat(initialState.saveButtonEnabled).isFalse()
215216
// Once a change is made, the save button is enabled
216217
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
217218
awaitItem().apply {
218-
assertThat(saveButtonEnabled).isEqualTo(true)
219+
assertThat(saveButtonEnabled).isTrue()
219220
}
220221
// If it's reverted then the save disables again
221-
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("fallback"))
222+
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name"))
222223
awaitItem().apply {
223-
assertThat(saveButtonEnabled).isEqualTo(false)
224+
assertThat(saveButtonEnabled).isFalse()
224225
}
225226
// Make a change...
226227
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
227228
awaitItem().apply {
228-
assertThat(saveButtonEnabled).isEqualTo(true)
229+
assertThat(saveButtonEnabled).isTrue()
229230
}
230231
// Revert it...
231232
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
232233
awaitItem().apply {
233-
assertThat(saveButtonEnabled).isEqualTo(false)
234+
assertThat(saveButtonEnabled).isFalse()
234235
}
235236
}
236237
}
@@ -250,7 +251,7 @@ class EditUserProfilePresenterTest {
250251
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("New name"))
251252
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
252253
initialState.eventSink(EditUserProfileEvents.Save)
253-
skipItems(5)
254+
consumeItemsUntilPredicate { matrixClient.setDisplayNameCalled && matrixClient.removeAvatarCalled && !matrixClient.uploadAvatarCalled }
254255
assertThat(matrixClient.setDisplayNameCalled).isTrue()
255256
assertThat(matrixClient.removeAvatarCalled).isTrue()
256257
assertThat(matrixClient.uploadAvatarCalled).isFalse()
@@ -259,7 +260,7 @@ class EditUserProfilePresenterTest {
259260
}
260261

261262
@Test
262-
fun `present - save doesn't change room details if they're the same trimmed`() = runTest {
263+
fun `present - save does not change room details if they're the same trimmed`() = runTest {
263264
val matrixClient = FakeMatrixClient()
264265
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
265266
val presenter = createEditUserProfilePresenter(
@@ -272,15 +273,16 @@ class EditUserProfilePresenterTest {
272273
val initialState = awaitItem()
273274
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName(" Name "))
274275
initialState.eventSink(EditUserProfileEvents.Save)
275-
assertThat(matrixClient.setDisplayNameCalled).isTrue()
276+
consumeItemsUntilPredicate { matrixClient.setDisplayNameCalled && !matrixClient.removeAvatarCalled && !matrixClient.uploadAvatarCalled }
277+
assertThat(matrixClient.setDisplayNameCalled).isFalse()
276278
assertThat(matrixClient.uploadAvatarCalled).isFalse()
277279
assertThat(matrixClient.removeAvatarCalled).isFalse()
278280
cancelAndIgnoreRemainingEvents()
279281
}
280282
}
281283

282284
@Test
283-
fun `present - save doesn't change name if it's now empty`() = runTest {
285+
fun `present - save does not change name if it's now empty`() = runTest {
284286
val matrixClient = FakeMatrixClient()
285287
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
286288
val presenter = createEditUserProfilePresenter(
@@ -315,7 +317,7 @@ class EditUserProfilePresenterTest {
315317
val initialState = awaitItem()
316318
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
317319
initialState.eventSink(EditUserProfileEvents.Save)
318-
skipItems(2)
320+
consumeItemsUntilPredicate { matrixClient.uploadAvatarCalled }
319321
assertThat(matrixClient.uploadAvatarCalled).isTrue()
320322
}
321323
}
@@ -377,7 +379,7 @@ class EditUserProfilePresenterTest {
377379
val matrixClient = FakeMatrixClient().apply {
378380
givenSetDisplayNameResult(Result.failure(Throwable("!")))
379381
}
380-
val presenter = createEditUserProfilePresenter(matrixUser = user)
382+
val presenter = createEditUserProfilePresenter(matrixUser = user, matrixClient = matrixClient)
381383
moleculeFlow(RecompositionMode.Immediate) {
382384
presenter.present()
383385
}.test {

libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class FakeMatrixClient(
143143
override suspend fun getAccountManagementUrl(action: AccountManagementAction?): Result<String?> {
144144
return accountManagementUrlString
145145
}
146+
146147
override suspend fun uploadMedia(
147148
mimeType: String,
148149
data: ByteArray,
@@ -151,17 +152,17 @@ class FakeMatrixClient(
151152
return uploadMediaResult
152153
}
153154

154-
override suspend fun setDisplayName(displayName: String): Result<Unit> {
155+
override suspend fun setDisplayName(displayName: String): Result<Unit> = simulateLongTask {
155156
setDisplayNameCalled = true
156157
return setDisplayNameResult
157158
}
158159

159-
override suspend fun uploadAvatar(mimeType: String, data: ByteArray): Result<Unit> {
160+
override suspend fun uploadAvatar(mimeType: String, data: ByteArray): Result<Unit> = simulateLongTask {
160161
uploadAvatarCalled = true
161162
return uploadAvatarResult
162163
}
163164

164-
override suspend fun removeAvatar(): Result<Unit> {
165+
override suspend fun removeAvatar(): Result<Unit> = simulateLongTask {
165166
removeAvatarCalled = true
166167
return removeAvatarResult
167168
}

0 commit comments

Comments
 (0)