Skip to content

Commit

Permalink
Merge pull request #20572 from wordpress-mobile/fix/20530-site-creati…
Browse files Browse the repository at this point in the history
…on-error

Handles site successfully fetched but has not been found in the local db
  • Loading branch information
jarvislin authored Apr 3, 2024
2 parents 20ce089 + ade4a33 commit 9611e6f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SiteP
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SitePreviewLoadingShimmerState
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SitePreviewWebErrorUiState
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SiteNotCreatedErrorUiState
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SiteNotFoundInDbUiState
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.UrlData
import org.wordpress.android.ui.sitecreation.services.FetchWpComSiteUseCase
import org.wordpress.android.ui.sitecreation.usecases.isWordPressComSubDomain
Expand Down Expand Up @@ -136,9 +137,13 @@ class SitePreviewViewModel @Inject constructor(
private suspend fun fetchNewlyCreatedSiteModel(remoteSiteId: Long): SiteModel? {
val onSiteFetched = fetchWpComSiteUseCase.fetchSiteWithRetry(remoteSiteId)
return if (!onSiteFetched.isError) {
return requireNotNull(siteStore.getSiteBySiteId(remoteSiteId)) {
"Site successfully fetched but has not been found in the local db."
val site = siteStore.getSiteBySiteId(remoteSiteId)
if (site == null) {
withContext(mainDispatcher) {
updateUiState(SiteNotFoundInDbUiState)
}
}
site
} else {
null
}
Expand Down Expand Up @@ -224,6 +229,14 @@ class SitePreviewViewModel @Inject constructor(
errorTitle = UiStringRes(R.string.error),
)

data object SiteNotFoundInDbUiState : SitePreviewUiState(
urlData = UrlData("", "", 0 to 0, 0 to 0),
webViewVisibility = false,
webViewErrorVisibility = true,
subtitle = UiStringRes(R.string.site_creation_error_generic_title),
caption = UiStringRes(R.string.site_creation_error_generic_subtitle),
)

data class SitePreviewLoadingShimmerState(
val isFree: Boolean,
override val urlData: UrlData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SiteP
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SitePreviewContentUiState
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SitePreviewWebErrorUiState
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SiteNotCreatedErrorUiState
import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SiteNotFoundInDbUiState
import org.wordpress.android.ui.sitecreation.progress.LOADING_STATE_TEXT_ANIMATION_DELAY
import org.wordpress.android.ui.sitecreation.services.FetchWpComSiteUseCase
import org.wordpress.android.util.UrlUtilsWrapper
Expand Down Expand Up @@ -100,6 +101,13 @@ class SitePreviewViewModelTest : BaseUnitTest() {
verify(fetchWpComSiteUseCase).fetchSiteWithRetry(SITE_REMOTE_ID)
}

@Test
fun `on start if site is created but cannot be retrieved from fb fails show error`() = testWith(FETCH_SUCCESS) {
whenever(siteStore.getSiteBySiteId(SITE_REMOTE_ID)).thenReturn(null)
startViewModel(SITE_CREATION_STATE.copy(result = RESULT_NOT_IN_LOCAL_DB))
assertThat(viewModel.uiState.value).isInstanceOf(SiteNotFoundInDbUiState::class.java)
}

@Test
fun `on start does not show preview when fetching fails`() = testWith(FETCH_ERROR) {
startViewModel()
Expand Down

0 comments on commit 9611e6f

Please sign in to comment.