Fix the test.
This commit is contained in:
parent
574e6199fe
commit
1bbfad4a8f
3 changed files with 33 additions and 25 deletions
|
|
@ -26,6 +26,7 @@ import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.core.net.toUri
|
||||||
import dagger.assisted.Assisted
|
import dagger.assisted.Assisted
|
||||||
import dagger.assisted.AssistedFactory
|
import dagger.assisted.AssistedFactory
|
||||||
import dagger.assisted.AssistedInject
|
import dagger.assisted.AssistedInject
|
||||||
|
|
@ -111,8 +112,12 @@ class EditUserProfilePresenter @AssistedInject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun hasDisplayNameChanged(name: String?, currentUser: MatrixUser?) = name?.trim() != currentUser?.displayName?.trim()
|
private fun hasDisplayNameChanged(name: String?, currentUser: MatrixUser) =
|
||||||
private fun hasAvatarUrlChanged(avatarUri: Uri?, currentUser: MatrixUser?) = avatarUri?.toString()?.trim() != currentUser?.avatarUrl?.trim()
|
name?.trim() != currentUser.displayName?.trim()
|
||||||
|
|
||||||
|
private fun hasAvatarUrlChanged(avatarUri: Uri?, currentUser: MatrixUser) =
|
||||||
|
// Need to call `toUri()?.toString()` to make the test pass (we mockk Uri)
|
||||||
|
avatarUri?.toString()?.trim() != currentUser.avatarUrl?.toUri()?.toString()?.trim()
|
||||||
|
|
||||||
private fun CoroutineScope.saveChanges(name: String?, avatarUri: Uri?, currentUser: MatrixUser, action: MutableState<Async<Unit>>) = launch {
|
private fun CoroutineScope.saveChanges(name: String?, avatarUri: Uri?, currentUser: MatrixUser, action: MutableState<Async<Unit>>) = launch {
|
||||||
val results = mutableListOf<Result<Unit>>()
|
val results = mutableListOf<Result<Unit>>()
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ import io.element.android.libraries.mediapickers.test.FakePickerProvider
|
||||||
import io.element.android.libraries.mediaupload.api.MediaUploadInfo
|
import io.element.android.libraries.mediaupload.api.MediaUploadInfo
|
||||||
import io.element.android.libraries.mediaupload.test.FakeMediaPreProcessor
|
import io.element.android.libraries.mediaupload.test.FakeMediaPreProcessor
|
||||||
import io.element.android.tests.testutils.WarmUpRule
|
import io.element.android.tests.testutils.WarmUpRule
|
||||||
|
import io.element.android.tests.testutils.consumeItemsUntilPredicate
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import io.mockk.mockkStatic
|
import io.mockk.mockkStatic
|
||||||
|
|
@ -87,14 +88,14 @@ class EditUserProfilePresenterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `present - initial state is created from room info`() = runTest {
|
fun `present - initial state is created from user info`() = runTest {
|
||||||
val user = aMatrixUser(avatarUrl = AN_AVATAR_URL)
|
val user = aMatrixUser(avatarUrl = AN_AVATAR_URL)
|
||||||
val presenter = createEditUserProfilePresenter(matrixUser = user)
|
val presenter = createEditUserProfilePresenter(matrixUser = user)
|
||||||
moleculeFlow(RecompositionMode.Immediate) {
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
presenter.present()
|
presenter.present()
|
||||||
}.test {
|
}.test {
|
||||||
val initialState = awaitItem()
|
val initialState = awaitItem()
|
||||||
assertThat(initialState.userId).isEqualTo(user.userId.value)
|
assertThat(initialState.userId).isEqualTo(user.userId)
|
||||||
assertThat(initialState.displayName).isEqualTo(user.displayName)
|
assertThat(initialState.displayName).isEqualTo(user.displayName)
|
||||||
assertThat(initialState.userAvatarUrl).isEqualTo(userAvatarUri)
|
assertThat(initialState.userAvatarUrl).isEqualTo(userAvatarUri)
|
||||||
assertThat(initialState.avatarActions).containsExactly(
|
assertThat(initialState.avatarActions).containsExactly(
|
||||||
|
|
@ -102,7 +103,7 @@ class EditUserProfilePresenterTest {
|
||||||
AvatarAction.TakePhoto,
|
AvatarAction.TakePhoto,
|
||||||
AvatarAction.Remove
|
AvatarAction.Remove
|
||||||
)
|
)
|
||||||
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
|
assertThat(initialState.saveButtonEnabled).isFalse()
|
||||||
assertThat(initialState.saveAction).isInstanceOf(Async.Uninitialized::class.java)
|
assertThat(initialState.saveAction).isInstanceOf(Async.Uninitialized::class.java)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -178,26 +179,26 @@ class EditUserProfilePresenterTest {
|
||||||
presenter.present()
|
presenter.present()
|
||||||
}.test {
|
}.test {
|
||||||
val initialState = awaitItem()
|
val initialState = awaitItem()
|
||||||
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
|
assertThat(initialState.saveButtonEnabled).isFalse()
|
||||||
// Once a change is made, the save button is enabled
|
// Once a change is made, the save button is enabled
|
||||||
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
|
||||||
awaitItem().apply {
|
awaitItem().apply {
|
||||||
assertThat(saveButtonEnabled).isEqualTo(true)
|
assertThat(saveButtonEnabled).isTrue()
|
||||||
}
|
}
|
||||||
// If it's reverted then the save disables again
|
// If it's reverted then the save disables again
|
||||||
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name"))
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name"))
|
||||||
awaitItem().apply {
|
awaitItem().apply {
|
||||||
assertThat(saveButtonEnabled).isEqualTo(false)
|
assertThat(saveButtonEnabled).isFalse()
|
||||||
}
|
}
|
||||||
// Make a change...
|
// Make a change...
|
||||||
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
||||||
awaitItem().apply {
|
awaitItem().apply {
|
||||||
assertThat(saveButtonEnabled).isEqualTo(true)
|
assertThat(saveButtonEnabled).isTrue()
|
||||||
}
|
}
|
||||||
// Revert it...
|
// Revert it...
|
||||||
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
awaitItem().apply {
|
awaitItem().apply {
|
||||||
assertThat(saveButtonEnabled).isEqualTo(false)
|
assertThat(saveButtonEnabled).isFalse()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -211,26 +212,26 @@ class EditUserProfilePresenterTest {
|
||||||
presenter.present()
|
presenter.present()
|
||||||
}.test {
|
}.test {
|
||||||
val initialState = awaitItem()
|
val initialState = awaitItem()
|
||||||
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
|
assertThat(initialState.saveButtonEnabled).isFalse()
|
||||||
// Once a change is made, the save button is enabled
|
// Once a change is made, the save button is enabled
|
||||||
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
|
||||||
awaitItem().apply {
|
awaitItem().apply {
|
||||||
assertThat(saveButtonEnabled).isEqualTo(true)
|
assertThat(saveButtonEnabled).isTrue()
|
||||||
}
|
}
|
||||||
// If it's reverted then the save disables again
|
// If it's reverted then the save disables again
|
||||||
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("fallback"))
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name"))
|
||||||
awaitItem().apply {
|
awaitItem().apply {
|
||||||
assertThat(saveButtonEnabled).isEqualTo(false)
|
assertThat(saveButtonEnabled).isFalse()
|
||||||
}
|
}
|
||||||
// Make a change...
|
// Make a change...
|
||||||
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
awaitItem().apply {
|
awaitItem().apply {
|
||||||
assertThat(saveButtonEnabled).isEqualTo(true)
|
assertThat(saveButtonEnabled).isTrue()
|
||||||
}
|
}
|
||||||
// Revert it...
|
// Revert it...
|
||||||
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
||||||
awaitItem().apply {
|
awaitItem().apply {
|
||||||
assertThat(saveButtonEnabled).isEqualTo(false)
|
assertThat(saveButtonEnabled).isFalse()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -250,7 +251,7 @@ class EditUserProfilePresenterTest {
|
||||||
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("New name"))
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("New name"))
|
||||||
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
||||||
initialState.eventSink(EditUserProfileEvents.Save)
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
skipItems(5)
|
consumeItemsUntilPredicate { matrixClient.setDisplayNameCalled && matrixClient.removeAvatarCalled && !matrixClient.uploadAvatarCalled }
|
||||||
assertThat(matrixClient.setDisplayNameCalled).isTrue()
|
assertThat(matrixClient.setDisplayNameCalled).isTrue()
|
||||||
assertThat(matrixClient.removeAvatarCalled).isTrue()
|
assertThat(matrixClient.removeAvatarCalled).isTrue()
|
||||||
assertThat(matrixClient.uploadAvatarCalled).isFalse()
|
assertThat(matrixClient.uploadAvatarCalled).isFalse()
|
||||||
|
|
@ -259,7 +260,7 @@ class EditUserProfilePresenterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `present - save doesn't change room details if they're the same trimmed`() = runTest {
|
fun `present - save does not change room details if they're the same trimmed`() = runTest {
|
||||||
val matrixClient = FakeMatrixClient()
|
val matrixClient = FakeMatrixClient()
|
||||||
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
val presenter = createEditUserProfilePresenter(
|
val presenter = createEditUserProfilePresenter(
|
||||||
|
|
@ -272,7 +273,8 @@ class EditUserProfilePresenterTest {
|
||||||
val initialState = awaitItem()
|
val initialState = awaitItem()
|
||||||
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName(" Name "))
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName(" Name "))
|
||||||
initialState.eventSink(EditUserProfileEvents.Save)
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
assertThat(matrixClient.setDisplayNameCalled).isTrue()
|
consumeItemsUntilPredicate { matrixClient.setDisplayNameCalled && !matrixClient.removeAvatarCalled && !matrixClient.uploadAvatarCalled }
|
||||||
|
assertThat(matrixClient.setDisplayNameCalled).isFalse()
|
||||||
assertThat(matrixClient.uploadAvatarCalled).isFalse()
|
assertThat(matrixClient.uploadAvatarCalled).isFalse()
|
||||||
assertThat(matrixClient.removeAvatarCalled).isFalse()
|
assertThat(matrixClient.removeAvatarCalled).isFalse()
|
||||||
cancelAndIgnoreRemainingEvents()
|
cancelAndIgnoreRemainingEvents()
|
||||||
|
|
@ -280,7 +282,7 @@ class EditUserProfilePresenterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `present - save doesn't change name if it's now empty`() = runTest {
|
fun `present - save does not change name if it's now empty`() = runTest {
|
||||||
val matrixClient = FakeMatrixClient()
|
val matrixClient = FakeMatrixClient()
|
||||||
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
val presenter = createEditUserProfilePresenter(
|
val presenter = createEditUserProfilePresenter(
|
||||||
|
|
@ -315,7 +317,7 @@ class EditUserProfilePresenterTest {
|
||||||
val initialState = awaitItem()
|
val initialState = awaitItem()
|
||||||
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
initialState.eventSink(EditUserProfileEvents.Save)
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
skipItems(2)
|
consumeItemsUntilPredicate { matrixClient.uploadAvatarCalled }
|
||||||
assertThat(matrixClient.uploadAvatarCalled).isTrue()
|
assertThat(matrixClient.uploadAvatarCalled).isTrue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -377,7 +379,7 @@ class EditUserProfilePresenterTest {
|
||||||
val matrixClient = FakeMatrixClient().apply {
|
val matrixClient = FakeMatrixClient().apply {
|
||||||
givenSetDisplayNameResult(Result.failure(Throwable("!")))
|
givenSetDisplayNameResult(Result.failure(Throwable("!")))
|
||||||
}
|
}
|
||||||
val presenter = createEditUserProfilePresenter(matrixUser = user)
|
val presenter = createEditUserProfilePresenter(matrixUser = user, matrixClient = matrixClient)
|
||||||
moleculeFlow(RecompositionMode.Immediate) {
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
presenter.present()
|
presenter.present()
|
||||||
}.test {
|
}.test {
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,7 @@ class FakeMatrixClient(
|
||||||
override suspend fun getAccountManagementUrl(action: AccountManagementAction?): Result<String?> {
|
override suspend fun getAccountManagementUrl(action: AccountManagementAction?): Result<String?> {
|
||||||
return accountManagementUrlString
|
return accountManagementUrlString
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun uploadMedia(
|
override suspend fun uploadMedia(
|
||||||
mimeType: String,
|
mimeType: String,
|
||||||
data: ByteArray,
|
data: ByteArray,
|
||||||
|
|
@ -151,17 +152,17 @@ class FakeMatrixClient(
|
||||||
return uploadMediaResult
|
return uploadMediaResult
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setDisplayName(displayName: String): Result<Unit> {
|
override suspend fun setDisplayName(displayName: String): Result<Unit> = simulateLongTask {
|
||||||
setDisplayNameCalled = true
|
setDisplayNameCalled = true
|
||||||
return setDisplayNameResult
|
return setDisplayNameResult
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun uploadAvatar(mimeType: String, data: ByteArray): Result<Unit> {
|
override suspend fun uploadAvatar(mimeType: String, data: ByteArray): Result<Unit> = simulateLongTask {
|
||||||
uploadAvatarCalled = true
|
uploadAvatarCalled = true
|
||||||
return uploadAvatarResult
|
return uploadAvatarResult
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun removeAvatar(): Result<Unit> {
|
override suspend fun removeAvatar(): Result<Unit> = simulateLongTask {
|
||||||
removeAvatarCalled = true
|
removeAvatarCalled = true
|
||||||
return removeAvatarResult
|
return removeAvatarResult
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue