diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PublishSettingsViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PublishSettingsViewModel.kt index 6b9ac3478a36..0c3a87edcbef 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PublishSettingsViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PublishSettingsViewModel.kt @@ -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()) } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/EditPostPublishSettingsViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/EditPostPublishSettingsViewModelTest.kt index ff79f33bbd8f..ecd6b449465b 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/EditPostPublishSettingsViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/EditPostPublishSettingsViewModelTest.kt @@ -95,6 +95,7 @@ class EditPostPublishSettingsViewModelTest : BaseUnitTest() { null } whenever(editPostRepository.getPost()).thenReturn(post) + whenever(editPostRepository.hasPost()).thenReturn(true) } @Test @@ -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") + } }