Skip to content

Commit

Permalink
✅ TEST: network issue feedback ui
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Sulzer <jonas@violoncello.ch>
  • Loading branch information
violoncelloCH committed May 31, 2024
1 parent e7052c0 commit 2d70887
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,12 @@ fun ProfileCreationUI(
modifier = Modifier.fillMaxWidth()
)
},
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }
snackbarHost = {
SnackbarHost(
hostState = snackbarHostState,
modifier = Modifier.testTag("profile-creation-snackbar")
)
}
) { innerPadding ->
Box(
modifier = modifier.fillMaxSize().padding(innerPadding).testTag("profile-creation"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import com.github.swent.echo.connectivity.NetworkService
import com.github.swent.echo.data.model.SectionEPFL
import com.github.swent.echo.data.model.SemesterEPFL
import com.github.swent.echo.data.model.Tag
import com.github.swent.echo.data.model.UserProfile
import com.github.swent.echo.data.repository.RepositoryImpl
import com.github.swent.echo.data.repository.RepositoryStoreWhileNoInternetException
import com.github.swent.echo.data.repository.SimpleRepository
import com.github.swent.echo.ui.navigation.NavigationActions
import com.github.swent.echo.viewmodels.authentication.CreateProfileViewModel
import com.github.swent.echo.viewmodels.tag.TagViewModel
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -84,6 +90,34 @@ class CreateProfileTest {
composeTestRule.onNodeWithTag("BA2").assertExists()
}

@Test
fun `profile creation while network error shows error snackbar`() {
val authenticationService: AuthenticationService = mockk(relaxed = true)
val mockedRepository = mockk<RepositoryImpl>()
val mockedNetworkService = mockk<NetworkService>()
every { mockedNetworkService.isOnline } returns MutableStateFlow(true)
coEvery { mockedRepository.getUserProfile(any()) } returns UserProfile.EMPTY
coEvery { mockedRepository.getUserProfilePicture(any()) } returns ByteArray(0)

val viewModel =
CreateProfileViewModel(authenticationService, mockedRepository, mockedNetworkService)

val mockedNavAction = mockk<NavigationActions>()
val mockedTagViewModel = mockk<TagViewModel>()

composeTestRule.setContent {
ProfileCreationScreen(
viewModel = viewModel,
navAction = mockedNavAction,
tagviewModel = mockedTagViewModel
)
}
coEvery { mockedRepository.setUserProfile(any()) } throws
RepositoryStoreWhileNoInternetException("test")
composeTestRule.onNodeWithTag("Save").performClick()
composeTestRule.onNodeWithTag("profile-creation-snackbar").assertIsDisplayed()
}

@Test
fun profileCreationPictureIsCorrect() {
val picture = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.github.swent.echo.data.model.Tag
import com.github.swent.echo.data.model.UserProfile
import com.github.swent.echo.data.model.toAssociationHeader
import com.github.swent.echo.data.repository.Repository
import com.github.swent.echo.data.repository.RepositoryStoreWhileNoInternetException
import com.github.swent.echo.fakes.FakeAuthenticationService
import io.mockk.coEvery
import io.mockk.coVerify
Expand Down Expand Up @@ -145,6 +146,13 @@ class AssociationViewModelTest {
assert(associationViewModel.followedAssociations.value.contains(associationList[2]))
associationViewModel.onFollowAssociationChanged(associationList[2])
assert(!associationViewModel.followedAssociations.value.contains(associationList[2]))
coEvery { mockedRepository.setUserProfile(any()) } throws RepositoryStoreWhileNoInternetException("test")
associationViewModel.onFollowAssociationChanged(associationList[2])
scheduler.runCurrent()
assert(!associationViewModel.followedAssociations.value.contains(associationList[2]))
assert(associationViewModel.status.value is AssociationStatus.Error)
associationViewModel.resetErrorState()
assert(associationViewModel.status.value is AssociationStatus.Okay)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.github.swent.echo.data.model.Location
import com.github.swent.echo.data.model.Tag
import com.github.swent.echo.data.model.UserProfile
import com.github.swent.echo.data.repository.Repository
import com.github.swent.echo.data.repository.RepositoryStoreWhileNoInternetException
import com.github.swent.echo.fakes.FakeAuthenticationService
import io.mockk.coEvery
import io.mockk.coVerify
Expand Down Expand Up @@ -120,6 +121,7 @@ class EventViewModelTest {
eventViewModel.setEvent(TEST_EVENT)
eventViewModel.saveEvent()
eventViewModel.saveEvent()
scheduler.runCurrent()
verify { Log.w(any(), any() as String) }
}

Expand All @@ -132,6 +134,7 @@ class EventViewModelTest {
)
eventViewModel.setEvent(event)
eventViewModel.saveEvent()
scheduler.runCurrent()
assertTrue(eventViewModel.status.value is EventStatus.Error)
}

Expand All @@ -140,6 +143,19 @@ class EventViewModelTest {
val event = TEST_EVENT.copy(title = " ")
eventViewModel.setEvent(event)
eventViewModel.saveEvent()
scheduler.runCurrent()
assertTrue(eventViewModel.status.value is EventStatus.Error)
}

@Test
fun saveWhileNetworkErrorChangesStatusToError() {
coEvery { mockedRepository.createEvent(TEST_EVENT) } throws
RepositoryStoreWhileNoInternetException("test")
coEvery { mockedRepository.setEvent(TEST_EVENT) } throws
RepositoryStoreWhileNoInternetException("test")
eventViewModel.setEvent(TEST_EVENT)
eventViewModel.saveEvent()
scheduler.runCurrent()
assertTrue(eventViewModel.status.value is EventStatus.Error)
}

Expand Down

0 comments on commit 2d70887

Please sign in to comment.