Skip to content

Commit

Permalink
Merge pull request #20745 from wordpress-mobile/issue/20272-npe-publi…
Browse files Browse the repository at this point in the history
…sh-settings

Fix: Null Pointer Exception in PublishSettingsViewModel
  • Loading branch information
AjeshRPai authored May 2, 2024
2 parents 3be32da + 980cb73 commit 5837010
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ constructor(

open fun start(postRepository: EditPostRepository?) {
editPostRepository = postRepository
val startCalendar = postRepository?.let { getCurrentPublishDateAsCalendar(it) }
?: localeManagerWrapper.getCurrentCalendar()
val startCalendar = postRepository?.takeIf { it.hasPost() }?.let {
getCurrentPublishDateAsCalendar(it)
} ?: localeManagerWrapper.getCurrentCalendar()
updateDateAndTimeFromCalendar(startCalendar)
onPostStatusChanged(postRepository?.getPost())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class EditPostPublishSettingsViewModelTest : BaseUnitTest() {
null
}
whenever(editPostRepository.getPost()).thenReturn(post)
whenever(editPostRepository.hasPost()).thenReturn(true)
}

@Test
Expand Down Expand Up @@ -450,4 +451,25 @@ class EditPostPublishSettingsViewModelTest : BaseUnitTest() {

assertThat(schedulingReminderPeriod).isEqualTo(ONE_HOUR)
}

@Test
fun `on start sets current date when post not present in the repository`() {
var uiModel: PublishUiModel? = null
viewModel.onUiModel.observeForever {
uiModel = it
}

whenever(editPostRepository.hasPost()).thenReturn(false)
whenever(editPostRepository.getPost()).thenReturn(null)

viewModel.start(editPostRepository)

assertThat(viewModel.year).isEqualTo(2019)
assertThat(viewModel.month).isEqualTo(6)
assertThat(viewModel.day).isEqualTo(6)
assertThat(viewModel.hour).isEqualTo(10)
assertThat(viewModel.minute).isEqualTo(20)

assertThat(uiModel!!.publishDateLabel).isEqualTo("Immediately")
}
}

0 comments on commit 5837010

Please sign in to comment.